java 计算时间

 

 @Test public void testDate() { Calendar calendar = Calendar.getInstance(); // 获取前一年 calendar.setTime(new Date()); calendar.add(Calendar.YEAR, -1); Date frontYearDate = calendar.getTime(); System.out.println(frontYearDate); // 获取前一个月 calendar.setTime(new Date()); calendar.add(Calendar.MONTH, -1); // 得到前一个月 Date frontMonthDate = calendar.getTime(); System.out.println(frontMonthDate); // 获取前一个星期 calendar.setTime(new Date()); calendar.add(Calendar.WEDNESDAY, -1); Date frontWeekDate = calendar.getTime(); System.out.println(frontWeekDate); // 获取前一天 calendar.setTime(new Date()); calendar.add(Calendar.DATE, -1); Date frontDayDate = calendar.getTime(); System.out.println(frontDayDate); // 设置并获取前一天的某个时间 ,比如是凌晨 calendar.set(Calendar.HOUR_OF_DAY, 0); calendar.set(Calendar.MINUTE, 0); calendar.set(Calendar.SECOND, 0); calendar.set(Calendar.MILLISECOND, 0); Date frontDay0Date = calendar.getTime(); System.out.println(frontDay0Date); }

你可能感兴趣的:(java,Date,calendar)