java获取当天开始时间,当周开始时间

分类: Core Java 0人评论 selfly 1年前发布

在项目中经常要获取当天开始时间,当周开始时间等,每次都不能很顺当的写出来,在这里贴一下备忘吧。

当天开始时间

Calendar currentDate = Calendar.getInstance();
currentDate.set(Calendar.HOUR_OF_DAY, 0);
currentDate.set(Calendar.MINUTE, 0);
currentDate.set(Calendar.SECOND, 0);
Date time = currentDate.getTime();

当天结束时间

Calendar currentDate = Calendar.getInstance();
currentDate.set(Calendar.HOUR_OF_DAY, 23);
currentDate.set(Calendar.MINUTE, 59);
currentDate.set(Calendar.SECOND, 59);
Date time = currentDate.getTime();

当周开始时间

Calendar currentDate = Calendar.getInstance();
currentDate.setFirstDayOfWeek(Calendar.MONDAY);
currentDate.set(Calendar.HOUR_OF_DAY, 0);
currentDate.set(Calendar.MINUTE, 0);
currentDate.set(Calendar.SECOND, 0);
currentDate.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
Date time = currentDate.getTime();

当周结束时间

Calendar currentDate = Calendar.getInstance();
currentDate.setFirstDayOfWeek(Calendar.MONDAY);
currentDate.set(Calendar.HOUR_OF_DAY, 23);
currentDate.set(Calendar.MINUTE, 59);
currentDate.set(Calendar.SECOND, 59);
currentDate.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
Date time = currentDate.getTime();
上一篇:
下一篇:

你可能感兴趣的文章

0 条评论