2009年8月7日金曜日

Java中的日期和时间

1. 使用Date:
使用System.currentTimeMillis()方法可以取得系统的时间,是从1970年1月1日0时0分0秒到当前系统时间的毫秒数。当使用Date对象时,会使用System.currentTimeMillis()来取得系统时间,使用Date实例的toString()方法时,会将取得的1970年1月1日0时0分0秒至今的毫秒数转为“星期 月 日 时:分:秒 公元“的格式;使用Date的getTime()方法则可以取得实际经过的毫秒数。如下例:

Java代码
package ysu.hxy;

import java.util.Date;

public class DateDemo {
public static void main(String[] args) {
Date date = new Date();

System.out.println("现在时间 "
+ date.toString());
System.out.println("自1970/1/1至今的毫秒数 "
+ date.getTime());

}
}

package ysu.hxy;

import java.util.Date;

public class DateDemo {
public static void main(String[] args) {
Date date = new Date();

System.out.println("现在时间 "
+ date.toString());
System.out.println("自1970/1/1至今的毫秒数 "
+ date.getTime());

}
}执行结果:

D:\hxy>java ysu.hxy.DateDemo
现在时间 Tue Apr 07 10:56:59 CST 2009
自1970/1/1至今的毫秒数 1239073019187
2.如果要对日期做格式设定可以使用java.text.DateFormat来做格式化。先来看看DateFormat的子类java.text.SimpleDateFormat是如何使用的。范例如下:

Java代码
package ysu.hxy;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

public class DateFormatDemo
{
public static void main(String[] args)
{
Date date = new Date();

DateFormat dateFormat =
new SimpleDateFormat("EE-MM-dd-yyyy");

System.out.println(dateFormat.format(date));
}
}

package ysu.hxy;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

public class DateFormatDemo
{
public static void main(String[] args)
{
Date date = new Date();

DateFormat dateFormat =
new SimpleDateFormat("EE-MM-dd-yyyy");

System.out.println(dateFormat.format(date));
}
}


执行结果:D:\hxy>java ysu.hxy.DateFormatDemo
星期二-04-07-2009

3.也可以直接使用DateFormat上的静态getDateTimeInstance()方法来指定格式,getDateTimeInstance()方法会返回DateFormat的实例,之后可以重复使用这个实例来格式化Date对象。此方法的两个参数分别表示日期格式与时间格式,如下范例:



Java代码
package ysu.hxy;

import java.text.DateFormat;
import java.util.Date;

public class DateTimeInstanceDemo
{
public static void main(String[] args)
{
//取得当前时间
Date date = new Date();

//简短消息格式
DateFormat shortFormat =
DateFormat.getDateTimeInstance(
DateFormat.SHORT,DateFormat.SHORT);
//中等消息格式
DateFormat mediumFormat =
DateFormat.getDateTimeInstance(
DateFormat.MEDIUM,DateFormat.MEDIUM);
//长信息格式
DateFormat longFormat =
DateFormat.getDateTimeInstance(
DateFormat.LONG,DateFormat.LONG);
//详细信息格式
DateFormat fullFormat =
DateFormat.getDateTimeInstance(
DateFormat.FULL,DateFormat.FULL);

System.out.println("简短信息格式:"+shortFormat.format(date));
System.out.println("中等信息格式:"+mediumFormat.format(date));
System.out.println("长信息格式:"+longFormat.format(date));
System.out.println("详细信息格式:"+fullFormat.format(date));
}
}

package ysu.hxy;

import java.text.DateFormat;
import java.util.Date;

public class DateTimeInstanceDemo
{
public static void main(String[] args)
{
//取得当前时间
Date date = new Date();

//简短消息格式
DateFormat shortFormat =
DateFormat.getDateTimeInstance(
DateFormat.SHORT,DateFormat.SHORT);
//中等消息格式
DateFormat mediumFormat =
DateFormat.getDateTimeInstance(
DateFormat.MEDIUM,DateFormat.MEDIUM);
//长信息格式
DateFormat longFormat =
DateFormat.getDateTimeInstance(
DateFormat.LONG,DateFormat.LONG);
//详细信息格式
DateFormat fullFormat =
DateFormat.getDateTimeInstance(
DateFormat.FULL,DateFormat.FULL);

System.out.println("简短信息格式:"+shortFormat.format(date));
System.out.println("中等信息格式:"+mediumFormat.format(date));
System.out.println("长信息格式:"+longFormat.format(date));
System.out.println("详细信息格式:"+fullFormat.format(date));
}
}4. 也可以使用getDateTimeInstance()取得DateFormat实例,并同时指定日期的区域显示方式,指定时要使用一个java.util.Locale实例作为参数。如下示例将按美国的时间表示方式显示系统时间:

Java代码
package ysu.hxy;

import java.text.DateFormat;
import java.util.Date;
import java.util.Locale;

public class DateTimeInstanceDemo2 {
public static void main(String[] args) {
// 取得目前时间
Date date = new Date();

// en: 英语系 US: 美国
Locale locale = new Locale("en", "US");

// 简短信息格式
DateFormat shortFormat =
DateFormat.getDateTimeInstance(
DateFormat.SHORT, DateFormat.SHORT, locale);
// 中等信息格式
DateFormat mediumFormat =
DateFormat.getDateTimeInstance(
DateFormat.MEDIUM, DateFormat.MEDIUM, locale);
// 长信息格式
DateFormat longFormat =
DateFormat.getDateTimeInstance(
DateFormat.LONG, DateFormat.LONG, locale);
// 详细信息格式

DateFormat fullFormat =
DateFormat.getDateTimeInstance(
DateFormat.FULL, DateFormat.FULL, locale);

System.out.println("short format: " +
shortFormat.format(date));
System.out.println("media format: " +
mediumFormat.format(date));
System.out.println("long format: " +
longFormat.format(date));
System.out.println("full format: " +
fullFormat.format(date));
}
}

package ysu.hxy;

import java.text.DateFormat;
import java.util.Date;
import java.util.Locale;

public class DateTimeInstanceDemo2 {
public static void main(String[] args) {
// 取得目前时间
Date date = new Date();

// en: 英语系 US: 美国
Locale locale = new Locale("en", "US");

// 简短信息格式
DateFormat shortFormat =
DateFormat.getDateTimeInstance(
DateFormat.SHORT, DateFormat.SHORT, locale);
// 中等信息格式
DateFormat mediumFormat =
DateFormat.getDateTimeInstance(
DateFormat.MEDIUM, DateFormat.MEDIUM, locale);
// 长信息格式
DateFormat longFormat =
DateFormat.getDateTimeInstance(
DateFormat.LONG, DateFormat.LONG, locale);
// 详细信息格式

DateFormat fullFormat =
DateFormat.getDateTimeInstance(
DateFormat.FULL, DateFormat.FULL, locale);

System.out.println("short format: " +
shortFormat.format(date));
System.out.println("media format: " +
mediumFormat.format(date));
System.out.println("long format: " +
longFormat.format(date));
System.out.println("full format: " +
fullFormat.format(date));
}
}运行结果:

D:\hxy>java ysu.hxy.DateTimeInstanceDemo2
short format: 4/7/09 11:25 AM
media format: Apr 7, 2009 11:25:01 AM
long format: April 7, 2009 11:25:01 AM CST
full format: Tuesday, April 7, 2009 11:25:01 AM CST

5.Calendar类:

可以使用Date来取得完整的日期时间并使用toString()显示日期的文字描述,但如果要单独取得某个时间或日期信息该如何进行?可以使用java.util.Calendar类,Calendar的一些方法会取回int类型数据,而取回的数据其实是对应于Calendar中定义的常数。

如果要取得现在的时间,首先要使用Calendar的getInstance()方法取得一个Calendar的实例。例如:

Java代码
Calendar rightNow = Calendar.getInstance();

Calendar rightNow = Calendar.getInstance(); 一个Calendar的实例是系统时间的抽象表示,您可以操作这个实例上的get()方法来取得您想要知道的时间信息。例如您想知道现在是公元几年,则可以使用get()方法并指定对应的常数。例如:

Java代码
System.out.println(rightNow.get(Calendar.YEAR));

System.out.println(rightNow.get(Calendar.YEAR)); 要想取得现在的月份可以使用:

Java代码
System.out.println(rightNow.get(Calendar.MONTH));

System.out.println(rightNow.get(Calendar.MONTH)); 如果当前月份是4,则这条语句会输出3。这是对于于Calendar.April的值,并不代表当前月份是3月份。Calendar在月份上的常数值从一月Calendar.JANUARY开始是0,到十二月Calendar.DECEMBER的11,所以如果想要显示返回值的真正意义,可以如下编写:

Java代码
String[] months = {"一月","二月","三月","四月","五月","六月","七月","八月","九月","十月","十一月","十二月"};
Calendar rightNow = Calendar.getInstance();
int monthConstant = rightNow.get(Calendar.MONTH);
System.out.println(months[monthConstant]);

String[] months = {"一月","二月","三月","四月","五月","六月","七月","八月","九月","十月","十一月","十二月"};
Calendar rightNow = Calendar.getInstance();
int monthConstant = rightNow.get(Calendar.MONTH);
System.out.println(months[monthConstant]); 同样地,如果要取得星期信息,则要记得Calendar的星期常数是从星期日Calendar.SUNDAY是1,到星期六确Calendar.SATURDAY是7。由于对应的数并不是从0开始,所以如果要使用数组来对应,第一个数组的元素值就不包括信息。例如:

Java代码
String[] dayOfWeek = {"","日","一","二","三","四","五","六"};
Calendar rightNow = Calendar.getInstance();
int dayOfWeekConstant = rightNow.get(Calendar.DAY_OF_WEEK);
System.out.println(dayOfWeek[dayOfWeekConstant]);

String[] dayOfWeek = {"","日","一","二","三","四","五","六"};
Calendar rightNow = Calendar.getInstance();
int dayOfWeekConstant = rightNow.get(Calendar.DAY_OF_WEEK);
System.out.println(dayOfWeek[dayOfWeekConstant]);总之要记得get()返回的int值是对应于Calendar的某个常数,如果怕搞混,使用switch来比对是一个不错的做法。如下例所示:

