16.5 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。 下面的程序说明了GregorianCalendar。 // 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"); } } } 该程序的输出如下所示: Date: Jan 25 2001 Time: 11:25:27 The current year is not a leap year 16.6 TimeZone(时区) 另一个与时间有关的类是TimeZone。TimeZone类允许给出相对于格林威治时间(GMT),也称为世界时间(UTC)的时区差。它也计算夏令时。TimeZone仅仅提供默认的构造函数。 由TimeZone定义的一些方法总结在表16-5中。 表16-5 由TimeZone 定义的一些方法 方法 描述 Object clone( ) 返回clone( )方法的特定时区版本 Static String[ ] getAvailableIDs( ) 返回一个表示所有时区的名字的字符串(String)对象的数组 续表 方法 描述 Static String[ ] getAvailableIDs(int timeDelta) 返回一个表示所有时区的名字的字符串(String)对象的数组,时区名为相对于GMT的timeDelta偏移 Static TimeZone getDefault( ) 返回一个表示被主机使用的默认时区的TimeZone对象 String getID( ) 返回调用TimeZone对象的名字 Abstract int getOffset(int era, int year, int month, int dayOfMonth, int dayOfWeek, int millisec) 返回计算当地时间而在GMT中加入的偏移量。该值为夏令时而做调整。该方法的参数表示日期和时间分量 Abstract int getRawOffset( ) 返回计算当地时间而在GMT中加入的未处理的偏移量。该值不对夏令时做调整 static TimeZone getTimeZone(String tzName) 对名字为tzName的时区返回TimeZone对象 abstract boolean inDaylightTime(Date d) 如果调用对象中用d表示的日期对应夏令时,则返回true;否则返回false static void setDefault(TimeZone tz) 设置主机使用的默认时区。tz是对使用的TimeZone对象的一个引用 void setID(String tzName) 将时区的名字(也就是它的ID)设置为由tzName指定的名字 abstract void setRawOffset(int millis) 确定相对GMT的以毫秒为单位的偏移量 abstract boolean useDaylightTime( ) 如果调用对象使用夏令时,则返回true;否则返回false 16.7 SimpleTimeZone SimpleTimeZone类是TimeZone的一个方便的子类。它实现TimeZone的抽象方法,并允许对公历进行时区操作。它也计算夏令时。SimpleTimeZone定义了三个构造函数。其中之一为: SimpleTimeZone(int timeDelta, String tzName)该构造函数创建了SimpleTimeZone对象。与格林威治标准时间(GMT)的偏移量是timeDelta。时区为tzName。 第二个SimpleTimeZone构造函数为: SimpleTimeZone(int timeDelta, String tzId, int dstMonth0, int dstDayInMonth0, int dstDay0, int time0, int dstMonth1, int dstDayInMonth1, int dstDay1, int time1) 这里相对于GMT的偏移量由timeDelta指定。时区名由tzId传递。夏令时的开始由参数dstMonth0 , dstDayInMonth0 , dstDay0 和time0 指出。而夏令时的结束由dstMonth1 ,dstDayInMonth1,dstDay1和time1指出。 第三个SimpleTimeZone构造函数为: SimpleTimeZone(int timeDelta, String tzId, int dstMonth0, int dstDayInMonth0, int dstDay0, int time0, int dstMonth1, int dstDayInMonth1, int dstDay1, int time1, int dstDelta) 这里dstDelta是在夏令时期间保存的毫秒数。
<<上一页
1
2
3
4
5
6
下一页>>
|