Java Foundation (16) - Date SimpleDateFormat CalendarDate class

Posted by tonga on Sat, 12 Feb 2022 14:50:41 +0100

catalogue

Overview of Date class

Construction method

Member method

Conversion between Date type and long type millisecond value

SimpleDateFormat class

SimpleDateFormat

Construction method

Definition of common rules for the SimpleDateFormat class

Member method

Case: calculate how many days you have been in this world.

Calendar Class

Member method

getInstance() method and get() method

Use of the add() set() method

case

Overview of Date class

Class Date represents a specific moment, accurate to milliseconds.

Construction method

public Date()
public Date(long date)  //Convert a millisecond value of long type into a date object

Member method

public long getTime():  //Gets the millisecond value of a date object
public void setTime(long time): //Sets the millisecond value specified on a date object

1) If we directly create a Date object using null parameter construction, it is equivalent to initializing this Date object. This object represents the millisecond value from the base time (calendar year, i.e. January 1, 1970, 00:00:00 GMT) to the current system time. As follows:

import java.util.Date;
​
public class Test {
    public static void main(String[] args) {
        Date date = new Date();
        long time = date.getTime();
        System.out.println(time);
    }
}

At this time, the output result is:

2) The corresponding millisecond value can be increased by using the parameterized structure, for example:

import java.util.Date;
​
public class Test2 {
    public static void main(String[] args) {
        Date date = new Date(1000*60*60);
        long time = date.getTime();
        System.out.println(time);
    }
}

The millisecond value added by the output result for us:

  1. Conversion between Date type and long type millisecond value

Date -- > long millisecond value

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

long millisecond value -- > date

long num = 1644628496559L;
Date date1 = new Date(num);

Conversion code implementation:

import java.util.Date;
​
public class Test3 {
    public static void main(String[] args) {
        //Date --> long
        Date date = new Date();
        long time = date.getTime();
        System.out.println(time);
        //long --> Date
        long num= 1644628496559L;
        Date date1 = new Date(num);
        System.out.println(date1);
    }
}

Results achieved:

SimpleDateFormat class

SimpleDateFormat

You can format a date object into a string (text), or you can parse a date string into a date object.

Construction method

public SimpleDateFormat()//Use the default schema to create a SimpleDateFormat object
public SimpleDateFormat(String pattern)//Create a SimpleDateFormat object using the specified pattern (rules such as yyyy:MM:dd HH:mm:ss)

Code for creating SimpleDateFormat objects using nonparticipated and parameterized constructs respectively:

import java.text.SimpleDateFormat;
​
public class Test4 {
    public static void main(String[] args) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat();
        SimpleDateFormat simpleDateFormat1 = 
                new SimpleDateFormat("yyyy-MM-dd");
    }
}

Definition of common rules for the SimpleDateFormat class

Common rules: yyyy MM DD HH: mm: SS

Representative charactermeaning
yyear
Mmonth
dday
HTime
mbranch
ssecond

Member method

public String format(Date date) //Format a date object into a string
public Date parse(String dateStr) //Parse a date string into a date object. Pay attention to parsing in the specified format

Format the date object as a string

import java.text.SimpleDateFormat;
import java.util.Date;
​
public class Test4 {
    public static void main(String[] args) {
        SimpleDateFormat simpleDateFormat =
                new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = new Date();
        String format = simpleDateFormat.format(date);
        System.out.println(format);
    }
}

Parse string into Date object

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Scanner;
​
public class Test5 {
    public static void main(String[] args) throws ParseException {
        SimpleDateFormat simpleDateFormat =
                new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Scanner scanner = new Scanner(System.in);
        Date date = simpleDateFormat.parse(scanner.nextLine());
        System.out.println(date);
    }
}

  1. Case: calculate how many days you have been in this world.

Analysis: according to the methods of Date and SimpleDateFormat classes, our steps are as follows:

1) Keyboard input birthday (i.e. a date string)

2) Parse the string into a Date object Date

3) Get the millisecond value time corresponding to Date

4) Gets the millisecond value time1 of the current system time Date1

5) Calculation (time1 time) / 1000 / 60 / 60 / 24

6) Print calculation results

Code implementation:

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Scanner;
​
public class Test6 {
    public static void main(String[] args) throws ParseException {
        Scanner sc = new Scanner(System.in);
        SimpleDateFormat simpleDateFormat =
                new SimpleDateFormat("yyyy-MM-dd");
        Date date = simpleDateFormat.parse(sc.nextLine());
        long time = date.getTime();
        long time1 = new Date().getTime();
        int days = (int) ((time1 - time) / 1000 / 60 / 60 / 24);
        System.out.println(days);
    }
}

Calendar Class

Member method

public void add(int field,int amount)//Adds or subtracts the specified amount of time for a given calendar field according to the rules of the calendar
public final void set(int year,int month,int date)//Set calendar time (mm / DD / yy)
  1. getInstance() method and get() method

static Calender getInstance() //Get a calendar using the default time zone and default locale
public int get(int field)//Gets the value corresponding to the given calendar character

For example: get the year of the current system time

import java.util.Calendar;
​
public class Test7 {
    public static void main(String[] args) {
        Calendar instance = Calendar.getInstance();
        int year = instance.get(Calendar.YEAR);
        System.out.println(year);
    }
}

Use of the add() set() method

import java.util.Calendar;
​
public class Test8 {
    public static void main(String[] args) {
        Calendar instance = Calendar.getInstance();
        //Use the set() method to set the date and time
        instance.set(2020, 10, 10, 14, 30, 30);
        int year = instance.get(Calendar.YEAR);
        System.out.println(year);
        //Use the add() method to add the year
        instance.add(Calendar.YEAR,2);
        int year1 = instance.get(Calendar.YEAR);
        System.out.println(year1);
    }
}

Operation results:

  1. case

Use the method of Calendar class to judge whether any year is a leap year.

import java.util.Calendar;
import java.util.Scanner;
​
public class Test9 {
    public static void main(String[] args) {
        Calendar instance = Calendar.getInstance();
        Scanner sc = new Scanner(System.in);
        instance.set(sc.nextInt(),2,1);
        instance.add(Calendar.DAY_OF_MONTH,-1);
        if(instance.get(Calendar.DAY_OF_MONTH)==29){
            System.out.println("leap year");
        }else{
            System.out.println("Ordinary year");
        }
    }
}

Operation results

Topics: Java Android linq