Date、Calendar

来源:互联网 发布:手机考勤软件排行 编辑:程序博客网 时间:2024/05/10 07:37
Date(日期)
Date类封装当前的日期和时间。
// Show date and time using only Date methods.
import java.util.Date;

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

// display time and date using toString()
System.out.println(date);

// Display number of milliseconds since midnight, January 1, 1970 GMT
long msec = date.getTime();
System.out.println("Milliseconds since Jan. 1, 1970 GMT = " + msec);

}
D:\javafile\lang>java DateDemo
Thu Dec 25 09:58:51 CST 2008
Milliseconds since Jan. 1, 1970 GMT = 1230170331109

比较日期
有三种方法可用于比较两个Date对象。首先,可以对两个对象使用getTime( )方法获得
它们各自自1970年1月1日午夜起至今的毫秒数的大小。然后比较这两个值的大小。其次,
可以使用before( ),after( )以及equals( )方法。例如,由于每个月的12号出现在18号之前,
所以new Date(99, 2, 12).before(new Date (99, 2, 18))将返回true。最后,可以使用由Comparable
接口定义,被Date实现的compareTo( )方法。


Calendar(日历)
抽象Calendar类提供了一组方法,这些方法允许将以毫秒为单位的时间转换为一组有用的分量。一些可以提供信息的类型是:年,月,日,小

时,分和秒。Calendar的子类能提供特定的功能,以便按照它们本身的规则去解释时间信息,这是能够写出在几个国际环境下都能运行的程序

的Java类库的一个方面。这种子类的一个例子是GregorianCalendar。

Calendar提供非公共的构造函数。
Calendar定义了几个受保护的实例变量。areFieldsSet是一个指示时间分量是否已经建立的boolean型变量。fields是一个包含了时间分量的

ints数组。isSet是一个指示特定时间分量是否已经建立的boolean数组。time是一个包含了该对象的当前时间的long型变量。isTimeSet是一个

指示当前时间是否已经建立的boolean型变量。

// Demonstrate Calendar
import java.util.Calendar;

