Date time related classes

Posted by jainsy on Mon, 10 Jan 2022 19:57:10 +0100

Date and time related classes Date,SimpleDateFormat,Calendar

Date class

Constructor

  • public Date()

    Allocates a Date object and initializes it to represent the time (in milliseconds) it was allocated to the system.

  • Date(long millisec)

    This parameter is the number of milliseconds since January 1, 1970.

common method

  • public long getTime()

    Returns the number of milliseconds represented by this Date object since January 1, 1970, 00:00:00 GMT.

  • public String toString()

    Convert this Date object to a String of the following form:
    dow mon dd hh:mm:ss zzz yyyy where:
    dow is the day of the week (Sun, Mon, Tue, Wed, Thu, Fri, Sat).
    mon is the month (Jan, Feb, Mar, Apr, May, Jun, Jul, Aug, Sep, Oct, Nov, Dec).
    dd is a day in a month (01 to 31) and is displayed as a two digit decimal number.
    hh is the hour of the day (00 to 23) and is displayed as a two digit decimal number.
    mm is the minute in the hour (00 to 59) and is displayed as a two digit decimal number.
    ss is the number of seconds in minutes (00 to 61) and is displayed as a two digit decimal number.
    Zzz is the time zone (and can reflect daylight saving time). Standard time zone abbreviations include time zone abbreviations recognized by the method parse. If no time zone information is provided, zzz is empty, that is, it does not include any characters at all.

    yyyy is the year and is displayed as a 4-digit decimal number.

Demo

public static void main(String[] args){
        // Create Date of current time
        Date date = new Date();
        System.out.println(date);

        // One hour before the current time
        long time = date.getTime() - (1*60*60*1000);
        Date date1 = new Date(time);
        System.out.println(date1);
    }

Output:
Wed Jul 28 15:54:27 CST 2021
Wed Jul 28 14:54:27 CST 2021

SimpleDateFormat class

summary

DateFormat is a formatting tool for Date. It can help us format Date, and then convert Date into the String string we want for our use

    public static void main(String[] args){

        /**
         *  y   :   year
         *  M   :   month
         *  d   :   day
         *  H   :   Time
         *  m   :   branch
         *  s   :   second
         *
         *  2021 16:08:00, July 28
         */

        SimpleDateFormat format = new SimpleDateFormat("yyyy year MM month dd day HH:mm:ss");
        
        // format: convert the data object into a string ("yyyy MM dd HH:mm:ss")
        String text = format.format(new Date());
        System.out.println(text);
        
        // parse: converts a string ("yyyy MM dd HH:mm:ss") into a data object
        Date date = null;
        try {
            date = format.parse("2020 August 1, 2010:00:00");
        } catch (ParseException e) {
            e.printStackTrace();
        }
        System.out.println(date);

        long time = (new Date().getTime() - date.getTime())/1000/60/60/24;
        System.out.println(time);
    }

Output:
2021 July 28, 2016:21:37
Sat Aug 01 10:00:00 CST 2020
361
  • Given a person's date of birth, ask for age

    1. Use the parse method in the SimpleDateFormat class to convert a String of type String to type Date
    2. Subtract from the current time obtained through getTime() to obtain the living time (unit: ms), which is converted into days

Calendar Class

concept

It is convenient to calculate the Date. The time zone and other issues are considered when obtaining the information in the Date, and the implementation method is more complex than the Date class;

Constructor

Calendar class is an abstract class. Because calendar class is an abstract class and the construction method of calendar class is protected, the construction method of calendar class cannot be used to create objects. getInstance method is provided in API to create objects.

Create a Calendar object that represents the current date of the system

//The default is the current date
Calendar c = Calendar.getInstance();

common method

Set

public final void set(int year,int month,int date)

  • Demo:
Calendar c1 = Calendar.getInstance();
c1.set(2009, 6, 12);//Set the year, month and day of Calendar object c1 as 2009, June and December respectively

Using field type settings

  • If you only set the value of a field, such as a date, you can use the following set method:

public void set(int field,int value)

  • Demo:
    public static void main(String[] args){
        Calendar c1 = Calendar.getInstance();
        // set method I
        c1.set(2022,0,10);
        
        // set method II
        c1.set(Calendar.YEAR, 2022);
        // Month 0-11, 0 is equivalent to January
        c1.set(Calendar.MONTH, 0);
        c1.set(Calendar.DATE, 10);
        
        // particular year
        int year = c1.get(Calendar.YEAR);
        // What day of the year
        int day = c1.get(Calendar.DAY_OF_YEAR);

        System.out.println(year);
        System.out.println(day);
    }

Output:
2022
10

Add

Calendar c1 = Calendar.getInstance();

Add 10 to the date of c1 object, that is, c1 is expressed as the date after 10 days, and all other values will be recalculated

c1.add(Calendar.DATE, 10);

Subtract 10 from the date of c1 object, that is, c1 is expressed as the date 10 days ago, and all other values will be recalculated

c1.add(Calendar.DATE, -10);
  • Demo:
    public static void main(String[] args){
        Calendar c1 = Calendar.getInstance();        
        c1.add(Calendar.MONTH, 3);
        System.out.println(c1.get(Calendar.MONTH));
        Date date = c1.getTime();
        System.out.println(date);
    }

Output:
9
Thu Oct 28 17:52:23 CST 2021

Gets the maximum value of a field

  • Returns the current date, the maximum value of a given field

int getActualMaximum(int field)

  • Returns the minimum value of the given field for the current date

int getActualMinimum(int field)

Topics: Java