2009年1月6日 星期二

[Java] 複製clone()的使用方式

Stack copy = (Stack)list.clone();

參照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);

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年9月7日 星期日

Date date = new Date();

SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");

String a = sdf.format(date.getTime());

date = sdf.parse(a);

Test t = new Test();

t.setStartDate(date);

Date enddate = new Date();

String end = sdf.format(date.getTime()+(60L*60L*24L*30L*1000));
enddate = sdf.parse(end);

t.setEndDate(enddate);

Session session = MulderSessionFactory.getSession();
Transaction tx = session.beginTransaction();
session.saveOrUpdate(t);
tx.commit();
MulderSessionFactory.closeSession();

2008年8月24日 星期日

note

JSTL API
http://java.sun.com/products/jsp/jstl/1.1/docs/api/index.html

JSTL TLD
http://java.sun.com/products/jsp/jstl/1.1/docs/tlddocs/index.html

workspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps\專案名稱

http://javaprepare.com/
http://www.javaranch.com/
http://www.digitalthink.com/
http://www.jaworski.com/java/certification
http://www.jchq.net/
http://www.certificationguru.com/

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>