Practical record of java8 date tool class encapsulation

  • 2021-11-14 05:46:01
  • OfStack

How Directory Preface Handles Date and Time in Java 8
Re-encapsulation
New date and time package for Java8
Summarize

Preface

java should be updated to 16 now, think about when I first joined the work. Generally, they are all java7. At that time, there was no SpringBoot, no springCloud, and there was no mine. She haha, she pulled a little far. Get back to business

Today we will talk about java's date class 1 Sao operation

How to deal with date and time in Java 8

LocalDate in Java 8 is used to indicate the date of the day. Unlike java. util. Date, it only has dates and does not include time. Use this class when you only need to represent dates.


LocalDate today = LocalDate.now();
System.out.println("Today's Local date : " + today);


Today's Local date : 2021-08-27

Then, for example, let's get the specific year, month and day


LocalDate today = LocalDate.now();
int year = today.getYear();
int month = today.getMonthValue();
int day = today.getDayOfMonth();
System.out.printf("Year="+year + "month =" +month + "day ="+day);
 
Year=2021month = 8day=27

Then some friends must have asked, for example, how do we specify the formatted date in our usual work?

In the first example above, we created the date of the day very easily through the static factory method now (). You can also call another useful factory method LocalDate. of () to create any date. This method needs to pass in the year, month and day as parameters and return the corresponding LocalDate instance.


LocalDate yearAndMonthDay = LocalDate.of(2021, 08, 27);
System.out.println("yearAndMonthDay is : " + yearAndMonthDay);
 
yearAndMonthDay is : 2021-08-27

First of all, introduce these kinds, and give some common methods of self-encapsulation below

Re-encapsulation

Below, I simply wrote several tools and methods in the normal process


**
 * java 8  Date tool class reencapsulation 
 *  Use as much as possible java8 Tool class operation date of 
 * @Date 2021/6/2 4:01  Afternoon 
 * @Author yn
 */
@Component
public class LocalDateUtil {


    /**
     *  Comparative number 1 Is the date less than the 2 Date 
     * @param firstDate  No. 1 1 Date 
     * @param secondDate  No. 1 2 Date 
     * @return true- Less than ;false- Greater than 
     */
    public static boolean localDateIsBefore(LocalDate firstDate, LocalDate secondDate) {
        return firstDate.isBefore(secondDate);
    }



    /**
     *  Comparative number 1 Is the date greater than the 2 Date 
     * @param firstDate  No. 1 1 Date 
     * @param secondDate  No. 1 2 Date 
     * @return true- Greater than ;false- Not greater than 
     */
    public static boolean localDateIsAfter(LocalDate firstDate, LocalDate secondDate) {
        return firstDate.isAfter(secondDate);
    }

    /**
     *  Compare whether two dates are equal 
     * @param firstDate  No. 1 1 Date 
     * @param secondDate  No. 1 2 Date 
     * @return true- Equality ;false- Unequal 
     */
    public static boolean localDateIsEqual(LocalDate firstDate, LocalDate secondDate) {
        return firstDate.isEqual(secondDate);
    }


    /**
     *  String conversion datetime
     * @param dateTime
     * @return yyyy-MM-dd HH:mm:ss
     */
    public static Date stringCoverDateTime(String dateTime){
        LocalDateTime startDateTime =
                LocalDateTime.parse(dateTime, DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        Date LocalDateTimeToDate = Date.from(startDateTime.atZone(ZoneId.systemDefault()).toInstant());
        return LocalDateTimeToDate;
    }


    /**
     *  String conversion date
     * @param dateTime
     * @return  yyyy-MM-dd
     */
    public static Date stringCoverDate(String dateTime){
        LocalDateTime startDateTime =
                LocalDateTime.parse(dateTime, DateTimeFormatter.ofPattern("yyyy-MM-dd"));
        Date LocalDateTimeToDate = Date.from(startDateTime.atZone(ZoneId.systemDefault()).toInstant());
        return LocalDateTimeToDate;
    }
}

New date and time package for Java8

In order to make up for the deficiency of traditional Java in dealing with date and time, Java8 provides a brand-new date and time library. Java8 has specially added an java. time package, which contains the following common classes. (Dear friends, you can go under the bag to have a look)

Clock: This class is used to get the current date and time in the specified time zone. This class replaces the currentTimeMillis () method of the System class, which provides a number of methods to get the current date and time Duration: This class represents duration Instant: This class represents a specific moment, which can be accurate to nanoseconds. This class mainly provides the following methods:

1) now (): Gets the current moment.

2) now (Clock clock): Obtain the time corresponding to clock.

3) minusXxx (): Subtract a period of time from the current time.

4) plusXxx (): Add a period of time to the current time.

LocalDate: This class represents a date without a time zone, such as: 2021-08-27 LocalTime: This class represents a time without a time zone, such as 10:20:09 LocalDateTime: This class represents a date or time without a time zone, such as 2021-08-27T 10:20:09. ZonedDateTime: This class represents a time-zone date and time. ZonedId: This class represents one time zone. DayOfWeek: This class is an enumeration class that defines enumeration values from Sunday to Saturday. Month: This class is an enumeration class that defines enumeration values from January to 102 months.

OK. The date class of java8 is introduced here first

Summarize

We should learn to summarize, every iteration of java will be updated a number of useful tools for our development, we in daily development, we should learn to use and summarize a set of things to improve development efficiency


Related articles: