The article directories

      • First, new
      • Second, compare
      • Third, print it out
      • Fourth, get the current time
      • Fifth, convert SimpleDateFormat to and from each other
        • Conversions: Date and TimeStamp
        • Conversion: Date and String
      • Sixth, add a Calendar day
      • The overall code

First, new

Timestamp two new methods, call the constructor, with long new, call valueOf static function, with a string new.

Date New Date() gets the current time. To get the specified time, you can convert it to String or Timestamp.

String is simply a new String, new String() or assignment.

Class Solution {public static void main(String[] args) throws Exception {// Timestamp Creates an object in two ways: ValueOf Timestamp timestampNow = new Timestamp(system.currentTimemillis ()); // Timestamp constructor has two functions: Timestamp Timestamp timestamp1 = timestamp.valueof ()"The 1970-01-02 18:17:02. 255"); // Timestamp static method valueOf has two functions: convert the specified time to Timestamp and type conversion, and convert String to Timestamp // Date. Date dateNow = new Date(); Date Date = new Date(timestamp1.getTime()); // Set Timestamp to // date and String by SimpleDateFormat} // Set Timestamp to String by SimpleDateFormat}}Copy the code

Second, compare

Timestamp compareTo before after ();

[zipcodes], [zipcodes], [zipcodes], [zipcodes], [zipcodes].

String compares time as a method, use compartTo.

Class Solution {public static void main(String[] args) throws Exception {// Timestamp Timestamp = new Timestamp(123422255); Timestamp timestamp2 = new Timestamp(934233255); System.out.println(timestamp); System.out.println(timestamp2); System.out.println(timestamp.compareTo(timestamp2)); System.out.println(timestamp.before(timestamp2)); System.out.println(timestamp.after(timestamp2)); CompareTo system.out.println ();"= = = = = = = = = = = = = = = = = = = ="); Date date=new Date(timestamp.getTime()); Date date1=new Date(timestamp2.getTime()); System.out.println(date); System.out.println(date1); System.out.println(date.compareTo(date1)); System.out.println(date.before(date1)); System.out.println(date.after(date1)); [^ # ^ # ^ # ^ #] [^ # ^ # ^ #];Copy the code

Third, print it out

Timestamp prints the standard Timestamp directly, and getTime prints long. Date prints the standard American time and getTime prints long. String prints a String directly.

Second, to print out the specified format, use SimpleDateFormat

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); System.out.println(sdf.format(new Date())); // The current time is printed in the specified formatCopy the code

SimpleDateFormat (Timestamp, String);

TimeStamp is converted to Date before it can be used in SimpleDateFormat (call format method); String judgment: Converted from SimpleDateFormat to Date, String can be used to determine whether the String complies with the time standard (call the parse method). String converts and prints: String converts to Date via SimpleDateFormat, and you can print standard American time again (call the parse method).

Class Solution {public static void main(String[] args) throws Exception {// TimeStamp Can be in SimpleDateFormat format only when converted to Date Timestamp timestamp = new Timestamp(123422255); Date date=new Date(timestamp.getTime()); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); String dateString= sdf.format(date); System.out.println(dateString); System.out.println(sdf.parse(dateString)); }}Copy the code

Fourth, get the current time

Date Create new Date to obtain the current American Standard time. The timestamp gets the current time through the constructor, long; String cannot get the current time.

Date date=new Date();
Timestamp timestampNow = new Timestamp(System.currentTimeMillis());
Copy the code

Fifth, convert SimpleDateFormat to and from each other

Conversions: Date and TimeStamp

The constructor and getTime are converted to each other

Date dateNow = new Date(timestampNow.getTime());
Timestamp timestampNowAgain = new Timestamp(dateNow.getTime());
Copy the code

Conversion: Date and String

SimpleDateFormat is a bridge between Date and String: Date and String. Since String can be in any format, the conversion between the two must be normalized by SimpleDateFormat.

Date parse(String source) Parse (String source) Parse (String source) Print the Date String format(Date Date) format, format the Date, print the String

The conversion between String and Timestamp seems to be absent. TimeStamp+”” becomes a string

class Solution {
    public static void main(String[] args) throws Exception {
        Date date=new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String dateString= sdf.format(date);
        System.out.println(dateString);   // Date is converted to dateString
        System.out.println(sdf.parse(dateString)); // dateString is converted to date}}Copy the code

The parse function converts a string to a date and the format function converts a date to a string. That is, parse the text of the string to produce Date. Format formats the given Date as a Date/time string and adds the result to the given StringBuffer.

Yyyy-mm-dd HH: MM :ss

Yyyy: there are two modes: YYYY and YYYY. Why yyyy? The only thing that matters is the last week of December,

Yyyy is Year, the current time or the time specified in the string. YYYY is Week Year: indicates the year of the Week of the current day. The Week starts on Sunday and ends on Saturday. The Week is counted as the year of the next year as long as it falls on the New Year’s day.

class Solution {
    public static void main(String[] args) throws Exception {
        System.out.println("========== use Calendar build time ==============="); Calendar calendar = Calendar.getInstance(); calendar.set(2017, Calendar.DECEMBER, 31); Date strDate1 = calendar.getTime(); // Date strDate1 = calendar.getTime(); SimpleDateFormat sf1 = new SimpleDateFormat("YYYY-MM-dd");
        System.out.println("Uppercase YYYY:" + sf1.format(strDate1));  // 2018-12-31
        SimpleDateFormat sf2 = new SimpleDateFormat("yyyy-MM-dd");
        System.out.println("Lowercase YYYY:" + sf2.format(strDate1));  // 2017-12-31

        System.out.println("-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -"); calendar.set(2016, Calendar.DECEMBER, 31); // The last day of 2016 is Saturday, so the last week is not New Year strDate1 = calendar.getTime(); System.out.println("Uppercase YYYY:" + sf1.format(strDate1));  // 2016-12-31
        System.out.println("Lowercase YYYY:" + sf2.format(strDate1));  // 2016-12-31

        System.out.println("========== can also use String build time ===============");
        String dateString = "2017-12-31";
        System.out.println("Uppercase YYYY:" + sf1.parse(dateString));  // 2017-01-01
        System.out.println("Lowercase YYYY:" + sf2.parse(dateString));  // 2017-12-31
        System.out.println("-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -");
        dateString = "2016-12-31";
        System.out.println("Uppercase YYYY:" + sf1.parse(dateString));  // 2015-12-27
        System.out.println("Lowercase YYYY:"+ sf2.parse(dateString)); / / the 2017-12-31}}Copy the code

Month: indicates only M or MM. Lowercase M MM indicates the minute but not the month.

Jt: There are dd and DD. Why use DD?

The DD format is “day of year”, which indicates the number of days in the current year. If today is the 100th day of the current year, one digit is too many in the string. The DD format is Day of month, which indicates the number of days in the current month and cannot exceed two digits.

When: there are two ways of HH and HH, why HH? Since HH stands for 24 hours HH stands for 12 hours, the former is more commonly used.

Minutes: only m or mm can be used. Mm indicates the month, not the minute

Ss: indicates the second.SSS: indicates the millisecond. It is used together with HH:mm: ss.sss.

M,H, M, M, M, s means non-zero start. MM,HH, MM, ss means zero start. For example, the month with one digit M has no leading zero. The difference is that MM means starting from zero, like in April, MM says 04, M says 4. For example, at 1:02 a.m., HH:mm is displayed as 01:02 and H:m is displayed as 1:2.

In case, MM indicates the month. MM indicates the minute. HH indicates the 24-hour system

Sixth, add a Calendar day

If you want to add or subtract a day, you must first obtain the current day in Date, which uses Calendar, and obtain and manipulate certain fields in Date.

Class Solution {public static void main(String[] args) throws Exception {// SimpleDateFormat Is used to check the format of a String, Calendar is used to add to or subtract from dates"The 2020-09-18 10:09:08";
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); Date date = simpleDateFormat.parse(dateString); Calendar calendar = Calendar.getInstance(); System.out.println(calendar.getTime())); System.out.println(calendar.get(Calendar.YEAR) +"Year" + (calendar.get(Calendar.MONTH) + 1) + "Month" + calendar.get(Calendar.DATE) + "Day" + calendar.get(Calendar.HOUR) + "When" + calendar.get(Calendar.MINUTE) + "Points" + calendar.get(Calendar.SECOND) + "Seconds"); calendar.setTime(date); System.out.println(calendar.getTime())); calendar.add(Calendar.YEAR, 1); calendar.add(Calendar.MONTH, 1); calendar.add(Calendar.DATE, 1); calendar.add(Calendar.HOUR, 1); calendar.add(Calendar.MINUTE, 1); calendar.add(Calendar.SECOND, 1); System.out.println(calendar.getTime()); System.out.println(calendar.get(Calendar.YEAR) +"Year" + (calendar.get(Calendar.MONTH) + 1) + "Month" + calendar.get(Calendar.DATE) + "Day" + calendar.get(Calendar.HOUR) + "When" + calendar.get(Calendar.MINUTE) + "Points" + calendar.get(Calendar.SECOND) + "Seconds"); }}Copy the code

Note that SimpleDateFormat is used to detect the format of strings, and Calendar is used to add and subtract from Date differently.

Calendar.MONTH public final static int JANUARY = 0 public final static int JANUARY = 0 That is, the subscript of calendar. MONTH starts at 0, so we need to add 1 when we use calendar. MONTH to represent Calendar months.

Note that for the Calendar class, getInstance() is used to get a generic object of type Calendar, and getInstance() returns a Calendar object. Calendar C =new Calendar();

The reason: The Calendar class is an abstract class that implements specific subclass objects when used in practice. Because the Calendar class is abstract and the constructor of the Calendar class is protected, you cannot use the constructor of the Calendar class to create objects. The getInstance method is provided in the API to create objects.

So defining a Calendar object should be:

Calendar  cal= Calendar.getInstance();
Copy the code

The overall code

import java.sql.Timestamp; import java.text.SimpleDateFormat; import java.util.*; Class Solution {public static void main(String[] args) throws Exception {// Timestamp Timestamp = new Timestamp(123422255); Timestamp timestamp2 = new Timestamp(934233255); System.out.println(timestamp); System.out.println(timestamp2); System.out.println(timestamp.compareTo(timestamp2)); System.out.println(timestamp.before(timestamp2)); System.out.println(timestamp.after(timestamp2)); CompareTo system.out.println ();"= = = = = = = = = = = = = = = = = = = ="); ValueOf Timestamp timestamp1 = timestamp.valueof ("The 1970-01-02 18:17:02. 255");
        Timestamp timestamp3 = Timestamp.valueOf("The 1970-01-12 03:30:33. 255");
        System.out.println(timestamp);
        System.out.println(timestamp2);
        System.out.println(timestamp.compareTo(timestamp2));
        System.out.println(timestamp.before(timestamp2));
        System.out.println(timestamp.after(timestamp2));

        System.out.println("= = = = = = = = = = = = = = = = = = = ="); // Create a new date SimpleDateFormat class based on a String to print the date String time = in the specified format"The 1970-01-02 18:17:02. 255";
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); Date d1 = sdf.parse(time); System.out.println(d1); // Date system.out.println (sdf.format(d1)); // Date system.out.println (sdf.format(d1)); Date parse(String); // Date parse(String)sourceDate // String format(Date Date) format, Date format, String system.out.println (String system.out.println)"= = = = = = = = = = = = = = = = = = = ="); Date d2 = new date (); // Date Gets the current time system.out.println (d2); System.out.println(sdf.format(d2)); System.out.println("= = = = = = = = = = = = = = = = = = = ="); Timestamp timestampNow = new Timestamp(System.currentTimeMillis()); System.out.println(timestampNow); // Timestamp Get the current time system.out. println(timestampNow); Println (timeStamnow.getTime ()); // Print the timestamp format system.out.println (timestamnow.getTime ()); Long getTime() changes the standard timestamp to long Date dateNow = new Date(timeStamnow.getTime ()); System.out.println(dateNow); Timestamp timestampNowAgain = new Timestamp(datenow.getTime ()); Date and Timestamp both have getTime methods, which convert the standard American time to long and convert the standard Timestamp to long, and which can be printed in another way. System.out.println(timestampNowAgain); // Date can be converted to timestamp}}Copy the code