Java basic detailed explanation "time origin, date, date formatting, turning calendar" + code example

date

If you want to learn date related knowledge, you must first know the Date class. To use the Date class, you must import, namely: import java.util.Date;

Origin of time

All data types, whether they are integers, booleans, floating-point numbers or strings, need to be expressed as numbers in the end.

The date type is no exception. In other words, a date, such as October 1, 2020, will be replaced by a number in the computer.

Then the most special number is zero. The number zero represents the origin of time in Java, and the corresponding date is January 1, 1970, 8: 0: 0. (Why is it 8 o'clock, because China's Pacific time zone is UTC-8, which is exactly 8 hours away from Greenwich Mean Time)

Why does it correspond to 1970? Because the first UNIX version was released in 1969: AT&T, considering all things considered, 1970 was taken as the origin of time at that time.

All dates are based on this 0 o'clock, and every millisecond passes, +1.

Create Date Object

Date d=new Date();

getTime

getTime() Gets a long integer.
This integer represents an increase of 1 for every millisecond from 1970.1.1 08:00:00:000.

import java.util.Date;

public class TestDate {
  
    public static void main(String[] args) {
        //注意:是java.util.Date;
        //而非 java.sql.Date,此类是给数据库访问的时候使用的
        Date now= new Date();
        //打印当前时间
        System.out.println("当前时间:"+now.toString());
        //getTime() 得到一个long型的整数
        //这个整数代表 1970.1.1 08:00:00:000,每经历一毫秒,增加1
        System.out.println("当前时间getTime()返回的值是:"+now.getTime());
          
        Date zero = new Date(0);
        System.out.println("用0作为构造方法,得到的日期是:"+zero);
          
    }
}

operation result:

System.CurrentTimeMillis();

The number of milliseconds of the current date
new Date().getTime() and System.currentTimeMillis() are the same.
However, due to machine performance, there may be a difference of tens of milliseconds. After all, every line of code execution takes time.

Examples of exercises

With the help of random numbers , create a random date between the last day of 1999 and the random date between the first day of 2021

import java.text.ParseException;//解析异常类
import java.text.SimpleDateFormat;//日期格式化类
import java.util.Date;

public class SimleDateFormat {
	public static void main(String[] args) {
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String str2="2021-01-01 00:00:00";
		String str1="1999-12-31 23:59:59";
		try {
	    Date d1=sdf.parse(str1);
	    System.out.println("1999年的最后一天:"+d1);
	    Date d2=sdf.parse(str2);
	    System.out.println("2021年的第一天:"+d2);
	    long timeMinus=d2.getTime()-d1.getTime();
	    long timeSum=(long)(Math.random()*(timeMinus+d1.getTime()));
	    Date d3=new Date(timeSum);
	    System.out.println("1999-2021间的随机日期:"+d3);
	}catch(ParseException e) {
		e.printStackTrace();
	}
		
	}
}

operation result:

 

 

Guess you like

Origin blog.csdn.net/qq_44624536/article/details/113604304