Use of Java Date and Time Class

1 Get the current time

  • Using the Date class

  • Using the Calendar class

// 1 Use the Date class
Date date = new Date();
System.out.println(date.toString());
// Fri Sep 13 21:47:38 CST 2019

// 2 Use the Calendar class
Calendar c = Calendar.getInstance();
System.out.println(c.getTime());
// Fri Sep 13 21:53:45 CST 2019

2 Designated Date

// 1 Use Calendar to specify the date
Calendar c = Calendar.getInstance();
c.set(2019, 9, 11); // 0 represents January.
System.out.println(c.getTime());
// Fri Oct 11 21:53:45 CST 2019

// Two-day comparison, using Date generation date and comparison
boolean before = new Date(2019, 9, 11).before(new Date(2019, 9, 8));
System.out.println(before);
// false

3 Format date and time

Use SimpleDateFormat

// yyyy is the full year, MM is the month, dd is the date, HH:mm:ss is the time, minutes, seconds.
// hh is a 24-hour system, while hh is a 12-hour system.
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
SimpleDateFormat dateFormat1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

String formatDate = dateFormat.format(date);
String formatDate1 = dateFormat1.format(date);

System.out.println(formatDate);
// 2019-09-13 09:56:48
System.out.println(formatDate1);
// 2019-09-13 21:58:24

4 Format output date and time

  • % After t, the year of the output date is represented by y (2-digit year, such as 99)

  • % The month of the output date is denoted by m after t, and the date of the output date is denoted by d after% t.

  • % After t, the year of the output date (4-digit year) is denoted by Y.

  • % T is followed by B for the full name of the month of the output date, and B for the month of the output date after% t.

Date date = new Date();
// getTime() returns the number of milliseconds represented by this Date object since 00:00 GMT on January 1, 1970.
long dataL = date.getTime();

// Format year, month and day
// % After t, the year of the output date is represented by y (2-digit year, such as 99)
// % The month of the output date is denoted by m after t, and the date of the output date is denoted by d after% t.
System.out.printf("%1$ty-%1$tm-%1$td; %2$ty-%2$tm-%2$td%n", date, dataL);
// 19-09-13; 19-09-13

// % After t, the year of the output date (4-digit year) is denoted by Y.
// % T is followed by B for the full name of the month of the output date, and B for the month of the output date after% t.
System.out.printf("%1$tY-%1$tB-%1$td; %2$tY-%2$tb-%2$td%n", date, dataL);
// 2019-September-13; 2019-September-13
// The use of c
System.out.printf("Full date and time information:%tc%n", date);
// Full Date and Time Information: Friday September 13 22:04:55 CST 2019

// The use of f
System.out.printf("year-month-Japanese format:%tF%n", date);
// Year-month-day format: 2019-09-13

// The use of d
System.out.printf("month/day/Annual format:%tD%n", date);
// Month/day/year format: 09/13/19

// The use of r
System.out.printf("HH:MM:SS PM Format (12 hours):%tr%n", date);
// HH:MM:SS PM format (12 o'clock): 10:04:55 p.m.

// The use of t
System.out.printf("HH:MM:SS Format (24 hours):%tT%n", date);
// HH:MM:SS format (24 hours): 22:04:55

// The use of R
System.out.printf("HH:MM Format (24 hours):%tR", date);
// HH:MM format (24 time): 22:04

5 Resolve strings to time

// 1 time string
String testDate = "2019-11-11 09:23:34";
// 2 Analytic Format
SimpleDateFormat parseFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

try {
    // 3 Analysis
    Date parse = parseFormat.parse(testDate);
    // 4 output
    System.out.println(parse);
    // Mon Nov 11 09:23:34 CST 2019
} catch (ParseException e) {
    e.printStackTrace();
}

6 Calculate date and time interval

// 1 Specify two dates and times
Date date1 = new Date(2019, 11, 11, 9, 12, 34);
Date date2 = new Date(2019, 12, 12, 10, 23, 23);

// 2 Calculate time interval, ms as unit
long interval = Math.abs(date1.getTime() - date2.getTime());
System.out.println(interval + " ms");
// 2769049000 ms

// Converting 3 to s
long seconds = interval / 1000;

System.out.println(seconds / 3600 + "hour " 
                   + seconds % 3600 / 60 + "minute " 
                   + seconds % 3600 % 60 + "seconds");
// 769hour 10minute 49seconds

Posted by darko886 on Fri, 04 Oct 2019 18:52:57 -0700