How to deal with common time and date in Java core tutorial

  • 2021-08-21 20:26:05
  • OfStack

Detailed Explanation of Java Date Processing Class Date

Basic knowledge of time

Time zone: The whole earth is divided into 2104 time zones, and each time zone has its own local time. For the purpose of unification 1, one time of unification 1 is used, which is called global standard time (UTC, Universal Time Coordinated). TC is almost the same as Greenwich Mean Time (GMT, Greenwich, Mean, Time, also translated as: Greenwich Mean Time) CST (Beijing time), Beijing time, China standard Time, China standard time. In time zone division, it belongs to East Zone 8, which is 8 hours earlier than UTC and is recorded as UTC+8. Timestamp: The total number of seconds from January 1, 1970 (08:00: 00GMT) to the current time. It is also called Unix Timestamp (unix Timestamp). It is widely used in intellectual property protection, contract signing, financial accounting, electronic quotation and bidding, stock trading and so on There are many formats: 2050-10-3110: 11:11, 2050/10/31 10/10: 10, month, day, week, etc.

Background: How to express time in program code? What if I need to get the current time

The ava. util package provides the Date class to encapsulate the current date and time

Constructor


// Current time 
Date () 
// From 1970 Year 1 Month 1 The number of milliseconds from the day as a parameter 
Date ( long millisec ) 

Common methods


// Returns from 1970 Year 1 Month 1 Day 00 : 00 : 00GMT Since this Date Gets or sets the number of milliseconds represented by the. 
long getTime () 
// Object that calls this method Date Object returns after the specified date true Otherwise, return false . 
boolean after ( Date date ) 
// Object that calls this method Date Object returns before the specified date true Otherwise, return false . 
boolean before ( Date date ) 

Time and date of the new edition of JDK8

Java8 is one step closer to date and time processing with the release of the new Date-Time API (JSR310)

Added many common api, such as date/time comparison, addition and subtraction, formatting and so on

Package location java. time

Core class

LocalDate: Date without specific time.
LocalTime: Time without date.
LocalDateTime: Includes date and time.

LocalDate Common API


 LocalDate today=LocalDate.now (); 
 system.out.print1n ( " Today's date: "+today ); 
 
// Get the year, month, day and week 
system.out.print1n ( " What year is it now: "+today.getYear ()); 
system.out.print1n ( " What month is it now: "+today.getMonth ()); 
System.out.print1n ( " What month (number) is it now: "+today.getMonthValue ()); 
System.out.print1n ( " What's the date now: "+today.getDayofMonth ()); 
system.out.print1n ( " What day of the week is it: "+today.getDayofweek ()); 
// Add and subtract the year, and the object returned after addition is the modified one, and the old one is still the old one LocalDate changeDate=today.plusYears ( 1 ); 
system.out.print1n ( " What year is it after adding: "+changeDate.getYear ()); 
System.out.print1n ( " What year is the old one: "+today.getYear ()); 
// Date comparison 
system.out.print1n ( "isafter:"+changeDate.isAfter ( today )); 
//getYear () int  Gets the year of the current date 
//getMonth () Month Gets the month object for the current date 
//getMonthValue () int  Get the month of the current date 
//getDayofweek () Dayofweek  Indicates what day of the week the object represents 
//getDayofMonth () int  Indicates that the date represented by the object is the day of the month 
//getDayofyear () int  Indicates that the date represented by this object is the day ordinal of this year 
//withyear ( int year ) LocalDate  Modify the year of the current object 
//withMonth ( int month ) LocalDate Modify the month of the current object 
//withpayofMonth ( int dayofMonth ) LocalDate  Modify the date of the current object in the current month 
//plusYears ( long yearsToAdd ) Localpate  Increments the current object by the specified number of years 
//plusMonths ( 1ong monthsToAdd ) LocalDate  Increments the current object by the specified number of months 
//plusweeks ( 1ong weeksToAdd ) LocalDate  Increases the current object by the specified number of weeks 
//plusDays ( 1ong daysToAdd ) LocalDate  Increases the current object by a specified number of days 
//minusYears ( long yearsTosubtract ) LocalDate  Current object minus the specified number of years 
//minusMonths ( 1ong months ToSubtract ) LocalDate The current object minus the doomed number of months 
//minusWeeks ( long weeksTosubtract ) LocalDate  Subtract the specified number of weeks from the current object 
//minusDays ( 1ong daysTosubtract ) LocalDate Subtract the specified number of days from the current object 
//compareTo ( ChronoLocalDate other ) int  Compares the current object with the other The size of the object in time. If the return value is positive, the current object is later 
//isBefore ( ChronoLocalDate other ) boolean Compares whether the current object date is in the other Before the date of the object 
//isAfter ( ChronoLocalDate other ) boolean  Compares whether the current object date is in the other After the date of the object 
//isEqual ( ChronoLocalDate other ) boolean  Compare two date objects for equality 

Time and date formatting of the new version of JDK8

Why do you want time and date to be formatted

Program printing, or web page display time and date format, users have different needs, then need to be formatted according to the rules set in 1

Common placeholders

y 4-digit year M Month d Day In h m score S milliseconds

After JDK8: Date and time when thread safety was introduced


LocalDateTime ldt = LocalDateTime.now();
System.out.println(ldt);
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd
HH:mm:ss");
String ldtStr = dtf.format(ldt);
System.out.println(ldtStr);

Gets the specified date-time object LocalDate Time ldt=LocalDate Time. of (2020, 11, 11, 8, 20, 30); System. out. println (ldt);

Calculate date time difference java. time. Duration


LocalDateTime today = LocalDateTime.now();
System.out.println(today);
LocalDateTime changeDate = LocalDateTime.of(2020,10,1,10,40,30);
System.out.println(changeDate);
Duration duration = Duration.between( today,changeDate);// No. 1 ⼆2 Parameters minus the first ⼀1
 Parameters 
System.out.println(duration.toDays());// Days of time difference between two times 
System.out.println(duration.toHours());// The number of hours of two time differences 
System.out.println(duration.toMinutes());// The number of minutes of the time difference between the two 
System.out.println(duration.toMillis());// The number of milliseconds of the time difference between the two 
System.out.println(duration.toNanos());// The number of nanoseconds of two time differences 

Summarize


Related articles: