当前位置:网站首页>Calendar time operation

Calendar time operation

2022-06-09 02:37:00 Gentle ~

calendar Is a variable Date object , Once modified, the time represented by its object itself will change .

import javax.xml.crypto.Data;
import java.util.Calendar;
import java.util.Date;

/**  The goal is : Calendar class Calendar Use , You can get more information . Calendar Represents the calendar object corresponding to the current date of the system . Calendar Is an abstract class , You can't create objects directly . Calendar Syntax for calendar class to create calendar objects : Calendar rightNow = Calendar.getInstance(); Calendar Methods : 1.public static Calendar getInstance():  Return an object of calendar class . 2.public int get(int field): Get the information of a field in the date . 3.public void set(int field,int value): Modify a field information of the calendar . 4.public void add(int field,int amount): Add... To a field / Decrease the specified value  5.public final Date getTime():  Get the current date object . 6.public long getTimeInMillis():  Get the current time in milliseconds   Summary :  remember . */
public class CalendarDemo{
    
    public static void main(String[] args) {
    
        // 1、 Get the calendar object of the system at the moment 
        Calendar cal = Calendar.getInstance();
        System.out.println(cal);

        // 2、 Get calendar information :public int get(int field): Get the information of a field in the date .
        int year = cal.get(Calendar.YEAR);
        System.out.println(year);

        int mm = cal.get(Calendar.MONTH) + 1;
        System.out.println(mm);

        int days = cal.get(Calendar.DAY_OF_YEAR) ;
        System.out.println(days);

        // 3、public void set(int field,int value): Modify a field information of the calendar .
        // cal.set(Calendar.HOUR , 12);
        // System.out.println(cal);

        // 4.public void add(int field,int amount): Add... To a field / Decrease the specified value 
        //  Excuse me, 64 What time is the day after 
        cal.add(Calendar.DAY_OF_YEAR , 64);
        cal.add(Calendar.MINUTE , 59);

        // 5.public final Date getTime():  Get the current date object .
        Date d = cal.getTime();
        System.out.println(d);

        // 6.public long getTimeInMillis():  Get the current time in milliseconds 
        long time = cal.getTimeInMillis();
        System.out.println(time);

    }
}
原网站

版权声明
本文为[Gentle ~]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/159/202206081211552645.html