Hutool implements date and time acquisition, conversion, and formatting

Table of contents

Get current time

Format date output

Get Date object

start and end time

Date time offset

date time difference

Get current date and time

Use object to get year, month and day

DateTime to string


      Java itself has limited support for date and time, and the coexistence of Date and Calendar objects causes confusion and complexity in the use of various methods, so this tool class is used to encapsulate it. The encapsulation is mainly to convert between dates and strings, and to provide date positioning.

        For the Date object, for convenience, a DateTime class is used instead, which is inherited from the Date object. The main convenience is that it overrides the toString() method and returns a string in the form of yyyy-MM-dd HH:mm:ss, which is convenient Called during output, it provides many convenient methods to operate on date objects.

Get current time

/**
     * 获取当前时间
     */
    @Test
    public void DateUtil(){
      //当前时间
      Date date = DateUtil.date();
      System.out.println(date);
      //当前时间
      Date date2 = DateUtil.date(Calendar.getInstance());
      System.out.println(date2);
      //当前时间
      Date date3 = DateUtil.date(System.currentTimeMillis());
      System.out.println(date3);
      //当前时间字符串,格式:yyyy-MM-dd HH:mm:ss
      String now = DateUtil.now();
      System.out.println(now);
      //当前日期字符串,格式:yyyy-MM-dd
      String today= DateUtil.today();
      System.out.println(today);
  }

Format date output

/**
     * 格式化日期输出
     */
    @Test
    public void DateParse(){
        String dateStr = "2022-04-19";

        //2022-04-19 00:00:00
        Date date = DateUtil.parse(dateStr);
        System.out.println(date);

        //2022/04/19
        String format = DateUtil.format(date, "yyyy/MM/dd");
        System.out.println(format);

        //2022-04-19
        String formatDate = DateUtil.formatDate(date);
        System.out.println(formatDate);

        //2022-04-19 00:00:00
        String formatDateTime = DateUtil.formatDateTime(date);
        System.out.println(formatDateTime);

        //00:00:00
        String formatTime = DateUtil.formatTime(date);
        System.out.println(formatTime);
    }

Get Date object

/**
     * 获取Date对象的某个部分
     */
    @Test
    public void DatePart(){
        Date date = DateUtil.date();
        System.out.println(DateUtil.year(date));
        //3 + 1
        System.out.println(DateUtil.month(date));
        //APRIL 4月
        System.out.println(DateUtil.monthEnum(date));
    }

start and end time

/**
     * 开始和结束时间
     */
    @Test
    public void beginOfDay(){
        String dateStr = "2022-03-01 22:33:23";
        Date date = DateUtil.parse(dateStr);

        //一天的开始,结果:2022-03-01 00:00:00
        Date beginOfDay = DateUtil.beginOfDay(date);
        System.out.println(beginOfDay);

        //一天的结束,结果:2022-03-01 23:59:59
        Date endOfDay = DateUtil.endOfDay(date);
        System.out.println(endOfDay);
    }

Date time offset

/**
     * 日期时间偏移
     * //昨天
     * DateUtil.yesterday()
     * //明天
     * DateUtil.tomorrow()
     * //上周
     * DateUtil.lastWeek()
     * //下周
     * DateUtil.nextWeek()
     * //上个月
     * DateUtil.lastMonth()
     * //下个月
     * DateUtil.nextMonth()
     */
    @Test
    public void offset(){
        String dateStr = "2022-03-01 22:33:23";

        //2022-03-01 22:33:23
        Date date = DateUtil.parse(dateStr);
        System.out.println(date);

        //2022-03-03 22:33:23
        Date newDate = DateUtil.offset(date, DateField.DAY_OF_MONTH, 2);
        System.out.println(newDate);

        //2022-03-04 22:33:23
        DateTime newDate2 = DateUtil.offsetDay(date, 3);
        System.out.println(newDate2);

        //2022-03-01 19:33:23
        DateTime newDate3 = DateUtil.offsetHour(date, -3);
        System.out.println(newDate3);
    }

date time difference

/**
     * 日期时间差
     * 有时候我们需要计算两个日期之间的时间差(相差天数、相差小时数等等),Hutool将此类方法封装为between方法
     */
    @Test
    public void between(){
        String dateStr1 = "2022-03-01 22:33:23";
        Date date1 = DateUtil.parse(dateStr1);

        String dateStr2 = "2022-04-01 23:33:23";
        Date date2 = DateUtil.parse(dateStr2);

        //相差一个月,31天
        long betweenDay = DateUtil.between(date1, date2, DateUnit.DAY);
        System.out.println(betweenDay);
    }

Get current date and time

/**
     * 获取当前日期时间
     */
    @Test
    public void DateTime(){
        Date date = new Date();
        //new方式创建
        DateTime time = new DateTime(date);
        System.out.println(time);
        //of方式创建
        DateTime now = DateTime.now();
        DateTime dt = DateTime.of(date);
        System.out.println(now);
        System.out.println(dt);
    }

Use object to get year, month and day

/**
     * 使用对象
     */
    @Test
    public void DateTimeObject(){
        DateTime dateTime = new DateTime("2022-04-18 12:34:23", DatePattern.NORM_DATETIME_FORMAT);
        int year = dateTime.year();
        System.out.println(year);
        int month = dateTime.month()+1;
        System.out.println(month);
        int day = dateTime.dayOfMonth();
        System.out.println(day);
    }

DateTime to string

    @Test
    public void dateStr(){
        DateTime dateTime = new DateTime("2022-04-18 12:34:23", DatePattern.NORM_DATETIME_FORMAT);
        String dateStr = dateTime.toString();
        System.out.println(dateStr);
    }

Guess you like

Origin blog.csdn.net/XikYu/article/details/132023250