Java time processing

Posted by loudrake on Fri, 11 Feb 2022 16:14:07 +0100

Java time processing

1, Calendar

(1) Introduction to Calender

The Chinese translation of calendar is calendar. In fact, there are many methods of timing in history. Therefore, in order to unify timing, you must specify a calendar selection. Now the most popular and universal calendar is "Gregorian Calendar". That is, we often use "ad * * year" when talking about years. Calendar is an abstract class in Java, and Gregorian Calendar is a concrete implementation of it. Calendar. The instance obtained in getInstance () is a "GregorianCalendar" object.

(2) Simple use date

Calendar cal = Calendar.getInstance();
System.out.println(cal.get(Calendar.DATE));//-----------------------What's the date today
System.out.println(cal.get(Calendar.DAY_OF_MONTH));//---------------What day is today in January
System.out.println(cal.get(Calendar.DAY_OF_WEEK));//----------------Calculated from Sunday. If today is Tuesday, return 3
System.out.println( cal.get(Calendar.DAY_OF_YEAR));//----------------What day of the year is today
System.out.println( cal.get(Calendar.HOUR));//-----------------------What's the 12 hour clock now
System.out.println( cal.get(Calendar.HOUR_OF_DAY));//----------------What time is the 24-hour system? This attribute is generally used for assignment
System.out.println(cal.get(Calendar.MILLISECOND));//----------------Current milliseconds
System.out.println( cal.get(Calendar.MINUTE));//---------------------What time is it now
System.out.println( cal.get(Calendar.SECOND));//---------------------How many seconds is it now
System.out.println(cal.get(Calendar.WEEK_OF_MONTH));//--------------What week of the month is it now
System.out.println(cal.get(Calendar.WEEK_OF_YEAR));//----------------What week of the year is it now
System.out.println(cal.get(Calendar.MONTH));//-----------------------If + 1 is required for month acquisition, then - 1 is required for assignment

System.out.println(cal.get(Calendar.MONTH)-1);     //You can get the month of last month

(3) Conversion between,, and Date

Calendar calendar = Calendar.getInstance();
// Get the Date object from a Calendar object

Date date = calendar.getTime();

// Reflect the Date object into a Calendar object,
// Get an instance first, and then set the Date object
calendar.setTime(date);

The starting value of the month is 0 instead of 1, so to set August, we use 7 instead of 8.
calendar.set(Calendar.MONTH, 7);

Calculation 2000-01-01 What day is it

calendar.set(2000,1,1)

calendar.get(calendar.DAY_OF_WEEK )

Of course, Calender is the earliest time processing, jdk1 6 things are inevitably outdated

2, DateUtils under Apache

(1) Introduction package

<dependency>
       <groupId>org.apache.commons</groupId>
       <artifactId>commons-lang3</artifactId>
       <version>3.9</version>
</dependency>

(2) I have nothing to say. Just translate

    /**
     * The number of milliseconds in seconds
     */
    public static final long MILLIS_PER_SECOND = 1000
    /**
     *Milliseconds in minutes
     */
    public static final long MILLIS_PER_MINUTE = 60 * MILLIS_PER_SECOND
    /**
     *Milliseconds in hours
     */
    public static final long MILLIS_PER_HOUR = 60 * MILLIS_PER_MINUTE
    /**
     * Milliseconds in days
     */
    public static final long MILLIS_PER_DAY = 24 * MILLIS_PER_HOUR
    /**
     * This type is half a month, so it can represent whether the date is the first or second half of a month.
     */
    public static final int SEMI_MONTH = 1001
    /**
     * Week range, starting from Sunday.
     */
    public static final int RANGE_WEEK_SUNDAY = 1
    /**
     * Week range, starting on Monday.
     */
    public static final int RANGE_WEEK_MONDAY = 2
    /**
     * Week range, starting from the day of concern.
     */
    public static final int RANGE_WEEK_RELATIVE = 3
    /**
     * Week range, centered on the day of concern.
     */
    public static final int RANGE_WEEK_CENTER = 4
     /**
     * Month range, starting from Sunday.
     */
    public static final int RANGE_MONTH_SUNDAY = 5
    /**
     * Monthly range, starting from Monday.
     */
    public static final int RANGE_MONTH_MONDAY = 6
     /**
     * Instead of obtaining the real column in the standard composition structure, you should directly use the static methods in the class, such as dateutils parseDate(str) . 
     * This constructor is intended to be used in tools that require a JavaBean real column.  
     */
    public DateUtils()
    /**
     * Judge whether the two dates and times are the same day.
     *
     * @param date1  First date, non modifiable, non null
     * @param date2  The second date, which cannot be modified, is not null
     */
    public static boolean isSameDay(final Date date1, final Date date2)
    /**
     * Judge whether the two calendar times are the same day.   
     *
     * @param cal1  The first calendar, which cannot be modified, is not null
     * @param cal2  The second calendar, which cannot be modified, is not null
     */
    public static boolean isSameDay(final Calendar cal1, final Calendar cal2)
    /**
     * Judge whether the two dates are the same
     * This method compares the millisecond time of two objects 
     *
     * @param date1  First date, non modifiable, non null
     * @param date2  The second date, which cannot be modified, is not null
     */
    public static boolean isSameInstant(final Date date1, final Date date2)
    /**
     * Judge whether the two calendars are the same
     * This method compares the millisecond time of two objects 
     *
     * @param cal1  The first calendar, which cannot be modified, is not null
     * @param cal2  The second calendar, which cannot be modified, is not null
     */
    public static boolean isSameInstant(final Calendar cal1, final Calendar cal2)
    /**
     * Judge whether the local time of two calendars is the same
     * In addition to comparing numerical values, the types of the two calendar objects should be the same
     *
     * @param cal1  The first calendar, which cannot be modified, is not null
     * @param cal2  The second calendar, which cannot be modified, is not null
     */
    public static boolean isSameLocalTime(final Calendar cal1, final Calendar cal2)
    /**
     * Try to parse the string str representing time with various date formats in parsePatterns.
     * 
     * When parsing, the formats in parsePatterns will be used one by one. If there is no match, an exception ParseException will be thrown.
     * 
     * @param str  Parsed time string, non null
     * @param parsePatterns  The time format used to parse str is one or more, non null
     */
    public static Date parseDate(final String str, final String... parsePatterns) throws ParseException
    /**
     * Try to parse the string str representing time with various date formats in parsePatterns.
     * When parsing, the given date format character locale will be used.
     * 
     * When parsing, the formats in parsePatterns will be used one by one. If there is no match, an exception ParseException will be thrown.
     * 
     * @param str  Parsed time string, non null
     * @param locale Use the date format character in locale. If it is null, the default locale will be used
     * @param parsePatterns  The time format used to parse str is one or more, non null
     */
    public static Date parseDate(final String str, final Locale locale, final String... parsePatterns) throws ParseException
    /**
     * Try to parse the string str representing time with various date formats in parsePatterns.
     *
     * When parsing, the formats in parsePatterns will be used one by one. If there is no match, an exception ParseException will be thrown.
     * The parser parses strictly disallowed dates, such as "February 942, 1996".
     * 
     * @param str  Parsed time string, non null
     * @param parsePatterns  The time format used to parse str is one or more, non null
     */
    public static Date parseDateStrictly(final String str, final String... parsePatterns) throws ParseException
    /**
     * Try to parse the string str representing time with various date formats in parsePatterns.
     * When parsing, the given date format character locale will be used.
     *
     * When parsing, the formats in parsePatterns will be used one by one. If there is no match, an exception ParseException will be thrown.
     * The parser parses strictly disallowed dates, such as "February 942, 1996".
     * 
     * @param str  Parsed time string, non null
     * @param locale Use the date format character in locale. If it is null, the default locale will be used
     * @param parsePatterns  The time format used to parse str is one or more, non null
     */
    public static Date parseDateStrictly(final String str, final Locale locale, final String... parsePatterns) throws ParseException
    /**
     * Add amount year to date.
     *
     * @param date  Date of processing, non null
     * @param amount  The number of years to be added may be negative
     */
    public static Date addYears(final Date date, final int amount)
    /**
     * Add amount month to date.
     *
     * @param date  Date of processing, non null
     * @param amount  The number of months to add may be negative
     */
    public static Date addMonths(final Date date, final int amount)
    /**
     * Add amount week to date.
     *
     * @param date  Date of processing, non null
     * @param amount  The number of weeks to add may be negative
     */
    public static Date addWeeks(final Date date, final int amount)
    /**
     * Add amount days to date.
     *
     * @param date  Date of processing, non null
     * @param amount  The number of days to add may be negative
     */
    public static Date addDays(final Date date, final int amount)
    /**
     * Add amount hours to date.
     *
     * @param date  Date of processing, non null
     * @param amount  The number of hours to add may be negative
     */
    public static Date addHours(final Date date, final int amount)
    /**
     * Add amount minutes to date.
     *
     * @param date  Date of processing, non null
     * @param amount  The number of minutes to add may be negative
     */
    public static Date addMinutes(final Date date, final int amount)
    /**
     * Add amount seconds to date.
     *
     * @param date  Date of processing, non null
     * @param amount  The number of seconds to add may be negative
     */
    public static Date addSeconds(final Date date, final int amount)
    /**
     * Add amount milliseconds to date.
     *
     * @param date  Date of processing, non null
     * @param amount  The number of milliseconds to be added, which may be negative
     */
    public static Date addMilliseconds(final Date date, final int amount)
     /**
     * Set a new year for date data.
     *
     * @param date Date of processing, non null
     * @param amount Year to set
     */
    public static Date setYears(final Date date, final int amount)
     /**
     * Set a new month for date data.
     *
     * @param date Date of processing, non null
     * @param amount Month to set
     */
    public static Date setMonths(final Date date, final int amount)
    /**
     * Set a new day for date data.
     *
     * @param date Date of processing, non null
     * @param amount Days to set
     */
    public static Date setDays(final Date date, final int amount)
    /**
     * Set a new hour for the date data.
     *
     * @param date Date of processing, non null
     * @param amount Hours to set
     */
    public static Date setHours(final Date date, final int amount)
     /**
     * Set a new minute for the date data.
     *
     * @param date Date of processing, non null
     * @param amount Minutes to set
     */
    public static Date setMinutes(final Date date, final int amount)
    /**
     * Set a new second for the date data.
     *
     * @param date Date of processing, non null
     * @param amount Seconds to set
     */
    public static Date setSeconds(final Date date, final int amount)
    /**
     * Set a new millisecond for date data.
     *
     * @param date Date of processing, non null
     * @param amount Milliseconds to set
     */
    public static Date setMilliseconds(final Date date, final int amount)
    /**
     * Put a date in the calendar.
     */
    public static Calendar toCalendar(final Date date)
    /**
     * Round the calendar date according to the threshold field.
     *
     * For example, if your time is 28 Mar 2002 13:45:01.231,
     * If the field is HOUR, it will return 28 Mar 2002 14:00:00.000;
     * If the field is MONTH, it will return 1 April 2002 0:00:00.000.
     * 
     * @param date  Date of processing, non null
     * @param field  threshold
     */
    public static Date round(final Date date, final int field)
    /**
     * Round the calendar date according to the threshold field.
     *
     * For example, if your time is 28 Mar 2002 13:45:01.231,
     * If the field is HOUR, it will return 28 Mar 2002 14:00:00.000;
     * If the field is MONTH, it will return 1 April 2002 0:00:00.000.
     * 
     * @param date  Date of processing, non null
     * @param field  threshold  
     */
    public static Calendar round(final Calendar date, final int field)
    /**
     * Round the calendar date according to the threshold field.
     *
     * For example, if your time is 28 Mar 2002 13:45:01.231,
     * If the field is HOUR, it will return 28 Mar 2002 14:00:00.000;
     * If the field is MONTH, it will return 1 April 2002 0:00:00.000.
     * 
     * @param date  Date of processing, non null
     * @param field  threshold 
     */
    public static Date round(final Object date, final int field)
    /**
     * Intercept the date according to the threshold field.
     *
     * For example, if your time is 28 Mar 2002 13:45:01.231,
     * If the field is HOUR, it will return 28 Mar 2002 13:00:00.000;
     * If the field is MONTH, it will return 1 Mar 2002 0:00:00.000.
     * 
     * @param date  Date of processing, non null
     * @param field  threshold 
     */
    public static Date truncate(final Date date, final int field)
     /**
     * Intercept the calendar date according to the threshold field.
     *
     * For example, if your time is 28 Mar 2002 13:45:01.231,
     * If the field is HOUR, it will return 28 Mar 2002 13:00:00.000;
     * If the field is MONTH, it will return 1 Mar 2002 0:00:00.000.
     * 
     * @param date  Date of processing, non null
     * @param field  threshold
     */
    public static Calendar truncate(final Calendar date, final int field)
    /**
     * Intercept the date according to the threshold field.
     *
     * For example, if your time is 28 Mar 2002 13:45:01.231,
     * If the field is HOUR, it will return 28 Mar 2002 13:00:00.000;
     * If the field is MONTH, it will return 1 Mar 2002 0:00:00.000.
     * 
     * @param date  Date of processing, non null
     * @param field  threshold
     */
    public static Date truncate(final Object date, final int field)
    /**
     * Round up the date according to the threshold field.
     *
     * For example, if your time is 28 Mar 2002 13:45:01.231,
     * If the field is HOUR, it will return 28 Mar 2002 14:00:00.000;
     * If the field is MONTH, it will return 1 Apr 2002 0:00:00.000.
     * 
     * @param date  Date of processing, non null
     * @param field  threshold
     */
    public static Date ceiling(final Date date, final int field)
    /**
     * Round up the date according to the threshold field.
     *
     * For example, if your time is 28 Mar 2002 13:45:01.231,
     * If the field is HOUR, it will return 28 Mar 2002 14:00:00.000;
     * If the field is MONTH, it will return 1 Apr 2002 0:00:00.000.
     * 
     * @param date  Date of processing, non null
     * @param field  threshold
     */
    public static Calendar ceiling(final Calendar date, final int field)
    /**
     * Round up the date according to the threshold field.
     *
     * For example, if your time is 28 Mar 2002 13:45:01.231,
     * If the field is HOUR, it will return 28 Mar 2002 14:00:00.000;
     * If the field is MONTH, it will return 1 Apr 2002 0:00:00.000.
     * 
     * @param date  Date of processing, non null
     * @param field  threshold
     */
    public static Date ceiling(final Object date, final int field)
    /**
     * Build a time range iterator based on the specified time focus and range type rangeStyle.
     *
     * For example, the incoming time is Thursday, July 4, 2002, and the range type is RANGE_MONTH_SUNDAY,
     * The range of returned iterators is from Sunday, June 30, 2002 to Saturday, August 3, 2002
     *
     * @param focus  Specified time
     * @param rangeStyle  Range type, the value must be one of the following:
     * DateUtils.RANGE_MONTH_SUNDAY, 
     * DateUtils.RANGE_MONTH_MONDAY,
     * DateUtils.RANGE_WEEK_SUNDAY,
     * DateUtils.RANGE_WEEK_MONDAY,
     * DateUtils.RANGE_WEEK_RELATIVE,
     * DateUtils.RANGE_WEEK_CENTER
     */
    public static Iterator<Calendar> iterator(final Date focus, final int rangeStyle)
    /**
     * Build a time range iterator based on the specified time focus and range type rangeStyle.
     *
     * For example, the incoming time is Thursday, July 4, 2002, and the range type is RANGE_MONTH_SUNDAY,
     * The range of returned iterators is from Sunday, June 30, 2002 to Saturday, August 3, 2002
     *
     * @param focus  Specified time
     * @param rangeStyle  Range type, the value must be one of the following:
     * DateUtils.RANGE_MONTH_SUNDAY, 
     * DateUtils.RANGE_MONTH_MONDAY,
     * DateUtils.RANGE_WEEK_SUNDAY,
     * DateUtils.RANGE_WEEK_MONDAY,
     * DateUtils.RANGE_WEEK_RELATIVE,
     * DateUtils.RANGE_WEEK_CENTER
     */
    public static Iterator<Calendar> iterator(final Calendar focus, final int rangeStyle)
    /**
     * Build a time range iterator based on the specified time focus and range type rangeStyle.
     *
     * For example, the incoming time is Thursday, July 4, 2002, and the range type is RANGE_MONTH_SUNDAY,
     * The range of returned iterators is from Sunday, June 30, 2002 to Saturday, August 3, 2002
     *
     * @param focus  Specified time
     * @param rangeStyle  Range type, the value must be listed in the iterator(Calendar, int) method comment
     */
    public static Iterator<?> iterator(final Object focus, final int rangeStyle)
 