Java代码
package ysu.hxy;

import java.util.Calendar;

public class CalendarDemo
{
public static void main(String[] args)
{
Calendar rightNow = Calendar.getInstance();

System.out.println("现在时间是:");
System.out.println("公元:"+rightNow.get(Calendar.YEAR));

System.out.println("月:"+getChineseMonth(rightNow));
System.out.println("日:"+rightNow.get(Calendar.DAY_OF_MONTH));
System.out.println("星期:"+getChineseDayOfWeek(rightNow));
//System.out.println(rightNow.get(Calendar.DAY_OF_WEEK));
//System.out.println(rightNow.get(Calendar.MONTH));
}

public static String getChineseMonth(Calendar rightNow)
{
String chineseMonth = null;

switch(rightNow.get(Calendar.MONTH))
{
case Calendar.JANUARY:
chineseMonth = "一";
break;
case Calendar.FEBRUARY:
chineseMonth = "二";
break;
case Calendar.MARCH:
chineseMonth = "三";
break;
case Calendar.APRIL:
chineseMonth = "四";
break;
case Calendar.MAY:
chineseMonth = "五";
break;
case Calendar.JUNE:
chineseMonth = "六";
break;
case Calendar.JULY:
chineseMonth = "七";
break;
case Calendar.AUGUST:
chineseMonth = "八";
break;
case Calendar.SEPTEMBER:
chineseMonth = "九";
break;
case Calendar.OCTOBER:
chineseMonth = "十";
break;
case Calendar.NOVEMBER:
chineseMonth = "十一";
break;
case Calendar.DECEMBER:
chineseMonth = "十二";
break;
}

return chineseMonth;
}

public static String getChineseDayOfWeek(Calendar rightNow)
{
String chineseDayOfWeek = null;

switch(rightNow.get(Calendar.DAY_OF_WEEK))
{
case Calendar.SUNDAY:
chineseDayOfWeek = "日";
break;
case Calendar.MONDAY:
chineseDayOfWeek = "一";
break;
case Calendar.TUESDAY:
chineseDayOfWeek = "二";
break;
case Calendar.WEDNESDAY:
chineseDayOfWeek = "三";
break;
case Calendar.THURSDAY:
chineseDayOfWeek = "四";
break;
case Calendar.FRIDAY:
chineseDayOfWeek = "五";
break;
case Calendar.SATURDAY:
chineseDayOfWeek = "六";
break;
}
return chineseDayOfWeek;
}
}

package ysu.hxy;

import java.util.Calendar;

public class CalendarDemo
{
public static void main(String[] args)
{
Calendar rightNow = Calendar.getInstance();

System.out.println("现在时间是:");
System.out.println("公元:"+rightNow.get(Calendar.YEAR));

System.out.println("月:"+getChineseMonth(rightNow));
System.out.println("日:"+rightNow.get(Calendar.DAY_OF_MONTH));
System.out.println("星期:"+getChineseDayOfWeek(rightNow));
//System.out.println(rightNow.get(Calendar.DAY_OF_WEEK));
//System.out.println(rightNow.get(Calendar.MONTH));
}

public static String getChineseMonth(Calendar rightNow)
{
String chineseMonth = null;

switch(rightNow.get(Calendar.MONTH))
{
case Calendar.JANUARY:
chineseMonth = "一";
break;
case Calendar.FEBRUARY:
chineseMonth = "二";
break;
case Calendar.MARCH:
chineseMonth = "三";
break;
case Calendar.APRIL:
chineseMonth = "四";
break;
case Calendar.MAY:
chineseMonth = "五";
break;
case Calendar.JUNE:
chineseMonth = "六";
break;
case Calendar.JULY:
chineseMonth = "七";
break;
case Calendar.AUGUST:
chineseMonth = "八";
break;
case Calendar.SEPTEMBER:
chineseMonth = "九";
break;
case Calendar.OCTOBER:
chineseMonth = "十";
break;
case Calendar.NOVEMBER:
chineseMonth = "十一";
break;
case Calendar.DECEMBER:
chineseMonth = "十二";
break;
}

return chineseMonth;
}

public static String getChineseDayOfWeek(Calendar rightNow)
{
String chineseDayOfWeek = null;

switch(rightNow.get(Calendar.DAY_OF_WEEK))
{
case Calendar.SUNDAY:
chineseDayOfWeek = "日";
break;
case Calendar.MONDAY:
chineseDayOfWeek = "一";
break;
case Calendar.TUESDAY:
chineseDayOfWeek = "二";
break;
case Calendar.WEDNESDAY:
chineseDayOfWeek = "三";
break;
case Calendar.THURSDAY:
chineseDayOfWeek = "四";
break;
case Calendar.FRIDAY:
chineseDayOfWeek = "五";
break;
case Calendar.SATURDAY:
chineseDayOfWeek = "六";
break;
}
return chineseDayOfWeek;
}
} 运行结果:

D:\Java_Test>java ysu.hxy.CalendarDemo
现在时间是:
公元:2009
月:四
日:7
星期:二

0 件のコメント: