This is the fourth day of my participation in the More text Challenge. For details, see more text Challenge

Common time classes are Date,Calendar, LocalDate, SimpleDateFormat, timestamp, and so on.

Date class and its methods

When using new Date(), as shown in the figure, the sun.util package is ignored, and the Date is created in two ways: the util package and the SQL package. Using the IDEA editor, you can use the shortcut key CTRL + H to see the class relationships, as shown below. The SQL. Date class inherits from the util package.

1) Created using java.util

Dates are primarily created using no-argument constructs and long types; the rest are obsolete.

Date date = new Date();
// Receive an argument of type long, the number of milliseconds from 1970-01-01 00:00:00.000 to the current time
Date date2 = new Date(1574673617000L);
System.out.println(date);
System.out.println(date2);
Copy the code
// The default time format is as follows
Mon Nov 25 17:20:59 CST 2019
Mon Nov 25 17:20:17 CST 2019
Copy the code

Date main methods

  • compareTo
// Use the date and date2 created above
System.out.println(date.compareTo(date2));
System.out.println(date2.compareTo(date));
System.out.println(date2.compareTo(new Date(1574673617000L)));
Copy the code
// The following is displayed
// Compare the date you want to compare with the date in brackets
1  // The date in brackets is smaller than the previous date
-1 // The date in brackets is larger than the previous date
0  // The two dates are equal
Copy the code
  • clone
Object clone = date.clone();
Copy the code
  • getTime
// Get the number of milliseconds
long time = date.getTime();
System.out.println(date.getTime());
Copy the code
  • after,before
// Before and after the interpretation time
boolean before = date.before(date2);
boolean after = date.after(date2);
Copy the code
  • toInstant
Instant instant = date.toInstant();
System.out.println(instant);
Copy the code
// Display the standard time with a time zone difference of 8
2019-11-25T09:55:27.543Z
Copy the code
  • from
Date from = Date.from(instant);
System.out.println(from);
Copy the code

The following outdated methods are replaced with Clandar class methods

2) Create using java.sql

We found that the Date class in the SQL package has no parameterless constructor. We injected a Date of type long using Date from the util package

 Date date = new Date(new java.util.Date().getTime());
 System.out.println(date);
Copy the code
// The result is displayed with the Date format as follows, indicating that the Date class of the SQL package overwrites the toString method without the detailed time
2019-11-25
Copy the code

The Date class inherits from the Date package from the util package. The Date class inherits from the Date package from the UTIl package. The method is basically the same.

  • toLocalDate()
// Convert to LocalDate
 LocalDate localdate = date.toLocalDate();
Copy the code

Java SQL. Date and util. Date

The difference between

1. Java.sql. Date is used for SQL, only date part, no time part

2. Java.util. date is the parent class of java.sql. Date

Between

java.sql.Date sqlDate=new java.sql.Date(utilDate.getTime());

The Calendar class

Calendar is an abstract class and cannot be created directly from New like Date. Created through the internal getInstance method.

Calendar instance = Calendar.getInstance();
System.out.println(instance);
Copy the code
// Display the result. The default toString is more complex
java.util.GregorianCalendar[time=1574735939207,areFieldsSet=true,areAllFieldsSet=true,lenient=true,zone=sun.util.calendar.ZoneInfo[id="Asia/Shanghai",offset=28800000,dstSavings=0,useDaylight=false,transitions=29,lastRule=null],firstDayOfWeek=1,minimalDaysInFirstWeek=1,ERA=1,YEAR=2019,MONTH=10,WEEK_OF_YEAR=48,WEEK_OF_MONTH=5,DAY_OF_MONTH=26,DAY_OF_YEAR=330,DAY_OF_WEEK=3,DAY_OF_WEEK_IN_MONTH=4,AM_PM=0,HOUR=10,HOUR_OF_DAY=10,MINUTE=38,SECOND=59,MILLISECOND=207,ZONE_OFFSET=28800000,DST_OFFSET=0]
Copy the code

Common methods are as follows

  • compareTo