/**
     * Returns the number of milliseconds in a specified segment. All DateFields greater than segments will be ignored.
     *
     * Requesting milliseconds for any date will return the number of milliseconds in the current second (a number between 0 and 999 will be returned).
     * Valid segment values are: calendar YEAR,Calendar.MONTH,Calendar.DAY_OF_YEAR,
     * Calendar.DATE,Calendar.HOUR_OF_DAY,Calendar.MINUTE,
     * Calendar.SECOND And calendar MILLISECOND
     * If the segment value is less than or equal to MILLISECOND, 0 will be returned.
     * 
     *  January 1, 2008 7:15:10.538 with Calendar.SECOND as fragment will return 538
     *  January 6, 2008 7:15:10.538 with Calendar.SECOND as fragment will return 538
     *  January 6, 2008 7:15:10.538 with Calendar.MINUTE as fragment will return 10538
     *  January 16, 2008 7:15:10.538 with Calendar.MILLISECOND as fragment will return 0
     *   (a millisecond cannot be split in milliseconds)
     * 
     * @param calendar Get the calendar object, non null
     * @param fragment Segment value
     */
  public static long getFragmentInMilliseconds(final Calendar calendar, final int fragment)
    /**
     * Returns the number of seconds within the specified segment. All DateFields greater than segments will be ignored.
     *
     * Requesting any date second will return the number of seconds of the current minute (a number between 0 and 59).
     * Valid segment values are: calendar YEAR,Calendar.MONTH,Calendar.DAY_OF_YEAR,
     * Calendar.DATE,Calendar.HOUR_OF_DAY,Calendar.MINUTE,
     * Calendar.SECOND And calendar MILLISECOND
     * If the segment value is less than or equal to SECOND, 0 will be returned.
     * 
     *  January 1, 2008 7:15:10.538 with Calendar.MINUTE as fragment will return 10
     *  January 6, 2008 7:15:10.538 with Calendar.MINUTE as fragment will return 10
     *  January 6, 2008 7:15:10.538 with Calendar.DAY_OF_YEAR as fragment will return 26110
     *   (7*3600 + 15*60 + 10)</li>
     *  January 16, 2008 7:15:10.538 with Calendar.MILLISECOND as fragment will return 0
     * 
     * @param calendar Get the calendar object, non null
     * @param fragment Segment value
     */
    public static long getFragmentInSeconds(final Calendar calendar, final int fragment)
    /**
     * Returns the number of minutes in a specified segment. All DateFields greater than segments will be ignored.
     *
     * Requesting any date minute will return the number of minutes of the current hour (a number between 0 and 59)
     * Valid segment values are: calendar YEAR,Calendar.MONTH,Calendar.DAY_OF_YEAR,
     * Calendar.DATE,Calendar.HOUR_OF_DAY,Calendar.MINUTE,
     * Calendar.SECOND And calendar MILLISECOND
     * If the segment value is less than or equal to MINUTE, 0 will be returned.
     * 
     *  January 1, 2008 7:15:10.538 with Calendar.HOUR_OF_DAY as fragment will return 15
     *  January 6, 2008 7:15:10.538 with Calendar.HOUR_OF_DAY as fragment will return 15
     *  January 1, 2008 7:15:10.538 with Calendar.MONTH as fragment will return 15
     *  January 6, 2008 7:15:10.538 with Calendar.MONTH as fragment will return 435 (7*60 + 15)
     *  January 16, 2008 7:15:10.538 with Calendar.MILLISECOND as fragment will return 0
     * 
     * @param calendar Get the calendar object, non null
     * @param fragment Segment value
     */
    public static long getFragmentInMinutes(final Calendar calendar, final int fragment)
    /**
     * Returns the number of hours in a specified segment. All DateFields greater than segments will be ignored.
     *
     * Requesting any date hour will return the number of hours of the current day (a number between 0 and 23).
     * Valid segment values are: calendar YEAR,Calendar.MONTH,Calendar.DAY_OF_YEAR,
     * Calendar.DATE,Calendar.HOUR_OF_DAY,Calendar.MINUTE,
     * Calendar.SECOND And calendar MILLISECOND
     * Segment value is less than or equal to HOUR_OF_DAY, 0 will be returned.
     *  
     *  January 1, 2008 7:15:10.538 with Calendar.DAY_OF_YEAR as fragment will return 7
     *  January 6, 2008 7:15:10.538 with Calendar.DAY_OF_YEAR as fragment will return 7
     *  January 1, 2008 7:15:10.538 with Calendar.MONTH as fragment will return 7
     *  January 6, 2008 7:15:10.538 with Calendar.MONTH as fragment will return 127 (5*24 + 7)
     *  January 16, 2008 7:15:10.538 with Calendar.MILLISECOND as fragment will return 0
     *  
     * @param calendar Get the calendar object, non null
     * @param fragment Segment value
     */
    public static long getFragmentInHours(final Calendar calendar, final int fragment)
    /**
     * Returns the number of days in a specified segment. All DateFields greater than segments will be ignored.
     *
     * If you request any number of days, you will return the number of days of the current month (a number between 1 and 31).
     * Valid segment values are: calendar YEAR,Calendar.MONTH,Calendar.DAY_OF_YEAR,
     * Calendar.DATE,Calendar.HOUR_OF_DAY,Calendar.MINUTE,
     * Calendar.SECOND And calendar MILLISECOND
     * If the segment value is less than or equal to DATE, 0 will be returned.
     * 
     *  January 28, 2008 with Calendar.MONTH as fragment will return 28
     *  February 28, 2008 with Calendar.MONTH as fragment will return 28
     *  January 28, 2008 with Calendar.YEAR as fragment will return 28
     *  February 28, 2008 with Calendar.YEAR as fragment will return 59
     *  January 28, 2008 with Calendar.MILLISECOND as fragment will return 0
     * 
     * @param calendar Get the calendar object, non null
     * @param fragment Segment value
     */
    public static long getFragmentInDays(final Calendar calendar, final int fragment)
    /**
     * Intercept and compare whether the value at the field of two calendar objects is the same.
     * 
     * @param cal1 First calendar object, non null
     * @param cal2 Second calendar object, non null
     * @param field Calendar Threshold in
     */
    public static boolean truncatedEquals(final Calendar cal1, final Calendar cal2, final int field)
    /**
     * Intercept and compare whether the value at the field of two date objects is the same.
     * 
     * @param date1 First date object, non null
     * @param date2 Second date object, non null
     * @param field Calendar Threshold in
     */
    public static boolean truncatedEquals(final Date date1, final Date date2, final int field)
    /**
     * Intercepts and compares the value at the field of two calendar objects.
     * If the first calendar is less than, equal to or greater than the second, negative integers, 0 and positive integers will be returned accordingly
     * 
     * @param cal1 First calendar object, non null
     * @param cal2 Second calendar object, non null
     * @param field Calendar Threshold in
     */
    public static int truncatedCompareTo(final Calendar cal1, final Calendar cal2, final int field)
    /**
     * Intercept and compare the value at the field of two date objects.
     * If the first date is less than, equal to, or greater than the second, negative integers, 0, and positive integers will be returned accordingly
     * 
     * @param date1 First date object, non null
     * @param date2 Second date object, non null
     * @param field Calendar Threshold in
     */
    public static int truncatedCompareTo(final Date date1, final Date date2, final int field)
	
	
	//Simple example
	System.out.println(DateUtils.addDays(new Date(), 1));//-----------------Current date. If you want to add 1 to the number of days, it is recommended to fill in a negative number
    System.out.println(DateUtils.addHours(new Date(), 0));//----------------Current date
    Date date = DateUtils.addDays(new Date(), 1);

DateUtils returns Date without conversion. Of course, it is a very early thing, but it was born on the basis of Calender

3, Jdk1 8 new date API LocalDate | LocalTime | LocalDateTime

The new date API s are immutable and are more suitable for multi-threaded environments (get the current date and time from the system clock in the default time zone. Do not consider the time zone difference)
advantage:

1. Previously used java util. Date month starts from 0. We usually use + 1, which is very inconvenient. Java time. The month and week of localdate have been changed to enum

2,java. util. Neither date nor SimpleDateFormat is thread safe. Like the most basic String, LocalDate and LocalTime are immutable types. They are thread safe and cannot be modified.

3,java.util.Date is a "universal interface", which includes date, time and milliseconds, making it more clear whether to choose or not

4. The reason why the new interface works better is that the operation of date and time is taken into account. It is often pushed forward or backward for a few days. Use Java util. Date and Calendar need to write a lot of code, and ordinary developers may not be able to write it correctly.

(1) , LocalDateTime (mm / DD / yyyy, H / min / s)

LocalDateTime date = LocalDateTime.now();
        //2021-03-26T10:24:14.009784900
        System.out.println(date);

		//Operation of obtaining month, day, hour, minute and second
        System.out.println(date.getYear());
        System.out.println(date.getMonthValue());
        System.out.println(date.getDayOfMonth());
        System.out.println(date.getHour());
        System.out.println(date.getMinute());
        System.out.println(date.getSecond());
        System.out.println(date.getNano());
		
		//Of course, you can also get any recent period of time
		 System.out.println(date.getSecond()+1);           ||        System.out.println(date.getSecond()-1);
		

        // Manually create a LocalDateTime instance
        LocalDateTime date2 = LocalDateTime.of(2017, 12, 17, 9, 31, 31, 31);
        System.out.println(date2);
        // Add to get a new date instance
        LocalDateTime date3 = date2.plusDays(12);
        System.out.println(date3);
        // Subtract to get a new date instance
        LocalDateTime date4 = date3.minusYears(2);
        System.out.println(date4);
		
		isAfter()Judge whether a date is returned after the specified date boolean type
		System.out.println(localDateTime.isBefore(LocalDateTime.now()));//---------Return false
		
		isBefore()Judge whether a date is returned before the specified date boolean type
		System.out.println(localDateTime.isAfter(LocalDateTime.now()));//---------Return false
	
		isLeapYear()Determine whether it is a leap year(This is LocalDate Methods in classes)  return boolean type
		System.out.println(localDateTime.isEqual(LocalDateTime.now()));//---------Return true

