Date and GregorianCalendar in Java

逝者如斯夫,不舍昼夜。

——《论语》

Date, Calendar and GregorianCalendar

In the first place, the signature of java.util.Date is like this.

public class Date
extends Object
implements Serilizable, Cloneable, Comparable<Date>

Now let's see an example.

import java.util.GregorianCalendar;
import java.util.Calendar;
import java.util.Date;

public class JavaApplication {
    public static void main(String[] args) {
        Date today = new Date();
        System.out.println("Now it is " + today);
        // Deprecated
        Date bachelor = new Date(1111, 10, 11, 11, 11, 11);
        System.out.println("The special day for the single ones is " + bachelor);
        // Note 10 is for November!
        Date bachelor2 = new GregorianCalendar(1111, 10, 11, 11, 11, 11).getTime();
        System.out.println("The special day for the single ones is " + bachelor2);
    }
}

According to the official Java document, a lot of methods of Date is deprecated and the corresponding functionality is moved to java.util.Calendar and java.util.GregorianCalendar. Also be careful to notice that Date(year, month, day) is GregorianCalendar(1900+year, month, day).

Before we go to GregorianCalendar, let's see Calendar first.

Calendar is an abstract class.

public abstract class Calendar
extends Object
implements Serializable, Cloneable, Comparable<Calendar>

Now a code snippet illustrating how to use Calendar:

import java.util.GregorianCalendar;
import java.util.Calendar;
import java.util.Date;

public class JavaApplication {
    public static void main(String[] args) {
        Calendar now = Calendar.getInstance();
        System.out.println("Now it is " + now);
        System.out.println("Now it is " + now.getTime());
        now.roll(Calendar.YEAR, 1);
        System.out.println("After one year, it will be " + now.getTime());
        
        Calendar demise = Calendar.getInstance();
        demise.clear();
        System.out.println("This is when the time begins: " + demise.getTime());
        demise.set(2012, 0, 1, 0, 0, 0);
        System.out.println("This is when the earth is supposed to be crashed: " + demise.getTime());
    }
}

Output:

Now it is java.util.GregorianCalendar[time=1455351336050,areFieldsSet=true,areAllFieldsSet=true,lenient=true,zone=sun.util.calendar.ZoneInfo[id="Asia/Shanghai",offset=28800000,dstSavings=0,useDaylight=false,transitions=19,lastRule=null],firstDayOfWeek=1,minimalDaysInFirstWeek=1,ERA=1,YEAR=2016,MONTH=1,WEEK_OF_YEAR=7,WEEK_OF_MONTH=2,DAY_OF_MONTH=13,DAY_OF_YEAR=44,DAY_OF_WEEK=7,DAY_OF_WEEK_IN_MONTH=2,AM_PM=1,HOUR=4,HOUR_OF_DAY=16,MINUTE=15,SECOND=36,MILLISECOND=50,ZONE_OFFSET=28800000,DST_OFFSET=0]
Now it is Sat Feb 13 16:15:36 CST 2016
After one year, it will be Mon Feb 13 16:15:36 CST 2017
This it when the time begins: Thu Jan 01 00:00:00 CST 1970
This is when the earth is supposed to be crashed: Sun Jan 01 00:00:00 CST 2012

The first line is not java.util.Calendar but rather java.uril.GregorianCalendar because as you know Calendar is an abstract class which can not be instantiated.

Timing

import java.util.GregorianCalendar;
import java.util.Calendar;
import java.util.Date;

class SimpleTimer {
    private long time;
    
    public void tic() {
        time = System.nanoTime();
    }
    
    // return time in milli-seconds
    public double toc() {
        return (double)(System.nanoTime() - time)/1e6;
    }
}

public class JavaApplication {
    public static void main(String[] args) {
        SimpleTimer timer = new SimpleTimer();
        timer.tic();
        try {
            Thread.sleep(1000);
        } catch(Exception e) {
            System.out.println("Exception in Thread.sleep()");
        }
        System.out.println("Time consumed is " + timer.toc());
    }
}

You can also use System.getTimeMillis() to acquire time. The difference is minor.

你可能感兴趣的:(java,calendar)