JAVA [Time]

来源:互联网 发布:免费动画制作软件 编辑:程序博客网 时间:2024/06/03 05:54
/* 
* Date
*/ 

Date now = new Date();
System.out.println("Now " + now);
System.out.println("Now.getTime " + now.getTime());
System.out.println("Now.getDate " + now.getDate());
System.out.println("Now.getDay " + now.getDay());
System.out.println("Now.getHours " + now.getHours());
System.out.println("Now.toLocaleString " + now.toLocaleString());
System.out.println("Now.toGMTString " + now.toGMTString());
System.out.println("Now.toString " + now.toString());

/*
* Calendar
*/

Calendar calendar = Calendar.getInstance();
if (calendar instanceof GregorianCalendar)
System.out.println("It is an instance of GregorianCalendar");

// 从一个 Calendar 对象中获取 Date 对象
Date date = calendar.getTime();
// 将 Date 对象反应到一个 Calendar 对象中,
// Calendar/GregorianCalendar 没有构造函数可以接受 Date 对象
// 所以我们必需先获得一个实例,然后设置 Date 对象

calendar.setTime(date);
//add() 有两条规则:当被修改的字段超出它可以的范围时,那么比它大的字段会自动修正
//如果比它小的字段是不可变的(由 Calendar 的实现类决定),那么该小字段会修正到变化最小的值

calendar.set(2000, 7, 31, 0, 0 , 0); //2000-8-31
calendar.add(Calendar.MONTH, 1); //2000-9-31 => 2000-10-1,对吗?
System.out.println(calendar); //结果是 2000-9-30
calendar.set(1999, 5, 6, 0, 0, 0); //1999-6-6, 周日
calendar.add(Calendar.WEEK_OF_MONTH, -1); //1999-5-30, 周日
System.out.println(calendar); //结果是 2000-5-30

/*
* Format
* 日期格式化:第一个参数是日期风格, 而第二个参数是时间风格. 它们都是基本数据类型int(整型).?
*/

DateFormat shortDateFormat = DateFormat.getDateTimeInstance(DateFormat.SHORT,DateFormat.SHORT);
DateFormat mediumDateFormat = DateFormat.getDateTimeInstance(DateFormat.MEDIUM,DateFormat.MEDIUM);
DateFormat longDateFormat =DateFormat.getDateTimeInstance(DateFormat.LONG,DateFormat.LONG);
DateFormat fullDateFormat =DateFormat.getDateTimeInstance(DateFormat.FULL,DateFormat.FULL);

System.out.println(shortDateFormat.format(date));
System.out.println(mediumDateFormat.format(date));
System.out.println(longDateFormat.format(date));
System.out.println(fullDateFormat.format(date));


// Create a date formatter that can parse dates of the form MM-dd-yyyy.
SimpleDateFormat bartDateFormat = new SimpleDateFormat("MM-dd-yyyy");
// Create a string containing a text date to be parsed.
String dateStringToParse = "9-29-2001";
try {
// Parse the text version of the date.
// We have to perform the parse method in a
// try-catch construct in case dateStringToParse
// does not contain a date in the format we are expecting.

Date d = bartDateFormat.parse(dateStringToParse);
// Now send the parsed date as a long value to the system output.
System.out.println(bartDateFormat.format(d));
}
catch (Exception ex) {
System.out.println(ex.getMessage());
}

/*
* Java.sql.Time 
* Java.sql.Date
*/

java.sql.Time t = new java.sql.Time(System.currentTimeMillis());

Timestamp time = new Timestamp(System.currentTimeMillis());
System.out.println(t);

System.out.print(new java.sql.Date(System.currentTimeMillis()));
System.out.println(time);


**************************************************************************************************************

Now Tue Jul 26 22:29:31 CST 2011
Now.getTime 1311690571078
Now.getDate 26
Now.getDay 2
Now.getHours 22
Now.toLocaleString 2011-7-26 22:29:31
Now.toGMTString 26 Jul 2011 14:29:31 GMT
Now.toString Tue Jul 26 22:29:31 CST 2011
It is an instance of GregorianCalendar
java.util.GregorianCalendar[time=?,areFieldsSet=false,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=2000,MONTH=8,WEEK_OF_YEAR=36,WEEK_OF_MONTH=5,DAY_OF_MONTH=30,DAY_OF_YEAR=244,DAY_OF_WEEK=5,DAY_OF_WEEK_IN_MONTH=5,AM_PM=0,HOUR=0,HOUR_OF_DAY=0,MINUTE=0,SECOND=0,MILLISECOND=781,ZONE_OFFSET=28800000,DST_OFFSET=0]
java.util.GregorianCalendar[time=927993600781,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=1999,MONTH=4,WEEK_OF_YEAR=23,WEEK_OF_MONTH=6,DAY_OF_MONTH=30,DAY_OF_YEAR=150,DAY_OF_WEEK=1,DAY_OF_WEEK_IN_MONTH=5,AM_PM=0,HOUR=0,HOUR_OF_DAY=0,MINUTE=0,SECOND=0,MILLISECOND=781,ZONE_OFFSET=28800000,DST_OFFSET=0]
11-7-26 下午10:29
2011-7-26 22:29:31
2011年7月26日 下午10时29分31秒
2011年7月26日 星期二 下午10时29分31秒 CST
09-29-2001
22:29:31
2011-07-26
2011-07-26  22:29:31


****************************************************************************

    /**
     * java.util.TimeZone
     */

    public static void TimeZoneTest(){
        TimeZone tz = TimeZone.getDefault();
        System.out.println(tz);
        System.out.println(tz.getDisplayName());
        System.out.println(tz.getID());
    }

原创粉丝点击