public static SimpleDateFormat sdf() {
return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
}
//获取当天的开始时间
public static String getDayBegin() {
Calendar cal = new GregorianCalendar();
cal.set(Calendar.HOUR_OF_DAY, 0);
cal.set(Calendar.MINUTE, 0);
cal.set(Calendar.SECOND, 0);
cal.set(Calendar.MILLISECOND, 0);
}
public static void main(String[] args) {
System.out.println("今天0点时间:" + getDayBegin());
System.out.println("今天24点时间:" + getDayEnd());
}
//获取当天的结束时间
public static String getDayEnd() {
Calendar cal = new GregorianCalendar();
cal.set(Calendar.HOUR_OF_DAY, 23);
cal.set(Calendar.MINUTE, 59);
cal.set(Calendar.SECOND, 59);
return sdf().format(cal.getTime());
}
//获取昨天的开始时间
public static String getBeginDayOfYesterday() {
Calendar cal = new GregorianCalendar();
cal.set(Calendar.HOUR_OF_DAY, 0);
cal.set(Calendar.MINUTE, 0);
cal.set(Calendar.SECOND, 0);
cal.set(Calendar.MILLISECOND, 0);
cal.add(Calendar.DAY_OF_MONTH, -1);
java获取当天、昨天、本周、上周、本月、上月开始时间和结束时间
最新推荐文章于 2024-06-30 03:12:10 发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)