如果需要一個固定長度的字串,例如從00000到99999,前面可以自動補0
,修改"%05d" ,只須把5修改成所需要的長度
寫一個迴圈來驗證:
2014年10月22日 星期三
2010年8月3日 星期二
2010年7月14日 星期三
[Java] 檔案刪除 File.delete()
application.getRealPath("")為抓取真實路徑
File file = new File(application.getRealPath("abc.txt")); //jsp user
或是
File file = new File("c:/abc.txt")
file.delete();
2010年6月30日 星期三
[Java] quartz 工作定時排程器
[Java] quartz 工作定時排程器
http://polinwei.blogspot.com/2008/11/java-quartz.html
quartz 工作定時排程器
http://www.ebismacau.com/blog/archives/41
http://polinwei.blogspot.com/2008/11/java-quartz.html
quartz 工作定時排程器
http://www.ebismacau.com/blog/archives/41
2010年4月9日 星期五
[Java]JOptionPane

JOptionPane.showInputDialog("輸入一個整數:");
JOptionPane.showMessageDialog(Componentparent Component,Object message,String title, int messageType)
參數:
parentComponent - 確定在其中顯示對話框的 Frame;如果為null或者parentComponent 不具有 Frame,則使用預設的 Frame
message - 要顯示的 Object title - 對話框的標題字元串
messageType - 要顯示的訊息型別:ERROR_MESSAGE、INFORMATION_MESSAGE、WARNING_MESSAGE、QUESTION_MESSAGE 或 PLAIN_MESSAGE
2009年9月10日 星期四
[Java] String.split()
2009年7月24日 星期五
[Java] String 取代字元
String_object.replaceAll("123","456").replaceAll("a","b");
在String_object中把"456"取代成"123",可以串聯使用
在String_object中把"456"取代成"123",可以串聯使用
[Java] Date 日期比較
常常會使用到日期的比較,從db中取出日期,和今天比較,return為boolean
before是指Date_object是否是今天日期之後
Date_object.before(new Date());
after是指Date_object是否是今天日期之前
Date_object.after(new Date());
before是指Date_object是否是今天日期之後
Date_object.before(new Date());
after是指Date_object是否是今天日期之前
Date_object.after(new Date());
2009年6月6日 星期六
[Java] JSP 一些常用的正規式
驗證Email格式的正規式
email.matches("\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*"))
只能輸入英文和數字長度30
user_id.matches("[a-z|0-9]{1,30}")
[Java] JSP 正規式取出中文字
import java.util.regex.Matcher;
import java.util.regex.Pattern;
String str="o中文o";
String pattern="";
Pattern p=null;
Matcher m = null;
//中文字碼範圍
pattern="[\u4e00-\u9fa5]";
p=Pattern.compile(pattern);
m=p.matcher(str);
while(m.find()){
System.out.println(m.group());
}
引用http://www.walkone.com.tw/blog/index.jsp?user_id=test&subject_id=5728
[Java] JSP 讀取UTF-8檔案
以下程式不考慮UTF-8 BOM的問題
引用http://www.walkone.com.tw/blog/index.jsp?user_id=test&subject_id=5726
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
File fn=new File("utf8.txt");
FileInputStream fis = new FileInputStream(fn);
//指定utf-8編碼
BufferedReader br = new BufferedReader( new InputStreamReader( fis,"utf-8" ) );
String file_str_append="";
String file_str="";
while((file_str_append=br.readLine())!=null){
file_str+=file_str_append;
}
br.close();
fis.close();
//將utf-8所有檔案內容輸出
System.out.println(file_str);
引用http://www.walkone.com.tw/blog/index.jsp?user_id=test&subject_id=5726
2009年2月12日 星期四
[Java] 將陣列轉為ArrayList
String[] phone = {"0", "1", "2"}
ArrayList arrayphone = new ArrayList(Arrays.asList(phone.split(",")));
這樣可以把Array轉為ArrayList
ArrayList
這樣可以把Array轉為ArrayList
2009年1月6日 星期二
[Java] 複製clone()的使用方式
Stack copy = (Stack)list.clone();
參照http://royaltykao.blogspot.com/2008/10/javaclone.html
參照http://royaltykao.blogspot.com/2008/10/javaclone.html
2009年1月5日 星期一
[Java] NumberFormat 數字格式化
int i=0;
NumberFormat nf=NumberFormat.getInstance();
nf.setMinimumIntegerDigits(3);
for(i=0; i<107; i++){
int j=i;
System.out.println(" ");
}
或
NumberFormat formatter = NumberFormat.getInstance();
formatter.setMinimumIntegerDigits(3); 不滿三位數前面補0
formatter.setGroupingUsed(false);
String rnd=formatter.format(int);
NumberFormat nf=NumberFormat.getInstance();
nf.setMinimumIntegerDigits(3);
for(i=0; i<107; i++){
int j=i;
System.out.println("
}
或
NumberFormat formatter = NumberFormat.getInstance();
formatter.setMinimumIntegerDigits(3); 不滿三位數前面補0
formatter.setGroupingUsed(false);
String rnd=formatter.format(int);
2009年1月3日 星期六
[Java] Dom4j 解析xml格式(二)
Element element = (Element) config_xml.selectSingleNode("/root/jack[@sn='"+rand+"']");
if(element != null){
String attribute = element.attributeValue("sn");
String text = element.getText();
}
取xml中屬性的方式,先轉成Element 在判斷是否為空值
<root>
<jack sn="1">AAA</jack>
</root>
上面語法attribute變數的值是取jack中屬性sn的value,text變數的值為AAA
2008年8月16日 星期六
[Java] Dom4j 解析xml格式
Document doc = DocumentHelper.parseText(xmlRequest);
String AAA = doc.selectSingleNode("/action/AAA").getText();
String BBB = doc.selectSingleNode("/action/BBB").getText();
AAA的值為 "abc"
BBB的值為 "def"
其中"/action/AAA" 為xpath 除了需要dom4j.jar外
還需import jaxen.jar套件
[Java] Dom4j 產生xml格式
Document document = DocumentHelper.createDocument();
Element root = document.addElement("action");
root.addElement("AAA").addText("abc");
root.addElement("BBB").addText("def");
xmlRequest = document.asXML();
若要在ccc下面在加一個node
Element ccc = root.addElement("ccc");
ccc.addElement("ddd").addText("xyz");
xmlRequest輸出為以下字串
<action>
<AAA>abc<AAA>
<BBB>def<BBB>
<ccc>
<ddd>xyz</ddd>
</ccc>
</action>
2008年8月6日 星期三
[Java] 今天和任一日期相差的天數
Calendar cal = Calendar.getInstance();
cal.set(2008, Calendar.JULY, 28); //取得DB中的日期
long daterange = new Date().getTime() - cal.getTimeInMillis();
long time = 1000*3600*24;
System.out.println(daterange/time);
2008年8月3日 星期日
[Java] JDBC連接資料庫 (六)
寫在finally區塊的關閉連線,有例外發生時會關閉連線
finally {
if (rset != null) {
try {
rset.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (stmt != null) {
try {
stmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (conn != null) {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
[Java] JDBC連接資料庫 (五)
執行SQL Query和處理SQL執行結果
ResultSet rset = null;
try {
/**
* Enter SQL Query
*/
rset = stmt.executeQuery("select deptid, deptname from dept");
/**
* Process SQL Query and print out
*/
while(rset.next()) {
int deptid = rset.getInt("deptid");
String deptname = rset.getString(2);
System.out.println(deptid +", "+deptname);
}
} catch (SQLException e1) {
e1.printStackTrace();
}
訂閱:
文章 (Atom)