Java Self-Learning - Date

Keywords: Java SQL Database Unix

Java Date Class

Date class
Note: It's java.util.Date;
Instead of java.sql.Date, this class is used for database access

Example 1: The concept of time origin

All data types, whether integer, Boolean, floating point or string, need to be represented in the form of numbers.

Date types are no exception, in other words, a date, such as October 1, 2020, will be replaced by a number in the computer.

So the most special number is zero. Zero, which represents the origin of time in Java. Its corresponding date is 8:0 minutes and 0 seconds on January 1, 1970. (Why 8 o'clock, because China's Pacific Time Zone is UTC-8, just eight hours from Greenwich Time)

Why did it correspond to 1970? Because the first UNIX version, AT&T, was released in 1969, when 1970 was considered as the time origin.

All dates are based on this zero point, every millisecond, plus 1.

Example 2: Create a date object

package date;
 
//
import java.util.Date;
 
public class TestDate {
 
    public static void main(String[] args) {
 
        // current time
        Date d1 = new Date();
        System.out.println("current time:");
        System.out.println(d1);
        System.out.println();
        // The number of milliseconds experienced since 8:0 a.m. on January 1, 1970
        Date d2 = new Date(5000);
        System.out.println("It took five seconds from 8:0 a.m. on January 1, 1970.");
        System.out.println(d2);
 
    }
}

Example 3: getTime

getTime() gets an integer of long type
This integer represents an increase of 1 per millisecond from 1970.1.108:00:00:000.
Print the object directly and you will see a format like "Tue Jan 05 09:51:48 CST 2016"

package date;
  
//
import java.util.Date;
  
public class TestDate {
  
    public static void main(String[] args) {
        //Note: It's java.util.Date;
        //Instead of java.sql.Date, this class is used for database access
        Date now= new Date();
        //Print the current time
        System.out.println("current time:"+now.toString());
        //getTime() gets an integer of long type
        //This integer represents 1970.1.108:00:00:000, an increase of 1 per millisecond.
        System.out.println("current time getTime()The value returned is:"+now.getTime());
          
        Date zero = new Date(0);
        System.out.println("Using 0 as the construction method, the date is:"+zero);
          
    }
}

Example 4: System. current Time Millis ()

Number of milliseconds for the current date
new Date().getTime() is the same as System.currentTimeMillis().
However, due to the performance of the machine, it may differ by tens of milliseconds. After all, it takes time to execute every line of code.

package date;
   
//
import java.util.Date;
   
public class TestDate {
   
    public static void main(String[] args) {
        Date now= new Date();
  
        //Number of milliseconds for the current date
        System.out.println("Date.getTime() \t\t\t Return value: "+now.getTime());
        //Get the number of milliseconds of the current date through System.currentTimeMillis()
        System.out.println("System.currentTimeMillis() \t Return value: "+System.currentTimeMillis());
           
    }
}

Practice: date

With the help of random numbers, create a random date from 1995.1.100:00:00 to 1995.12.31 23:59:59

Answer:

package date;
 
import java.util.Date;
 
public class TestDate {
    
    public static void main(String[] args) {
        long second = 1000;
        long minute = 60*second;
        long hour = minute * 60;
        long day = hour*24;
        long year = day * 365;
        long year1995Start  = (1995-1970) * year;
        long leapDay = (1995-1970)/4*day; //There is a moistening day every four years.
        year1995Start+=leapDay;
        long eightHour = hour*8; //The number of milliseconds for eight hours, because the corresponding number from 0 milliseconds is 1970.1.108:00:00.
        year1995Start-=eightHour;
         
        Date dStart = new Date(year1995Start);
        System.out.println("1995 On the first day of the year:"+dStart);
        long year1995End = year1995Start + year - 1;
        Date dEnd = new Date(year1995End);
        System.out.println("1995 On the last day of the year:"+dEnd);
         
        long randomTime = (long) (Math.random()*(year-1) + year1995Start);
        Date dRandom = new Date(randomTime);
        System.out.println("1995 A random time of the year.:" + dRandom);
          
    }
}

Posted by cauri on Thu, 10 Oct 2019 01:51:52 -0700