Date
- Date: java.util Date: java.sql.Date: java.util Date: java.sql.Date: java.util Date: java.sql.Date: java.util Date: java.sql.Date: java.util Date: java.sql.Date
- Let’s talk about Date under util
- Constructor:
- Date() and Date(long Date)
- GetTime () : Gets the value of the time in milliseconds, as of 1970
- Before (), after(), and equals() : compare dates
SimpleDateFormat
- Date formatting
- Constructor: SimpleDateFormat(pattern: “YYYY-MM-DD “)
- Common methods (the result of formatting is the formatting mode set in the constructor) :
- Format (Date Date) : Converts the Date Date to a string
- Parse (String date) : Formats a String as a date. The String format must match pattern. Otherwise, ParseException is thrown
Calendar
- Calendar c = calendar.getInstance () Calendar c = calendar.getInstance ()
Commonly used method
- get(int field)
- The get method is used to get the value of a specified field
- Calendar.YEAR, calendar. MONTH…
- Calendar months are 0-11 corresponding to January to December, and weeks are calculated from Sunday
- set(int field, int value)
- The set method sets the value of a specified field
- If you want to convert value, if it’s October, if you want to set October, you have to subtract 1 to get it, and if you set value to 9, you set the month to October
- add(int field, int amount)
- The add method adds or subtracts the value of a specified calendar field, adding the offset if the second argument is positive or subtracting the offset if it is negative.
- getTime()
- Instead of retrieving the millisecond time, the getTime method in Calendar retrieves the corresponding Date object
Class used in Java8 to calculate jet lag
1. Period
- The static method between of Period is used to calculate the two localdates
- The specific difference is obtained by calling the class methods getYears(), getMonths()), getDays()
Duration (seconds and milliseconds)
Various conversions between Date
1, Format Date and then go to String
Date date = new Date();
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("YYYY-MM-dd");
String format = simpleDateFormat.format(date);
System.out.println(format);
Copy the code
Output:
2020-10-16
Copy the code
2. Convert Date to Datetime
Date date = new Date();
long time = date.getTime();
Timestamp timestamp = new Timestamp(time);
System.out.println(timestamp);
Copy the code
Output:
The 2020-10-16 12:28:03. 169Copy the code
3. Convert String to Date
String d = "2020-10-16";
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("YYYY-MM-dd");
Date date = null;
try {
date = simpleDateFormat.parse(d);
} catch (ParseException e) {
e.printStackTrace();
}
System.out.println(date);
Copy the code
Output:
Fri Oct 16 00:00:00 CST 2020
Copy the code
String is converted to java.sql.Date
String d = "2020-10-16";
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
java.sql.Date date = null;
try {
Date parse = simpleDateFormat.parse(d);
date = new java.sql.Date(parse.getTime());
} catch (ParseException e) {
e.printStackTrace();
}
System.out.println(date);
Copy the code
Output:
2020-10-16
Copy the code