1. 代码
private static final String DATE_FORMAT = "yyyy-MM-dd HH:mm:ss"; private static Date getStartTime() { Calendar todayStart = Calendar.getInstance(); todayStart.set(Calendar.HOUR_OF_DAY, 0); todayStart.set(Calendar.MINUTE, 0); todayStart.set(Calendar.SECOND, 0); todayStart.set(Calendar.MILLISECOND, 0); return todayStart.getTime(); } private static Date getEndTime() { Calendar todayEnd = Calendar.getInstance(); todayEnd.set(Calendar.HOUR_OF_DAY, 23); todayEnd.set(Calendar.MINUTE, 59); todayEnd.set(Calendar.SECOND, 59); todayEnd.set(Calendar.MILLISECOND, 999); return todayEnd.getTime(); }
public static String getStartTimeStr(){ return new SimpleDateFormat(DATE_FORMAT).format(getStartTime()); } public static String getEndTimeStr(){ return new SimpleDateFormat(DATE_FORMAT).format(getEndTime()); }
2. 其他的joda框架好像也可以方便get到当日起止时间戳
java自带日期也好使, Calendar.HOUR_OF_DAY要搞对,不然出现过了12点,结果从今日12时到明日12时