信手拈来 妙手偶得 » 日志 » [原创]时间计算实用类
[原创]时间计算实用类
Junglesong 发表于 2008-06-04 11:48:20
import java.text.Format;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
public class TimeUtil {
private TimeUtil() {
}
public static String getCurrDateTime() {
Date date = new Date();
Format formatter = new SimpleDateFormat("yyyy.MM.dd.HH.mm.ss");
return formatter.format(date);
}
public static String getCurrDate() {
Date date = new Date();
Format formatter = new SimpleDateFormat("yyyy.MM.dd");
return formatter.format(date);
}
public static String getSimpleCurrDate() {
Date date = new Date();
Format formatter = new SimpleDateFormat("yyyy.M.d");
return formatter.format(date);
}
public static String getCurrTime() {
Date date = new Date();
Format formatter = new SimpleDateFormat(".HH.mm.ss");
return formatter.format(date);
}
public static String getCurrMonth() {
Date date = new Date();
Format formatter = new SimpleDateFormat("yyyy.M");
return formatter.format(date);
}
public static Date getDate(String strMonth) {
SimpleDateFormat myFormatter = new SimpleDateFormat("yyyy.MM.dd");
try {
java.util.Date date = myFormatter.parse(strMonth);
return date;
} catch (Exception ex) {
return null;
}
}
public static long getDaysBetween(String startDate,String endDate){
Date dStart=getDate(startDate);
Date dEnd=getDate(endDate);
return (dEnd.getTime()-dStart.getTime())/(24 * 60 * 60 * 1000);
}
public static void main(String[] args){
System.out.println(getDaysBetween("2008.06.05","2008.06.08"));
System.out.println(getDaysBetween("2008.05.01","2008.06.01"));
System.out.println(getDaysBetween("2007.01.01","2008.01.01"));
}
}
