很多朋友都想知道java获取本周日期?下面就一起来了解一下吧~
//获得本周一日期 Date timesWeekmorning = getTimesWeekmorning(); String format1 = format.format(timesWeekmorning);
//获得本周日日期 Date timesWeeknight = getTimesWeeknight(); String format2 =format.format(timesWeeknight);
// 获得本月第一天日期 Date timesMonthmorning = getTimesMonthmorning(); String format3 =format.format(timesMonthmorning);
// 获得本月最后一天日期 Date timesMonthnight = getTimesMonthnight(); String format4 =format.format(timesMonthnight);
// 获得本周一0点时间 public static Date getTimesWeekmorning() { Calendar cal = Calendar.getInstance(); cal.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONDAY), cal.get(Calendar.DAY_OF_MONTH), 0, 0, 0); cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY); return cal.getTime(); }
// 获得本周日24点时间 public static Date getTimesWeeknight() { Calendar cal = Calendar.getInstance(); cal.setTime(getTimesWeekmorning()); cal.add(Calendar.DAY_OF_WEEK, 7); return cal.getTime(); }
// 获得本月第一天0点时间 public static Date getTimesMonthmorning() { Calendar cal = Calendar.getInstance(); cal.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONDAY), cal.get(Calendar.DAY_OF_MONTH), 0, 0, 0); cal.set(Calendar.DAY_OF_MONTH, cal.getActualMinimum(Calendar.DAY_OF_MONTH)); return cal.getTime(); }
// 获得本月最后一天24点时间 public static Date getTimesMonthnight() { Calendar cal = Calendar.getInstance(); cal.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONDAY), cal.get(Calendar.DAY_OF_MONTH), 0, 0, 0); cal.set(Calendar.DAY_OF_MONTH, cal.getActualMaximum(Calendar.DAY_OF_MONTH)); cal.set(Calendar.HOUR_OF_DAY, 24); return cal.getTime(); }
以上就是小编今天的分享,希望能够帮到大家。