无线&移动互联网技术研发

换位思考·····
posts - 19, comments - 53, trackbacks - 0, articles - 283
  BlogJava :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理

Calendar 工具类 操作

Posted on 2009-07-22 23:49 Gavin.lee 阅读(435) 评论(0)  编辑  收藏 所属分类: Date tools

 

package com.yixun.wap;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.SimpleTimeZone;
import java.util.TimeZone;

import org.apache.log4j.Logger;

public class TestCalendar {

    
private static Logger logger = Logger.getLogger(TestCalendar.class);
    
public static void main(String[] args) {
        
/**
         * Calendar 是抽象类,
         
*/

        Calendar calendar 
= Calendar.getInstance();
//        Calendar calendar = new GregorianCalendar();    //Calendar 的一个子类
        
//        
        Date trialTime = new Date();
        calendar.setTime(trialTime);

        
// print out a bunch of interesting things
        logger.debug("ERA: " + calendar.get(Calendar.ERA));
        logger.debug(
"YEAR: " + calendar.get(Calendar.YEAR));
        logger.debug(
"MONTH: " + calendar.get(Calendar.MONTH));
        logger.debug(
"WEEK_OF_YEAR: " + calendar.get(Calendar.WEEK_OF_YEAR));
        logger.debug(
"WEEK_OF_MONTH: " + calendar.get(Calendar.WEEK_OF_MONTH));
        logger.debug(
"DATE: " + calendar.get(Calendar.DATE));
        logger.debug(
"DAY_OF_MONTH: " + calendar.get(Calendar.DAY_OF_MONTH));
        logger.debug(
"DAY_OF_YEAR: " + calendar.get(Calendar.DAY_OF_YEAR));
        logger.debug(
"DAY_OF_WEEK: " + calendar.get(Calendar.DAY_OF_WEEK));
        logger.debug(
"DAY_OF_WEEK_IN_MONTH: " + calendar.get(Calendar.DAY_OF_WEEK_IN_MONTH));
        logger.debug(
"AM_PM: " + calendar.get(Calendar.AM_PM));
        logger.debug(
"HOUR: " + calendar.get(Calendar.HOUR));
        logger.debug(
"HOUR_OF_DAY: " + calendar.get(Calendar.HOUR_OF_DAY));
        logger.debug(
"MINUTE: " + calendar.get(Calendar.MINUTE));
        logger.debug(
"SECOND: " + calendar.get(Calendar.SECOND));
        logger.debug(
"MILLISECOND: " + calendar.get(Calendar.MILLISECOND));
        logger.debug(
"ZONE_OFFSET: " + (calendar.get(Calendar.ZONE_OFFSET) / (60 * 60 * 1000))); // in
                                                                            
// hours
        logger.debug("DST_OFFSET: "    + (calendar.get(Calendar.DST_OFFSET) / (60 * 60 * 1000))); // in hours

        logger.debug(
"Date:" + calendar.getTime());
        logger.debug(
"getTimeInMillis:" + calendar.getTimeInMillis());
        
        SimpleDateFormat sdf 
= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        
        Date date 
= null;
        
try {
            date 
= sdf.parse("1986-6-3 12:30:00");
        }
 catch (ParseException e) {
            e.printStackTrace();
        }

        
        calendar.setTime(date);
        
        calendar.setTimeInMillis(
100000*1000);
        
        
long times = calendar.getTimeInMillis();
        logger.debug(sdf.format(times));
        logger.debug(sdf.format(calendar.getTime()));
        
        calendar.set(
198663);
        logger.debug(sdf.format(calendar.getTime()));
        
        calendar.set(
198663101010);
        logger.debug(sdf.format(calendar.getTime()));
        
        logger.debug(calendar.getTimeZone());
        logger.debug(calendar);
    }


}


//[DEBUG  0 ] ->  -content:ERA: 1
//[DEBUG 16 ] ->  -content:YEAR: 2009
//[DEBUG 16 ] ->  -content:MONTH: 6
//[DEBUG 16 ] ->  -content:WEEK_OF_YEAR: 30
//[DEBUG 16 ] ->  -content:WEEK_OF_MONTH: 4
//[DEBUG 16 ] ->  -content:DATE: 23
//[DEBUG 16 ] ->  -content:DAY_OF_MONTH: 23
//[DEBUG 16 ] ->  -content:DAY_OF_YEAR: 204
//[DEBUG 16 ] ->  -content:DAY_OF_WEEK: 5
//[DEBUG 16 ] ->  -content:DAY_OF_WEEK_IN_MONTH: 4
//[DEBUG 16 ] ->  -content:AM_PM: 1
//[DEBUG 16 ] ->  -content:HOUR: 1
//[DEBUG 16 ] ->  -content:HOUR_OF_DAY: 13
//[DEBUG 16 ] ->  -content:MINUTE: 31
//[DEBUG 16 ] ->  -content:SECOND: 14
//[DEBUG 16 ] ->  -content:MILLISECOND: 312
//[DEBUG 16 ] ->  -content:ZONE_OFFSET: 0
//[DEBUG 16 ] ->  -content:DST_OFFSET: 0
//[DEBUG 31 ] ->  -content:Date:Thu Jul 23 13:31:14 GMT 2009
//[DEBUG 31 ] ->  -content:getTimeInMillis:1248355874312
//[DEBUG 31 ] ->  -content:1970-01-02 03:46:40
//[DEBUG 31 ] ->  -content:1970-01-02 03:46:40
//[DEBUG 31 ] ->  -content:1986-07-03 03:46:40
//[DEBUG 31 ] ->  -content:1986-07-03 10:10:10
//[DEBUG 31 ] ->  -content:sun.util.calendar.ZoneInfo[id="GMT",offset=0,dstSavings=0,useDaylight=false,transitions=0,lastRule=null]
//[DEBUG 31 ] ->  -content:java.util.GregorianCalendar[time=520769410000,areFieldsSet=true,areAllFieldsSet=true,lenient=true,zone=sun.util.calendar.ZoneInfo[id="GMT",offset=0,dstSavings=0,useDaylight=false,transitions=0,lastRule=null],firstDayOfWeek=1,minimalDaysInFirstWeek=1,ERA=1,YEAR=1986,MONTH=6,WEEK_OF_YEAR=27,WEEK_OF_MONTH=1,DAY_OF_MONTH=3,DAY_OF_YEAR=184,DAY_OF_WEEK=5,DAY_OF_WEEK_IN_MONTH=1,AM_PM=0,HOUR=10,HOUR_OF_DAY=10,MINUTE=10,SECOND=10,MILLISECOND=0,ZONE_OFFSET=0,DST_OFFSET=0]



只有注册用户登录后才能发表评论。


网站导航: