// 日期返回毫秒的方法
public static long getMillis(java.util.Date date) {
java.util.Calendar c = java.util.Calendar.getInstance();
c.setTime(date);
return c.getTimeInMillis();
}
//毫秒返回日期字符串方法
public static void main(String[] args) {
Calendar calendar = Calendar.getInstance();
String str = String.valueOf(calendar.getTimeInMillis());
String time1 = new StringUtil().getDateTimeByMillisecond(str);
String time2 = new StringUtil().getDateTimeByMillisecond("1272337252796");
System.out.println(time1 + "/n" + time2);
}
public String getDateTimeByMillisecond(String str) {
Date date = new Date(Long.valueOf(str));
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
String time = format.format(date);
return time;
}
//日期相减
public static int diffDate(java.util.Date date, java.util.Date date1) {
return (int) ((getMillis(date) - getMillis(date1)) / (24 * 3600 * 1000));
}
//日期格式转换
// 2010-9-10T00:00:00+86->2010-9-10
public String convertDate(String olddate) {
if (olddate != null && olddate.length() > 0 && olddate.indexOf("T") > 0) {
return olddate.substring(0, olddate.indexOf("T"));
} else if (olddate != null && olddate.length() > 0
&& olddate.indexOf(" ") > 0) {
return olddate.substring(0, olddate.indexOf(" "));
} else {
return "";
}
}
//日期字符转日期型
public Date convertStringToDate(String olddate) throws ParseException {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
//SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//return sdf.parse("2008-08-08 12:10:12");
return sdf.parse(this.convertDate(olddate));
}