// Similar to the Date class method, return a value
1  // The date in brackets is smaller than the previous date
-1 // The date in brackets is larger than the previous date
0  // The two dates are equal
int i = instance.compareTo(Calendar.getInstance());
Copy the code
  • getTime
// Return the Date class
Date time = instance.getTime();
System.out.println(time);
Copy the code
  • get

== Important method ==

// Pass in the Calendar class constants and return the corresponding information
int i1 = instance.get(Calendar.DAY_OF_WEEK);
int i2 = instance.get(Calendar.DAY_OF_MONTH);
System.out.println(i1);
System.out.println(i2);
//
3
26
Copy the code
  • setTime
// To set the time, pass the type Date
instance.setTime(new Date());
Copy the code
  • set
// Set the time via Calendar constants
instance.set(Calendar.YEAR,2019);
instance.set(Calendar.MONTH,11);
// You can set the time, minute and second
instance.set(2019.11.26);
Copy the code

New date class LocalDate LocalTime, LocalDateTime

The toLocalDate() method of the previous SQL package Date class converts it to the new Date class.

Java 8 has added the LocalDate and LocalTime interfaces, making the methods more practical. Date and SimpleDateFormatter are not thread-safe, while LocalDate and LocalTime, like the most basic String, are immutable types that are thread-safe and cannot be modified. In Java 8, date and time are explicitly divided into LocalDate and LocalTime. LocalDate cannot contain time, and LocalTime cannot contain date.

The constructor is now private, created through the built-in now() method

LocalDate date = LocalDate.now();
LocalTime time = LocalTime.now();
LocalDateTime dateTime = LocalDateTime.now();
System.out.println(date);
System.out.println(time);
System.out.println(dateTime);
Copy the code
// The following is displayed
2019-11-26
13:57:57.440
2019-11-26T13:57:57.440
Copy the code

Commonly used method

Format the date, SimpleDateFormat

  • The Date format

The == simpleDateFormat.parse () method converts a String to a Date==

 Date date = new Date();
 System.out.println(date);
  // Use SimpleDateFormat to customize the format
  SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  String format = dateFormat.format(date);
  System.out.println(format);
  // Convert String to Date
  try {
      Date parse = dateFormat.parse(format);
  } catch (ParseException e) {
      e.printStackTrace();
  }
Copy the code
// The following is displayed
Tue Nov 26 15:00:04 CST 2019
2019-11-26 15:00:04
Tue Nov 26 15:00:04 CST 2019
Copy the code
  • Calendar format
// The getTime method formats Calendar to Date
 Calendar instance = Calendar.getInstance();
 Date date = instance.getTime();
 SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
 String format = dateFormat.format(date);
 System.out.println(format);
Copy the code
  • LocalDate formatting
LocalDate date = LocalDate.now();
LocalTime time = LocalTime.now();
LocalDateTime dateTime = LocalDateTime.now();
System.out.println(date);
System.out.println(time);
System.out.println(dateTime);
// The following is displayed
2019-11-26
15:15:23.722
2019-11-26T15:15:23.722
Copy the code

Format localDate format() method, need to pass DateTimeFormatter

DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyyMMdd");
String format = date.format(dateTimeFormatter);
System.out.println(format);
DateTimeFormatter dateTimeFormatter2 = DateTimeFormatter.ofPattern("HH:mm:ss");
String format2 = time.format(dateTimeFormatter2);
System.out.println(format2);
// The following is displayed
20191126
16: 08:52
Copy the code

Time stamp (timestamp)

The time stamp is the total number of seconds from 00:00 00:00 00:00 1970 GMT (0800 00:00 00:00 1970 GMT) to the present time.

  • Gets the current timestamp
 // Get the timestamp in milliseconds
 long l = System.currentTimeMillis();
  System.out.println(l);
  //
  Date date = new Date();
  long time = date.getTime();
  System.out.println(time);
  //
  long timeInMillis = Calendar.getInstance().getTimeInMillis();
  System.out.println(timeInMillis);
Copy the code
  • Time stamp to date
// The Date construct can be converted directly
Date date1 = new Date(1574758172536L);
System.out.println(date1);
Copy the code