class CalendarDemo {
public static void main(String args[]) {
String months[] = { 
"Jan", "Feb", "Mar", "Apr", 
"May", "Jun", "Jul", "Aug",
"Sep", "Oct", "Nov", "Dec"};

// Create a calendar initialized with the
// current date and time in the default
// locale and timezone.
Calendar calendar = Calendar.getInstance();

// Display current time and date information.
System.out.print("Date: ");

System.out.print(months[calendar.get(Calendar.MONTH)]);
System.out.print(" " + calendar.get(Calendar.DATE) + " ");
System.out.println(calendar.get(Calendar.YEAR));

System.out.print("Time: ");
System.out.print(calendar.get(Calendar.HOUR) + ":");
System.out.print(calendar.get(Calendar.MINUTE) + ":");
System.out.println(calendar.get(Calendar.SECOND));

// Set the time and date information and display it.
calendar.set(Calendar.HOUR, 10);
calendar.set(Calendar.MINUTE, 29);
calendar.set(Calendar.SECOND, 22);

System.out.print("Updated time: ");
System.out.print(calendar.get(Calendar.HOUR) + ":");
System.out.print(calendar.get(Calendar.MINUTE) + ":");
System.out.println(calendar.get(Calendar.SECOND));


}
D:\javafile\lang>java CalendarDemo
Date: Dec 25 2008
Time: 11:20:38
Updated time: 10:29:22


GregorianCalendar(标准阳历)
GregorianCalendar是Calendar的一个实现大家所熟悉的标准日历(格列高利历)的具体工具。Calendar的getInstance( )方法返回用默认的地

区和时区的当前日期和当前时间所初始化的GregorianCalendar(标准日历)。
GregorianCalendar定义了两个域:AD和BC。它们代表由公历定义的两个纪元。对GregorianCalendar对象,也有几个构造函数。默认的

GregorianCalendar( )方法用默认的地区和时区的当前日期和当前时间初始化对象。提供的三种构造函数如下:
GregorianCalendar(int year, int month, int dayOfMonth)
GregorianCalendar(int year, int month, int dayOfMonth, int hours,
int minutes)
GregorianCalendar(int year, int month, int dayOfMonth, int hours,
int minutes, int seconds)
三种形式中,都设置了日,月和年。这里,year指定了从1900年起的年数。month指定了月,以0表示一月。月中的日由dayOfMonth指定。
第一种形式以午夜设置时间。
第二种形式以小时和分钟设置时间,
第三种形式增加了秒。
也可以通过指定地区和/或时区来构造GregorianCalendar对象。下面的构造函数创建了用指定的时区和/或地区的当前日期和当前时间来初始化

的对象。
GregorianCalendar(Locale locale)
GregorianCalendar(TimeZone timeZone)
GregorianCalendar(TimeZone timeZone, Locale locale)
GregorianCalendar对Calendar的所有抽象方法提供了实现工具。它也提供了一些另外的方法。其中最令人感兴趣的大概是isLeapYear( ),该

方法用于测试某年是否是闰年。它的形式如下:
boolean isLeapYear(int year)
当year是一个闰年时,该方法返回true;否则返回false。
// Demonstrate GregorianCalendar
import java.util.*;

class GregorianCalendarDemo {
public static void main(String args[]) {
String months[] = { 
"Jan", "Feb", "Mar", "Apr", 
"May", "Jun", "Jul", "Aug",
"Sep", "Oct", "Nov", "Dec"};
int year;
// Create a Gregorian calendar initialized 
// with the current date and time in the
// default locale and timezone.
GregorianCalendar gcalendar = new GregorianCalendar();

// Display current time and date information.
System.out.print("Date: ");
System.out.print(months[gcalendar.get(Calendar.MONTH)]);
System.out.print(" " + gcalendar.get(Calendar.DATE) + " ");
System.out.println(year = gcalendar.get(Calendar.YEAR));

System.out.print("Time: ");
System.out.print(gcalendar.get(Calendar.HOUR) + ":");
System.out.print(gcalendar.get(Calendar.MINUTE) + ":");
System.out.println(gcalendar.get(Calendar.SECOND));

// Test if the current year is a leap year
if(gcalendar.isLeapYear(year)) {
System.out.println("The current year is a leap year");

else {
System.out.println("The current year is not a leap year");


}

D:\javafile\lang>java GregorianCalendarDemo
Date: Dec 25 2008
Time: 11:26:3
The current year is a leap year

D:\javafile\lang>

TimeZone(时区)


另 一 个与 时间 有关 的 类是TimeZone。 TimeZone类 允 许给 出相 对于 格 林威 治 时间(GMT),也称为世界时间(UTC)的时区差。它

也计算夏令时。TimeZone仅仅提供默认的构造函数。


SimpleTimeZone 


SimpleTimeZone类是TimeZone的一个方便的子类。它实现TimeZone的抽象方法,并允许对公历进行时区操作。它也计算夏令时。

Locale(地区)


Locale类被实例化以生成其中每一个描述一个地理或文化区域的对象。它是提供了编写在不同的国际环境下都能运行的程序的几个类之一。例

如,用于显示各个区域的日期,时间和数字区别的格式设计。国际化是一个超过本书讨论范围的大课题。然而大多数的程序仅仅需要处理其中

的一些基本要求,包括设置当前的地区。
Locale类定义了下面的一些常数用于处理最常见的地区。

CANADA GERMAN
KOREAN
CANADA_FRENCH
GERMANY
PRC
CHINA ITALIAN
SIMPLIFIED_CHINESE
CHINESE
ENGLISH
FRANCE
FRENCH
ITALY
JAPAN
JAPANESE
KOREA
TAIWAN
TRADITIONAL_CHINESE
UK
US

Locale定义了几种方法。其中最重要的一种是setDefault( ),说明如下:
static void setDefault(Locale localeObj)
它将默认区域设置为由localeObj指定的值。
其他的一些比较感兴趣的方法如下:
final String getDisplayCountry( )
final String getDisplayLanguage( )
final String getDisplayName( )

它们返回可用于显示国家名字,语言种类和对地区做完整描述的可读的字符串。
通过使用getDefault( )方法可以获得默认的区域,说明如下:
static Locale getDefault( )
Calendar 和 GregorianCalendar 是 按 地 区 方 式 工 作 的 类 的 例 子 。 DateFormat 和SimpleDateFormat也与地区有关。
0 0
原创粉丝点击