首页 > 代码库 > Java Calendar

Java Calendar

package com.qhong;import java.text.DateFormat;import java.text.SimpleDateFormat;import java.util.Calendar;import java.util.Date;public class Main {    public static void main(String[] args) throws Exception {        DateFormat df=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS");        System.out.println(df.format(new Date()));        Calendar calendar=Calendar.getInstance();        Date dt=calendar.getTime();        System.out.println(df.format(dt));        System.out.println(calendar.getWeekYear());        System.out.println(calendar.getFirstDayOfWeek());        System.out.println(calendar.getWeeksInWeekYear());        System.out.println("====================");        System.out.println(calendar.get(Calendar.YEAR));        System.out.println(calendar.get(Calendar.MONTH));        System.out.println(calendar.get(Calendar.DAY_OF_MONTH));        System.out.println(calendar.get(Calendar.DAY_OF_WEEK));        System.out.println(calendar.get(Calendar.DAY_OF_WEEK_IN_MONTH));        System.out.println(calendar.get(Calendar.DAY_OF_YEAR));        System.out.println("====================");        System.out.println(calendar.get(Calendar.DATE));        System.out.println(calendar.get(Calendar.HOUR));        System.out.println(calendar.get(Calendar.HOUR_OF_DAY));        System.out.println(calendar.get(Calendar.MINUTE));        System.out.println(calendar.get(Calendar.SECOND));        System.out.println("====================");        System.out.println(calendar.get(Calendar.MILLISECOND));        Calendar NewCalendar=Calendar.getInstance();        NewCalendar.setTime(new Date());        System.out.println(df.format(NewCalendar.getTime()));        NewCalendar.add(Calendar.YEAR,-1);        System.out.println(df.format(NewCalendar.getTime()));        NewCalendar.add(Calendar.MONTH,2);        System.out.println(df.format(NewCalendar.getTime()));        NewCalendar.add(Calendar.DATE,3);        //后者大,显示 -1          System.out.println(df.format(NewCalendar.getTime()));        System.out.println("时间比较:"+NewCalendar.compareTo(calendar));        //前者大,显示 1        NewCalendar.add(Calendar.YEAR,1);        System.out.println("时间比较:"+NewCalendar.compareTo(calendar));    }}
2017-03-13 22:36:08:3932017-03-13 22:36:08:3942017152====================20172132272====================131022368====================3942017-03-13 22:36:08:3942016-03-13 22:36:08:3942016-05-13 22:36:08:3942016-05-16 22:36:08:394时间比较:-1时间比较:1

 

http://blog.csdn.net/joyous/article/details/9630893

http://sunnyhl.iteye.com/blog/1205820

Java Calendar