Javase | Date class, SimpleDateFormat class, System class

1.Date class (date class)

1.1 Construction method of Date class

public Date( )

  • public Date( ): No-argument constructor to create a Date object .

  • The output Date object is the current date and time of the system .

       //创建Date对象
       Date nowTime = new Date(); 
    
        //输出系统当前的“日期和时间”
       //Date类中的toString()方法被重写了,所以此处输出的不是一个内存地址,应是一个日期字符串。
       System.out.println(nowTime); //Thu Sep 14 14:36:24 CST 2023
    

public Date( long date )

  • public Date( long date ): Construction method with parameters , passed" specified number of milliseconds "to create a Date object .

       //有参构造方法,通过“指定的毫秒数”创建Date对象
    
       /*
       这个时间是: 1970-01-01 00:00:00 001 (格林尼治时间),
       通过下面的时间格式转换,可以验证
       */
       Date date = new Date(1); //注意: 参数 1 是一个毫秒数
    
       SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss SSS");
       String format = sdf.format(date);
       System.out.println(format); //1970-01-01 08:00:00 001 (北京时间)
    

2.SimpleDateFormat class (simple date format class)

2.1 Definition of SimpleDateFormat class

  • SimpleDateFormat( Simple date format class ): Under the Java . text package, specificallyResponsible for date formatting

2.2 Construction method of SimpleDateFormat

SimpleDateFormat( )

  • SimpleDateFormat( ): No-argument constructor to create a SimpleDateFormat object.
   //SimpleDateFormat 在Java.text包下的,专门负责日期格式化。
   SimpleDateFormat sdf = new SimpleDateFormat();

SimpleDateFormat( String pattern )

  • SimpleDateFormat( String pattern ): Construction method with parameters , usegiven patternand default locale date format symbolConstruct SimpleDateFormat object

  • in this constructorparameter: Date format symbol . That is, the content passed in the parameter is: date format symbol , to create a "simple date format" object

  • SimpleDateFormat can be combined with.format(Date date)The method can get the " date information " in the specified format . The format() method is the method of DateFormat, the parent class of SimpleDateFormat.

     	     //创建Date对象
            Date nowTime = new Date();
            /*
               日期格式符号:
               yyyy 年
               MM   月
               dd   日
               HH   时
               mm   分
               ss   秒
               SSS  毫秒
    
              完整表示年月日时间秒的日期格式符号 : yyyy-MM-dd HH:mm:ss:SSS
             */
            //该构造方法中的参数为“日期格式符号”,以指定的日期格式来创建简单日期格式对象
    SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS");
            SimpleDateFormat sdf3 = new SimpleDateFormat("dd/MM/yyyy");
            SimpleDateFormat sdf4 = new SimpleDateFormat("yy/MM/dd HH:mm:ss");
    
            // 调用.format()方法 输出指定格式的"时间信息"
            String format2 = sdf2.format(nowTime);
            String format3 = sdf3.format(nowTime);
            String format4 = sdf4.format(nowTime);
    
            System.out.println(format2); //2023-09-13 20:24:48:243
            System.out.println(format3); //13/09/2023
            System.out.println(format4); //23/09/13 20:24:48
    

date format symbol

  • date format symbolThe most commonly used ones are:

    yyyy year
    MM month
    dd day
    HH
    hour mm
    minute ss second
    SSS millisecond (highest three digits, up to 999, 1000 milliseconds represents 1 second)

  • Date format symbol that fully represents year, month, day, time and seconds : yyyy-MM-dd HH:mm:ss:SSS

letter date or time element express Example
G Era identifier Text AD
y Year Year 1996; 96
M month of year Month July; Jul; 07
w week number in year Number 27
W week number in month Number 2
D days in year Number 189
d number of days in month Number 10
F week of month Number 2
E Days of the week Text Tuesday;Tue
a Am/pm mark Text PM
H Hour of day (0-23) Number 0
k Hour of day (1-24) Number 24
K Hour in am/pm (0-11) Number 0
h Hour in am/pm (1-12) Number 12
m minutes in hour Number 30
s seconds in minutes Number 55
S milliseconds Number 978
z Time zone General time zone Pacific Standard Time; PST; GMT-08:00
Z Time zone RFC 822 time zone -0800

3. Convert "Date type" to "date string String"

  • first step:Creates a SimpleDateFormat (date format notation) object.

  • Step 2: Call SimpleDateFormat parent class: DateFormat method:format( Date date), to convert Date into " date string String " ( " date information " in the specified format ).

  • important point:

    The parameters in new SimpleDateFormat (date format symbol) are:date format symbol

    The parameters in the format(Date date) method are:Date object

  • Examples include:

    	    //创建Date对象
            Date date = new Date();
            /*
               日期格式符号:
               yyyy 年
               MM   月
               dd   日
               HH   时
               mm   分
               ss   秒
               SSS  毫秒
    
              完整表示年月日时间秒的日期格式符号 : yyyy-MM-dd HH:mm:ss:SSS
             */
            //该构造方法中的参数为“日期格式符号”,以指定的日期格式来创建简单日期格式对象
    SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS");
            SimpleDateFormat sdf3 = new SimpleDateFormat("dd/MM/yyyy");
            SimpleDateFormat sdf4 = new SimpleDateFormat("yy/MM/dd HH:mm:ss");
    
            // 调用.format()方法 输出指定格式的"时间信息"
            String format2 = sdf2.format(date);
            String format3 = sdf3.format(date);
            String format4 = sdf4.format(date);
    
            System.out.println(format2); //2023-09-13 20:24:48:243
            System.out.println(format3); //13/09/2023
            System.out.println(format4); //23/09/13 20:24:48
    

4. Convert "date string String" to "Date type"

  • first step:Creates a SimpleDateFormat (date format notation) object.

  • Step 2: Call SimpleDateFormat parent class: DateFormat method:parse( String pattern ), to convert " date string String " to " Date type ".

  • important point:

    The parameters in new SimpleDateFormat (date format symbol) are:date format symbol. But the " date format symbol" Don't write casually, must be in the same format as "date string " , otherwise an exception will occur.

    The parameters in the parse(String pattern) method are:Date format string String

  • Examples include:

       //假设现在有一个日期字符串String,怎么转换为Date类型?
       String time = "2008-08-08 08:08:08 888";
       SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss SSS");
       
       //调用parse()方法将"日期字符串String"转换为Date类型
       Date date = sdf.parse(time);
       System.out.println(date); //Fri Aug 08 08:08:08 CST 2008
    

5. Actual functional requirements:

Requirement: How to get the total number of milliseconds from 00:00:00 000 on January 1, 1970 to the "current system time"

   //获取自1970年1月1日 00:00:00 000到当前系统时间的总毫秒数。
   //1秒 = 1000毫秒
   long nowTimeMillis = System.currentTimeMillis();
   System.out.println(nowTimeMillis); //1694666718586

Requirements: Count the time it takes to execute a method

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


        //统计一个方法执行所耗费的时长
        //在调用目标方法之前记录一个毫秒数
        long begin = System.currentTimeMillis();
        print();
        //在执行完目标方法之后记录一个毫秒数
        long end = System.currentTimeMillis();
        System.out.println("耗费的时长"+(end - begin)+"毫秒");
    }

    //统计一个方法执行所耗费的时长
    private static void print() {
    
    
        for (int i = 0; i < 1000; i++) {
    
    
            System.out.println("i= " + i);
        }
    }
}

Requirement: Get the time of "yesterday" at this time

   //获得"昨天"的此时的时间
   Date date = new Date(System.currentTimeMillis() - 1000 * 60 * 60 * 24);
   SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss SSS");
   String strTime = sdf.format(date);
   System.out.println(strTime);

6.System class

Related properties and methods of the System class:

System.outout is a static variable of the System class .

System.out.println( ): The println method is not System's, but a method of PrintStream class .

System.gc( ): Start the garbage collection mechanism .

System.currentTimeMillis( ): Get the total number of milliseconds from 00:00:00 000 on January 1, 1970 to the current system time .

System.exit(0 ): Exit the JVM .

Guess you like

Origin blog.csdn.net/m0_70720417/article/details/132877665