当前位置:网站首页>Common member methods of the calendar class

Common member methods of the calendar class

2022-06-24 20:57:00 Platonic

Calendar Class common member methods
void add(int field, int amount) For the given calendar field field Add or subtract the specified amount of time amount
int get(int field)int get(int field)
static Calendar getInstance()static Calendar getInstance()
Date getTime() Return a representation of this Calendar Time is worth Date object

Example :

public class Demo02Calendar {
    public static void main(String[] args) {
        //demo02();
       // demo01();
        //demo03();
        demo04();
    }
    /*public int get(int field): Returns the value of the given calendar field 
    *  Parameters : Pass the specified calendar field (YEar,MONTH..)
    * Return value : Calendar fields represent specific values 
    * */
    private static void demo01(){
    // Use getInstance Method to get Calendar() object 
    Calendar c=Calendar.getInstance();
        int year = c.get(Calendar.YEAR);
        int month = c.get(Calendar.MONTH)+1;
        int date= c.get(Calendar.DATE);
        System.out.println(year+" year ");
        System.out.println(month+" month ");
        System.out.println(date+" Japan ");
    }
    //public void set(int field,int value): Set the given calendar field to the given value 
    private static void demo02(){
        // Use getInstance Method to get Calendar() object 
        Calendar c=Calendar.getInstance();
        c.set(Calendar.YEAR,2021);
        c.set(Calendar.MONTH,2);
        c.set(Calendar.DATE,21);
        // You can also set the year, month and day at the same time 
    //    c.set(2021,2,21);
        System.out.println();
        int year = c.get(Calendar.YEAR);
        int month = c.get(Calendar.MONTH);
        int date= c.get(Calendar.DATE);
        System.out.println(year+" year ");
        System.out.println(month+" month ");
        System.out.println(date+" Japan ");

    }
    //public abstract void add(int field,int value): Add and subtract specific values ;
    private static void demo03(){
        // Use getInstance Method to get Calendar() object 
        Calendar c=Calendar.getInstance();
        c.add(Calendar.YEAR,2);
        c.add(Calendar.MONTH,2);
        c.add(Calendar.DATE,1);
        int year = c.get(Calendar.YEAR);
        int month = c.get(Calendar.MONTH);
        int date= c.get(Calendar.DATE);
        System.out.println(year+" year ");
        System.out.println(month+" month ");
        System.out.println(date+" Japan ");
    }
     public static void demo04(){
         // Use getInstance Method to get Calendar() object 
         Calendar c=Calendar.getInstance();
         Date time = c.getTime();
         System.out.println(time);
         System.out.println(time.toLocaleString());
     }
}

原网站

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