Date类

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.swing.JOptionPane;

public class DateUtil {
private DateUtil(){
}
public static String long2String(long d){
Date date = new Date(d);
DateFormat df = new SimpleDateFormat("yyyy年MM月dd HH:mm:ss");
String str = df.format(date);
return str;
}
/*
* 代码抽取技术:
* 首先不要想“抽出的方法”怎么写,而是把类似的代码拷在一起,观察其中的变化部分和不变化部分。
* 把这段代码中用到的“前面定义的变量”抽取成方法的参数--本例中为txtInDate和erroInfo,把“留给后面使用的”将在这段代码中新
* 创建的变量定义成方法的返回值---本例为inDate。
*/

public static long string2long(String txtInDate,String erroInfo){//要考虑参数和返回值
DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
long inDate = 0;
try {
Date d = df.parse(txtInDate);
inDate = d.getTime();
} catch (ParseException e) {
JOptionPane.showMessageDialog(null, erroInfo);
return -1;
}
return inDate;
}

}

posted @ 2016-06-21 13:53  折腾青春  阅读(100)  评论(0编辑  收藏  举报