[整理] java.util.Calendar

[整理] java.util.Calendar
1.Method_Add
/**/ /*
输出:
It is now 星期四 2006/08/17 at 05:55:57 下午 CST
Two years ago was 星期二 2004/08/17 at 05:55:57 下午 CST
*/

package  api.java.util.Calendar;

import  java.text.SimpleDateFormat;
import  java.util.Calendar;

public   class  Method_Add  {
    
public   static   void  main(String[] a)  throws  Exception  {

        Calendar now 
=  Calendar.getInstance();
        
        SimpleDateFormat formatter 
=   new  SimpleDateFormat(
                
" E yyyy/MM/dd 'at' hh:mm:ss a zzz " );
        System.out.println(
" It is now  "   +  formatter.format(now.getTime()));
        
/**/ /*
         * public abstract void add(int field, int
         * amount)根据日历的规则,为给定的日历字段添加或减去指定的时间量。
         
*/

        now.add(Calendar.YEAR, 
- 2 );
        System.out.println(
" Two years ago was  "
                
+  formatter.format(now.getTime()));
    }

}


主要经验值获得:
1. java.text.SimpleDateFormat 的用法
2. java.util.Calendar.add( ) 的用法,如何进行日期计算。

----------------------------------------------------------------------------------

2.Class_GregorianCalendar
/**/ /*
输出:
136 days remain in current year
*/

package  api.java.util.Calendar;

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

public   class  Class_GregorianCalendar  {
    
public static void main(String args[]) {

        Calendar calendar1 
= Calendar.getInstance();
        
int doy1 = calendar1.get(Calendar.DAY_OF_YEAR);

        
int year = calendar1.get(Calendar.YEAR);
        
        
//在具有默认语言环境的默认时区内构造一个带有给定日期设置的 GregorianCalendar
        Calendar calendar2 = new GregorianCalendar(year, 1131);
        
int doy2 = calendar2.get(Calendar.DAY_OF_YEAR);
        
        
//今年最后一天减去今天的剩余天数
        int days = doy2 - doy1;
        System.out.println(days 
+ " days remain in current year");
    }

}


主要经验值获得:
1. java.util.GregorianCalendar 的作用
2. 如何返回指定的日期的Calendar

次要经验值获得
1. 如何计算据今年结束的天数

----------------------------------------------------------------------------------

3.Class_Calendar
/**/ /*
输出:
ERA: 1
YEAR: 2006
MONTH: 7
WEEK_OF_YEAR: 33
WEEK_OF_MONTH: 3
DATE: 17
DAY_OF_MONTH: 17
DAY_OF_WEEK: 5
DAY_OF_WEEK_IN_MONTH: 3
AM_PM: 1
MILLISECOND: 92
ZONE_OFFSET: 8
DST_OFFSET: 0

*/

package  api.java.util.Calendar;

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

public   class  Class_Calendar  {
    
public static void main(String[] a) {
        Calendar calendar 
= new GregorianCalendar();
        calendar.setTime(
new Date());

        System.out.println(
"ERA: " + calendar.get(Calendar.ERA));
        System.out.println(
"YEAR: " + calendar.get(Calendar.YEAR));
        System.out.println(
"MONTH: " + calendar.get(Calendar.MONTH));
        System.out.println(
"WEEK_OF_YEAR: "
                
+ calendar.get(Calendar.WEEK_OF_YEAR));
        System.out.println(
"WEEK_OF_MONTH: "
                
+ calendar.get(Calendar.WEEK_OF_MONTH));
        System.out.println(
"DATE: " + calendar.get(Calendar.DATE));
        System.out.println(
"DAY_OF_MONTH: "
                
+ calendar.get(Calendar.DAY_OF_MONTH));
        System.out
                .println(
"DAY_OF_WEEK: " + calendar.get(Calendar.DAY_OF_WEEK));
        System.out.println(
"DAY_OF_WEEK_IN_MONTH: "
                
+ calendar.get(Calendar.DAY_OF_WEEK_IN_MONTH));
        System.out.println(
"AM_PM: " + calendar.get(Calendar.AM_PM));
        System.out
                .println(
"MILLISECOND: " + calendar.get(Calendar.MILLISECOND));
        System.out.println(
"ZONE_OFFSET: "
                
+ (calendar.get(Calendar.ZONE_OFFSET) / (60 * 60 * 1000)));
        System.out.println(
"DST_OFFSET: "
                
+ (calendar.get(Calendar.DST_OFFSET) / (60 * 60 * 1000)));
    }

}


主要经验值获得:
1. Calendar 的一些变量和方法用法

----------------------------------------------------------------------------------

4.

你可能感兴趣的:([整理] java.util.Calendar)