JAVA常用时间操作类

时间:2025-01-29 20:32:56

http://www.360doc.com/content/10/1210/17/2703996_76839640.shtml

   在程序里面要获取当前的开始时间和结束时间,以及当前天所在的周的开始时间和结束时间,在这个地方记录一下。

   当天开始时间:

Calendar currentDate = new GregorianCalendar();

currentDate.set(Calendar.HOUR_OF_DAY, 0);
currentDate.set(Calendar.MINUTE, 0);
currentDate.set(Calendar.SECOND, 0);
dateCursor.setTodayStart((Date)currentDate.getTime().clone());

当天结束时间:

Calendar currentDate = new GregorianCalendar();

currentDate.set(Calendar.HOUR_OF_DAY, 23);
currentDate.set(Calendar.MINUTE, 59);
currentDate.set(Calendar.SECOND, 59);
dateCursor.setTodayEnd((Date)currentDate.getTime().clone());

当周开始时间:

Calendar currentDate = new GregorianCalendar();
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);
dateCursor.setWeekStart((Date)currentDate.getTime().clone());

 当周结束时间:

Calendar currentDate = new GregorianCalendar();
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);
dateCursor.setWeekEnd((Date)currentDate.getTime().clone());