java获取当前月和周的第一天和最后一天

冷不防 2022-01-06 03:43 936阅读 0赞

获取当前周的第一天:

  1. public static Date getFirstDayOfWeek(Date date) {
  2. Calendar cal = Calendar.getInstance();
  3. try {
  4. cal.setTime(date);
  5. cal.set(Calendar.DAY_OF_WEEK, 2);
  6. } catch (Exception e) {
  7. e.printStackTrace();
  8. }
  9. return cal.getTime();
  10. }

获取当前周最后一天:

  1. public static Date getLastDayOfWeek(Date date) {
  2. Calendar cal = Calendar.getInstance();
  3. try {
  4. cal.setTime(date);
  5. cal.set(Calendar.DAY_OF_WEEK, 2);
  6. cal.set(Calendar.DATE, cal.get(Calendar.DATE) + 6);
  7. } catch(Exception e) {
  8. e.printStackTrace();
  9. }
  10. return cal.getTime();
  11. }

获取当前月第一天:

  1. public static String getCurrentMonthFirstDay() {
  2. SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
  3. Calendar cale = Calendar.getInstance();
  4. cale = Calendar.getInstance();
  5. cale.add(Calendar.MONTH, 0);
  6. cale.set(Calendar.DAY_OF_MONTH, 1);
  7. return format.format(cale.getTime());
  8. }

获取当前月最后一天:

  1. public static String getCurrentMonthLastDay() {
  2. SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
  3. Calendar cale = Calendar.getInstance();
  4. cale = Calendar.getInstance();
  5. cale.add(Calendar.MONTH, 1);
  6. cale.set(Calendar.DAY_OF_MONTH, 0);
  7. return format.format(cale.getTime());
  8. }

发表评论

表情:
评论列表 (有 0 条评论,936人围观)

还没有评论,来说两句吧...

相关阅读