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

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

当天开始时间

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();
本文标题:java获取当天开始时间,当周开始时间
本文来自码农的士,转载请注明出处
交流QQ群:32261424
Previous:
Next:

发表评论

,将以游客形式发表

网友最新评论(1)

  1. 1楼 106.39.75.*** [游客] 发表于 2016-11-17 13:01:41 回复此评论
    貌似会有夏令时的问题