Java时间获取

// java时间获取类

public class DateUtil {


  // 获取当前时间
  public static Date getNowTime() {
    Calendar calendar = Calendar.getInstance();
    return calendar.getTime();
  }

  // 获得当天0点时间
  public static Date getTodayStartTime() {
    Calendar cal = Calendar.getInstance();
    cal.set(Calendar.HOUR_OF_DAY, 0);
    cal.set(Calendar.SECOND, 0);
    cal.set(Calendar.MINUTE, 0);
    cal.set(Calendar.MILLISECOND, 0);
    return cal.getTime();
  }

  // 获得明天0点时间
  public static Date getTomorrowStartTime() {
    Calendar cal = Calendar.getInstance();
    cal.add(Calendar.DATE, 1);
    cal.set(Calendar.HOUR_OF_DAY, 0);
    cal.set(Calendar.SECOND, 0);
    cal.set(Calendar.MINUTE, 0);
    cal.set(Calendar.MILLISECOND, 0);
    return cal.getTime();
  }

  //获取某天的结束时间
  public static Date getDayLastTime(int year, int month, int day) {
    Calendar cal = Calendar.getInstance();
    cal.set(Calendar.YEAR, year);
    cal.set(Calendar.MONTH, month-1);
    cal.set(Calendar.DAY_OF_MONTH, day);
    cal.set(Calendar.HOUR_OF_DAY, cal.getActualMaximum(Calendar.HOUR_OF_DAY));
    cal.set(Calendar.SECOND, cal.getActualMaximum(Calendar.SECOND));
    cal.set(Calendar.MINUTE, cal.getActualMaximum(Calendar.MINUTE));
    cal.set(Calendar.MILLISECOND, cal.getActualMaximum(Calendar.MILLISECOND));
    return cal.getTime();
  }

  // 获得本月第一天0点时间
  public static Date getMonthStartTime() {
    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();
  }

  // 获得本月最后一天的最后时间
  public static Date getMonthLastTime() {
    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, cal.getActualMaximum(Calendar.HOUR_OF_DAY));
    cal.set(Calendar.SECOND, cal.getActualMaximum(Calendar.SECOND));
    cal.set(Calendar.MINUTE, cal.getActualMaximum(Calendar.MINUTE));
    cal.set(Calendar.MILLISECOND, cal.getActualMaximum(Calendar.MILLISECOND));
    return cal.getTime();
  }

  //获取指定年月的第一天
  public static Date getFirstDayOfMonth(int year, int month) {
    Calendar cal = Calendar.getInstance();
    cal.set(Calendar.YEAR, year);
    cal.set(Calendar.MONTH, month-1);
    //获取某月最小天数
    int firstDay = cal.getMinimum(Calendar.DATE);
    //设置日历中月份的最小天数
    cal.set(Calendar.DAY_OF_MONTH, firstDay);
    cal.set(Calendar.HOUR_OF_DAY, 0);
    cal.set(Calendar.SECOND, 0);
    cal.set(Calendar.MINUTE, 0);
    cal.set(Calendar.MILLISECOND, 0);
    return cal.getTime();
  }

  //获取指定年月的最后一天
  public static Date getLastDayOfMonth(int year, int month) {
    Calendar cal = Calendar.getInstance();
    cal.set(Calendar.YEAR, year);
    cal.set(Calendar.MONTH, month-1);
    //获取某月最大天数
    int lastDay = cal.getActualMaximum(Calendar.DATE);
    //设置日历中月份的最大天数
    cal.set(Calendar.DAY_OF_MONTH, lastDay);
    cal.set(Calendar.HOUR_OF_DAY, 0);
    cal.set(Calendar.SECOND, 0);
    cal.set(Calendar.MINUTE, 0);
    cal.set(Calendar.MILLISECOND, 0);
    return cal.getTime();
  }

  // 判断是否为周末
  public static boolean isWeekend(String date) throws ParseException {
    DateFormat format1 = new SimpleDateFormat("yyyy-MM-dd");
    Date date1 = format1.parse(date);
    Calendar cal = Calendar.getInstance();
    cal.setTime(date1);
    if((cal.get(Calendar.DAY_OF_WEEK) == Calendar.SATURDAY) || (cal.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY)) {
      return true;
    } else {
      return false;
    }
  }

}

// 测试类

public class DateTest {

  public static void main(String[] args) throws Exception {
    SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    System.out.println("获取当前时间:" + format.format(DateUtil.getNowTime()));
    System.out.println("获得当天0点时间:" + format.format(DateUtil.getTodayStartTime()));
    System.out.println("获得明天0点时间:" + format.format(DateUtil.getTomorrowStartTime()));
    System.out.println("获取某天的结束时间:" + format.format(DateUtil.getDayLastTime(2020, 1, 25)));
    System.out.println("获得本月第一天0点时间:" + format.format(DateUtil.getMonthStartTime()));
    System.out.println("获得本月最后一天的最后时间:" + format.format(DateUtil.getMonthLastTime()));
    System.out.println("获取指定年月的第一天:" + format.format(DateUtil.getFirstDayOfMonth(2020, 2)));
    System.out.println("获取指定年月的最后一天:" + format.format(DateUtil.getLastDayOfMonth(2020, 2)));
    System.out.println("判断是否为周末:" + DateUtil.isWeekend(format.format(DateUtil.getNowTime())));
  }

}

posted @ 2020-01-19 02:27  黎明的星海  阅读(225)  评论(0编辑  收藏  举报