Advanced JAVA - Date class, SimpleDateFormat class, and Calendar class commonly used APIs (5)

Table of contents

API 

       1.0 Description of Date class

        ​ ​ 1.1 How to create a Date class object

         ​ ​ 1.2 getTime() instance method in Date class

        1.3 Parameterized constructor in Date class  

         ​ ​ 1.4 setTime(long time) instance method in Date class

        ​ ​ 2.0 Description of SimpleDateFormat class

        ​ ​ 2.1 How to create an object of type SimpleDateFormat

        ​ ​ 2.2 format() instance method in the SimpleDateFormat class

        ​ ​ 2.3 The parse() instance method in the SimpleDateFormat class

        3.0 Description of Calendar class

        3.1 How to obtain an object of Calendar type

        3.2 get() instance method in Calendar class

        3.3 getTime() instance method in Calendar class

        3.4 getTimeInMillis() instance method in Calendar class 


API 

       1.0 Date Category Description

        Represents date and time.

        1.1 How to build Date classification

        You can new an object through the parameterless constructor.

code show as below:

import java.util.Date;

public class DateAPI {
    public static void main(String[] args) {
        Date date = new Date();
        System.out.println(date);
    }

}

The running results are as follows:

        Use the overridden toString() method to output the date object in the form of a string.

         1.2 Date in category getTime() Example Method

          returns the current time millisecond value, which must be received by a variable of type long .

code show as below:

import java.util.Date;

public class DateAPI {
    public static void main(String[] args) {
        Date date = new Date();
        long a = date.getTime();
        System.out.println(a);

    }
}

 The running results are as follows:

        1.3 Date Parameterized constructor in class  

        Specifies a specific time and returns an object of type Date .

code show as below:

import java.util.Date;

public class DateAPI {
    public static void main(String[] args) {
        Date date = new Date();
        long a = date.getTime();
        Date date1 = new Date(a+2000);
        //a代表的是当前的时间毫秒值,a+2000代表实际上加了两秒
        //所以这个有参数的构造器可以用来设定日期返回Date类型的对象
        System.out.println(date1);
    }
}

The running results are as follows:

         1.4 Date class setTime(long time) Practical method

        Pass in the time millisecond value, and it is set as the time object at this moment. This method has no return value. What is changed is the current time and no new object is created.

code show as below:

import java.util.Date;

public class DateAPI {
    public static void main(String[] args) {
        Date date = new Date();
        long a = date.getTime();
        date.setTime(a+5000);
        System.out.println(date);

    }

 The running results are as follows:

         2.0 SimpleDateFormat Type of explanation

        Format an object of type Date.

        2.1 How to create an object of type SimpleDateFormat

        It is possible to pass the construction method to the construction, and enter the number of concrete formalities. Comparative yyyy(year), MM(month), EEE< /span>(Upper morning or lower afternoon)a(星期几)、 (seconds), ss (minutes), mm (hour), HH (day), dd

 code show as below:

import java.text.SimpleDateFormat;
import java.util.Date;

public class SimpleDateFormatAPI {
    public static void main(String[] args) {
        
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        
    }

        2.2  SimpleDateFormat Specific format() Example Method

        Format the object of type Date . The passed parameter can be a date object or a time millisecond value. What is returned is a string type.

code show as below:

import java.text.SimpleDateFormat;
import java.util.Date;

public class SimpleDateFormatAPI {
    public static void main(String[] args) {

        Date date = new Date();
        long a = date.getTime();

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        //参数为日期对象
        String s1 = sdf.format(date);
        //参数为时间毫秒值
        String s2 = sdf.format(a);

        //输出的结果都是一样的当前时间
        System.out.println(s1);
        System.out.println(s2);
    }

}

The running results are as follows:

        2.3 SimpleDateFormat in category parse() Example Method

        Pass in the formatted string and return an object of type Date.

code show as below:

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class SimpleDateFormatAPI {
    public static void main(String[] args) throws ParseException {

        Date date = new Date();
        long a = date.getTime();

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        //参数为日期对象
        String s1 = sdf.format(date);
        //参数为时间毫秒值
        String s2 = sdf.format(a);

        //输出的结果都是一样的当前时间
        System.out.println(s1);
        System.out.println(s2);
        
        //将格式化的字符串
        Date d1 = sdf.parse(s1);
        Date d2 = sdf.parse(s2);

        System.out.println(d1);
        System.out.println(d2);
    }

}

The running results are as follows:

        3.0 Calendar Category explanation

       Represents the calendar corresponding to the system time at this moment, through which the year, month, day, hour, minute, second, etc. can be obtained individually.

        3.1 How to obtain an object of type Calendar

        SinceCalendar class is an abstract class and cannot be passed directly new  method.

        Calendar.getInstance() static method can be used to get Calendar< an object of type a i=4>.

code show as below:

import java.util.Calendar;

public class CalendarAPI {
    public static void main(String[] args) {
        Calendar calendar = Calendar.getInstance();
        System.out.println(calendar);
    }
   

}

The running results are as follows:

         What you get is all kinds of information.

        3.2 Calendar classified get() Example Method

        Get an item from the calendar.

code show as below:

import java.util.Calendar;

public class CalendarAPI {
    public static void main(String[] args) {
        Calendar calendar = Calendar.getInstance();
        System.out.println(calendar);

        int m = calendar.get(calendar.MONTH);
        System.out.println(m);
    }
    
}

              Note that the month is calculated from 0, and the output is 9, but when this article was written, it was October.

        3.3 Calendar in category getTime() Example Method

        Get the date object.

code show as below:

import java.util.Calendar;
import java.util.Date;

public class CalendarAPI {
    public static void main(String[] args) {
        Calendar calendar = Calendar.getInstance();
        System.out.println(calendar);

        Date date = calendar.getTime();
        System.out.println(date);

    }

The running results are as follows:

        3.4 Calendar class getTimeInMillis() Example Method 

        Get the time in milliseconds.

code show as below:

import java.util.Calendar;
import java.util.Date;

public class CalendarAPI {
    public static void main(String[] args) {
        Calendar calendar = Calendar.getInstance();
        System.out.println(calendar);

        long a = calendar.getTimeInMillis();
        System.out.println(a);
    }

The running code is as follows:


Guess you like

Origin blog.csdn.net/Tingfeng__/article/details/133817666