Brief introduction to Date class, DateFormat class and Calendar Class

1, Package location

  • java.util.Date;
  • java.text.DateFormat;
  • java.lang.Calendar;

2, Summarize the introduction of the three as a whole


  • Represents a specific time with an accuracy of milliseconds
  • Not suitable for international processing;


  • Date formatting class;
  • The Date object can be formatted as a string in a specific format;
  • You can also convert a string of a specific format into a Date object;


  • Calendar Class
  • It appears after the Date class, mainly to solve the problem that the Date class cannot be internationalized

3, Date

1. Construction method

  • Only useful construction methods are recorded below
  • Date()
    • Nonparametric construction method
    • Indicates the current time
  • Date(long date)
    • Calculate the number of milliseconds from 00:00:00 GMT to date on January 1, 1970
      • GMT refers to Greenwich mean time. China is in the East eighth District, which needs eight hours
      • In other words, the constructor calculates the total number of milliseconds from 8:00 a.m. on January 1, 1970 to the time represented by the incoming date
      • Hereinafter referred to as green time (08:00:00, January 1, 1970)
    • date can be earlier or later than the current time. You can customize it

2. Common API

  • long getTime()
    • Returns the total number of milliseconds from Green's time to the present
  • void setTime(long time)
    • Set this date object to the point in time milliseconds after Green's time
public class demo {
    public static void main(String[] args) {
        //Create a Date of the current time
        Date date = new Date();
        //Date minus one day (24 hours * 60 minutes * 60 seconds * 1000 milliseconds)
        long time = date.getTime()-(24*60*60*1000);
        Date date2 = new Date(time);
        System.out.println(date2); //You'll find the date a day earlier

Tue Nov 02 20:36:18 CST 2021
Mon Nov 01 20:36:18 CST 2021

4, DateFormat class

1. Overview

  • Date formatting class. Note that this is an abstract class
  • To use this class, you need to pass the subclass SimpleDateFormat
  • The following mainly introduces the usage of the subclass SimpleDateFormat

2. Construction method of SimpleDateFormat

  • SimpleDateFormat("date format string")
  • Meaning of some letters in date format string (common)
    • y stands for year, M for month and d for day
    • H represents hour, m represents minute and s represents second

3. Common API

  • First, create an object of SimpleDateFormat, which is defined as format
  • format.format(Date date)
    • Formats the passed in date object into a time string in the specified format
    • The return value is Sring
  • format.parse(String time)
    • Converts the incoming string to a Date object
    • The String passed in must be in the same date format as when the SimpleDateFormat object was defined
    • The return value is Date

4. Application scenarios and examples

  • You can use some API s of SimpleDateFormat and Date to calculate how many days a person has lived since he was born
package SimpleDateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class demo {
    public static void main(String[] args) throws ParseException {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        //parse: converts a string of type yyyy MM DD HH: mm: SS to a Date object
        Date birthday = format.parse("2000-12-09 00:00:00");
        //Calculate days of birth
        System.out.println((new Date().getTime()-birthday.getTime())/1000/60/60/24);

5, Calendar Class

1. Overview

  • Calendar is a calendar class, which appears after the Date class, which solves the problem that the Date class cannot be internationalized
    • The source code of getInstance method will create the corresponding calendar class according to the default time zone (just learn about it)
  • The Calendar class is an abstract class. You cannot create an object through new, but you can get the Calendar object through the getInstance method
  • The date information is stored in the array, and the year, month, day and other information are encapsulated into different constants. Through these constants, you can access the subscript corresponding to the array, and the corresponding subscript stores the corresponding date information
  • be careful
    • The calendar month in the computer is 0-11, corresponding to 1-12 of the real life month
    • Sunday is the first day of a week

2. Common API

static Calendar getInstance()

  • Create a calendar class object using the default time zone and locale

int get (int field)

  • Returns the specified calendar field
    • The field passed in is the constant defined in the Calendar class or the number corresponding to the constant
    • Through the passed in parameters, you can access the corresponding lower flag to obtain the corresponding date information
      • For example, visit YEAR, MONTH, DAY, DAY_OF_YEAR, etc

void set (int field, int amount)

  • Sets the given calendar field to the specified value
    • The first parameter can be passed into a constant of the Calendar class or a corresponding number to represent a field of the date
    • amount indicates the value to be set for the date field
    • Note that the month is 0-11. The month you want to set - 1 is the real value

abstract void add (int field, int amount)

  • Add or delete the specified calendar field according to the rules of the calendar
    • Which field of the date does the first parameter refer to
    • The second number is the value to add or delete to the field
      • For example, add or delete 2 years to Calendar.YEAR
        • The first parameter is Calendar.YEAR
        • The second parameter is 2 or - 2

Date getTime()

  • Returns a Date object representing the time value of this Calendar

int getAutcualMaximum(int field)

  • Calculate the maximum value of the field for a given date
    • For example, if the maximum number of days in a month is required, the passed in parameter should be Calendar.DAY_OF_MONTH
      • Before that, you can use the set method to set the month as required. Note that the real month - 1 is set
      • Assuming that the month represents January, the result of calling this method is 31, that is, there are at most 31 days in January (the maximum value)

3. Examples

package Calendar;
import java.util.Calendar;
import java.util.Date;
public class demo {
    public static void main(String[] args) {
        //Create date class object
        Calendar calendar = Calendar.getInstance();
        //Get method: get the subscript value of the date array according to different constants
        int year = calendar.get(Calendar.YEAR); //Get year
        int month = calendar.get(Calendar.MONTH); //Get the month, the month is 0-11, and the value is the current month - 1
        int day_of_year = calendar.get(Calendar.DAY_OF_YEAR); //What day of the year is that
        System.out.println(day_of_year); //307
        System.out.println(year+"-"+month); //2021-10
        //set method: sets the value of the date represented by the index value of the array
        calendar.set(Calendar.YEAR,2022); //Set the year to 2022
        System.out.println(calendar.get(Calendar.YEAR)); //2022
        //add method: Date addition and subtraction method
        calendar.add(Calendar.YEAR,5); //Year + 5 years
        System.out.println(calendar.get(Calendar.YEAR)); //2022+5=2027
        calendar.add(Calendar.DATE,-1); //The date of mm / DD / yy - 1 day, originally 3
        System.out.println(calendar.get(Calendar.DAY_OF_MONTH)); // 3-1=2
        //getTime: get calendar time and return a Date object
        Date time = calendar.getTime();
        //getActualMaximum: gets the maximum value of the specified date
        calendar.set(Calendar.MONTH,0); //Set the month to January
        int max1 = calendar.getActualMaximum(Calendar.DAY_OF_MONTH); //Maximum number of days in January: 31
        int max2 = calendar.getActualMaximum(Calendar.DAY_OF_WEEK); // Maximum number of days in a week: 7

Tags: Java Back-end

Posted on Thu, 04 Nov 2021 03:10:02 -0400 by visualAd