(2),ToDate

Date to LocalDateTime

Date todayDate = new Date();

LocalDateTime ldt = Instant.ofEpochMilli( todayDate.getTime() )
        .atZone( ZoneId.systemDefault() )
        .toLocalDateTime();

System.out.println(ldt);

LocalDateTime to Date

LocalDateTime localDateTime = LocalDateTime.now();
LocalDateTime localDate = LocalDateTime.now();
LocalDateTime localTime = LocalDateTime.now();

Date date = Date.from( localDateTime.atZone( ZoneId.systemDefault()).toInstant());
Date date2 = Date.from(localDate.atStartOfDay(ZoneId.systemDefault()).toInstant());
Date date3 = Date.from(localTime.atDate(LocalDate.now()).atZone(ZoneId.systemDefault()).toInstant());

System.out.println(date);

Of course, there are many interesting things. There is a big difference between localdate, Localtime and localdatetime, so just one

4, Hutool (Java tool class library)

A more fun Java tool class library, including many fun tool classes, simple and convenient.

(1) . installation

Maven

<dependency>
    <groupId>cn.hutool</groupId>
    <artifactId>hutool-all</artifactId>
    <version>5.6.1</version>
</dependency>

Gradle

compile 'cn.hutool:hutool-all:5.6.1'

Maven central warehouse
Hutool 5.x supports JDK8 +, has no test on Android platform, and cannot guarantee the availability of all tool classes or tool methods. If your project uses JDK7, please use hutool 4 X version

(2) , use

	//The time of this tool class is formatted
	System.out.println(DateUtil.now());//------------------------------Take the current time
	
	There's nothing to say about it. Just download the source code and look at the Chinese notes. Don't give examples one by one. It's simple.

summary

In short, there are a lot of interesting tools, whether they are JDK native or encapsulated by others. If you want, you can also use jdk1 8's date api encapsulates its own date tools. There are too many Date tools on the Internet. Don't build wheels again ~ ~ ~ ~ ~ ~ ~ ~ today is also a full day

Topics: Java Back-end