Read the Java Calendar class in one article

java.util.Calendar class: calendar class

Calendar time


Preface

Common member methods of the Calendar class:
1 public int get(int field): Returns the value of a given calendar field.

2 public void set(int field, int value): Set the given calendar field to the given value.

3 public abstract void add(int field, int amount): According to the calendar rules, add or subtract a specified amount of time for a given calendar field.

4 public Date getTime(): Returns a Date object representing the time value of this Calendar (millisecond offset from the epoch to the present).
The parameters of the member method:
int field: the field of the calendar class, you can use the static member variables of the Calendar class to get
public static final int YEAR = 1; year
public static final int MONTH = 2; month
public static final int DATE = 5; mid-month one day
public static final int DAY_OF_MONTH = 5; one day of the month
public static final int HOUR = 10; when
public static final int MINUTE = 12; points
public static final int sECOND = 13; sec

The Calendar class is an abstract class that provides many methods to manipulate calendar fields

(YEAR, MONTH, DAY_OF_MONTH, HOUR) The
Calendar class cannot be used to directly create objects. There is a static method called getInstance(),
which returns a subclass object of the Calendar class
static Calendar getInstance() Use the default time zone and locale to get one calendar.


Tip: The following is the content of this article, the following cases are for reference

A getInstance() uses the default time zone and locale to obtain a calendar.

   public static void main(String[] args) {
    
    
        Calendar c = Calendar.getInstance();
        System.out.println(c);//获取日期时间

    }
}

Two get(int field) returns the value of a given calendar field.

**public int get(int field):** returns the value of a given calendar field.

Parameters: Pass the specified calendar field (YEAR, MONTH...)

Return value: the specific value represented by the calendar field

``The code is as follows (example):

 private static void get() {
    
    
        //第一步使用getInstance方法获取Calendar对象
        Calendar c = Calendar.getInstance();
        //第二步传入需要参数并接收
        int year = c.get(Calendar.YEAR);//接收年
        System.out.println(year);

        int month = c.get(Calendar.MONTH);//接收月
        System.out.println(month);

        int date = c.get(Calendar.DATE);//接收月中某一天
        System.out.println(date);

        int DAY_MONTH = c.get(Calendar.DAY_OF_MONTH);//接收月中某一天
        System.out.println(DAY_MONTH);

    }

Three set(int field, int value): Set the given calendar field to the given value.

public void set(int field, int value) : Set the given calendar field to the given value.
Parameters:
int field : pass the specified calendar field (YEAR, MONTH...)
int value : set the value code for the specified field
as follows (example):

 private static void set()
    {
    
    
        //第一步使用getInstance方法获取Calendar对象
        Calendar c = Calendar.getInstance();
        //设置年为2000
        c.set(Calendar.YEAR,2000);
        //设置月为10月
        c.set(Calendar.MONTH,9);
        //设置日为20日
        c.set(Calendar.DATE,20);

        //同时设置年月日  可以使用set的重载方法
        c.set(2000,10,8);

        int month1 = c.get(Calendar.DATE);
        System.out.println(month1);//西方的月份0—11 东方:1——12

        int date=c.get(Calendar.DATE);
        System.out.println(date);
    }

Four add(int field, int amount): According to the rules of the calendar, add or subtract a specified amount of time for a given calendar field

Method 3:
**public abstract void add(int field, int amount):** According to the rules of the calendar, add or subtract the specified amount of time for a given calendar field.

Increase/decrease the specified value
parameter of the specified
field : int field : pass the specified calendar field (YEAR, MONTH...)
int amount : increase/decrease the specified value
Positive number: increase
Negative number: decrease The
code is as follows (example):

 private static void add()
    {
    
    
        //第一步使用getInstance方法获取Calendar对象
        Calendar c = Calendar.getInstance();
        //把年增加2年
        c.add(Calendar.YEAR,2);
        //把月份减少3个月
        c.add(Calendar.YEAR,-3);



        int year = c.get(Calendar.YEAR);
        System.out.println(year);

        int month = c.get(Calendar.MONTH);
        System.out.println(month);//西方的月份0-11 东方:1-12

        //int date = c.get(Calendar.DAY_OF_MONTH);
        int date = c.get(Calendar.DATE);
        System.out.println(date);

    }

Five getTime(): Returns a Date object representing the time value of this Calendar (millisecond offset from the epoch to the present).

The code for converting the calendar object into a date object
is as follows (example):

private static void Time()
    {
    
    
        //第一步使用getInstance方法获取Calendar对象
        Calendar c = Calendar.getInstance();

        Date date = c.getTime();
        System.out.println(date);

    }

to sum up

**Common member methods of the Calendar class:**

public int get(int field): Returns the value of a given calendar field.
public void set(int field, int value): Set the given calendar field to the given value.
public abstract void add(int field, int amount): According to the calendar rules, add or subtract a specified amount of time for a given calendar field.
public Date getTime(): Returns a Date object representing the time value of this Calendar (the millisecond offset from the epoch to the present).
The parameters of the member method:
int field: the field of the calendar class, which can be obtained using the static member variable of the Calendar class

        public static final int YEAR = 1;	年
        public static final int MONTH = 2;	月
        public static final int DATE = 5;	月中的某一天
        public static final int DAY_OF_MONTH = 5;月中的某一天
        public static final int HOUR = 10; 		时
        public static final int MINUTE = 12; 	分
        public static final int SECOND = 13;	秒

Guess you like

Origin blog.csdn.net/weixin_46235428/article/details/109269583