java中时期相关类——Date、DateFormat、Calendar
Java 语言的Calendar(日历),Date(日期),和DateFormat(日期格式)组成了Java标准的一个基本但是非常重要的部分。日期是商业逻辑计算一个关键的部分。所有的开发者都应该能够计算未来的日期,定制日期的显示格式,并将文本数据解析成日期对象。
个人见解:
Date表示一个日期对象
对Date对象进行格式化输出或者解析某种格式的日期字符串为日期对象,使用DateFormat及其子类SimpleDateFormat(在字符串和Date类型间转换)
对已有的日期对象进行更改、计算,使用Calendar及其子类GregorianCalendar
其中,类DateFormat和类Calendar都不能被实例化,因为二者均只有protected的构造函数,但可以通过相应的静态方法(如getDateTimeInstance()、getInstance()等)来获得实例。
创建一个日期对象
让我们看一个使用系统的当前日期和时间创建一个日期对象并返回一个长整数。这个时间通常被称为Java 虚拟机(JVM)主机环境的系统时间。
- import java.util.Date;
- public class DateExample1 {
- public static void main(String[] args) {
- // Get the system date/time
- Date date = new Date();
- System.out.println(date.getTime());
- } }
在星期六,2001年9月29日,下午大约是6:50的样子,上面的例子在系统输出设备上显示的结果是 1001803809710。值得注意的是我们使用了Date 构造函数创建一个日期对象,这个构造函数没有接受任何参数,而这个构造函数在内部使用了System.currentTimeMillis() 方法来从系统获取日期。现在我们已经知道了如何获取从1970年1月1日开始经历的毫秒数了。我们如何才能以一种用户明白的格式来显示这个日期呢? 在这里类java.text.SimpleDateFormat 和它的抽象基类 java.text.DateFormat 就派得上用场了。
日期数据的定制格式
假如我们希望定制日期数据的格式,比方星期六-9月-29日-2001年. 下面的例子展示了如何完成这个工作:
- import java.text.SimpleDateFormat;
- import java.util.Date;
- public class DateExample2 {
- public static void main(String[] args) {
- SimpleDateFormat bartDateFormat = new SimpleDateFormat("EEEE-MMMM-dd-yyyy");
- Date date = new Date();
- System.out.println(bartDateFormat.format(date));
- } }
只要通过向SimpleDateFormat 的构造函数传递格式字符串"EEE-MMMM-dd-yyyy",我们就能够指明自己想要的格式。格式字符串中的ASCII 字符告诉格式化函数下面显示日期数据的哪一个部分。EEEE是星期,MMMM是月,dd是日,yyyy是年。字符的个数决定了日期是如何格式化的。传递"EE-MM-dd-yy"会显示 Sat-09-29-01。
将文本数据解析成日期对象
假设我们有一个文本字符串包含了一个格式化了的日期对象,我们希望解析这个字符串并从文本日期数据创建一个日期对象。我们将再次以格式化字符串"MM-dd-yyyy" 调用SimpleDateFormat类。但是这一次,我们使用格式化解析而不是生成一个文本日期数据。我们的例子,显示在下面,将解析文本字符串"9-29-2001"并创建一个值为001736000000 的日期对象。
- import java.text.SimpleDateFormat;
- import java.util.Date;
- public class DateExample3 {
- public static void main(String[] args) {
- // 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 date = bartDateFormat.parse(dateStringToParse);
- // Now send the parsed date as a long value
- // to the system output.
- System.out.println(date.getTime());
- }
- catch (Exception ex) {
- System.out.println(ex.getMessage());
- }
- } }
使用标准的日期格式化过程
既然我们已经可以生成和解析定制的日期格式了,让我们来看一看如何使用内建的格式化过程。方法 DateFormat.getDateTimeInstance() 让我们得以用几种不同的方法获得标准的日期格式化过程。下面是我们获取了四个内建的日期格式化过程。它们包括一个短的,中等的,长的,和完整的日期格式。
- import java.text.DateFormat;
- import java.util.Date;
- public class DateExample4 {
- public static void main(String[] args) {
- Date date = new Date();
- 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));
- }
- }
注意我们在对 getDateTimeInstance的每次调用中都传递了两个值:第一个参数是日期风格,而第二个参数是时间风格。它们都是基本数据类型int(整型)。考虑到可读性,我们使用了DateFormat 类提供的常量: SHORT,MEDIUM,LONG,和 FULL。
运行我们的例子程序的时候,它将向标准输出设备输出下面的内容:
9/29/01 8:44 PM
Sep 29,2001 8:44:45 PM
September 29,2001 8:44:45 PM EDT
Saturday,September 29,2001 8:44:45 PM EDT
Calendar 类
我们现在已经能够格式化并创建一个日期对象了,但是我们如何才能设置和获取日期数据的特定部分呢,比如说小时,日,或者分钟? 我们又如何在日期的这些部分加上或者减去值呢? 答案是使用Calendar 类。
假设你想要设置,获取,和操纵一个日期对象的各个部分,比方一个月的一天或者是一个星期的一天,为了演示这个过程,我们将使用具体的子类 java.util.GregorianCalendar。 考虑下面的例子,它计算得到下面的第十个星期五是13号。
- import java.util.GregorianCalendar;
- import java.util.Date;
- import java.text.DateFormat;
- public class DateExample5 {
- public static void main(String[] args) {
- DateFormat dateFormat = DateFormat.getDateInstance(DateFormat.FULL);
- // Create our Gregorian Calendar.
- GregorianCalendar cal = new GregorianCalendar();
- // Set the date and time of our calendar
- // to the system´s date and time
- cal.setTime(new Date());
- System.out.println("System Date: " + dateFormat.format(cal.getTime()));
- // Set the day of week to FRIDAY
- cal.set(GregorianCalendar.DAY_OF_WEEK, GregorianCalendar.FRIDAY);
- System.out.println("After Setting Day of Week to Friday: " +dateFormat.format(cal.getTime()));
- int friday13Counter = 0;
- while (friday13Counter <= 10) {
- // Go to the next Friday by adding 7 days.
- cal.add(GregorianCalendar.DAY_OF_MONTH,7);
- // If the day of month is 13 we have
- // another Friday the 13th.
- if (cal.get(GregorianCalendar.DAY_OF_MONTH) == 13) { friday13Counter++;
- System.out.println(dateFormat.format(cal.getTime()));
- }
- }
- }
- }
在这个例子中我们作了有趣的函数调用:
- cal.set(GregorianCalendar.DAY_OF_WEEK,
- GregorianCalendar.FRIDAY);
和:
- cal.add(GregorianCalendar.DAY_OF_MONTH,7);
set 方法能够让我们通过简单的设置星期中的哪一天这个域来将我们的时间调整为星期五。注意到这里我们使用了常量 DAY_OF_WEEK 和 FRIDAY来增强代码的可读性。add 方法让我们能够在日期上加上数值,润年的所有复杂的计算都由这个方法自动处理。
我们这个例子的输出结果是:
System Date: Saturday,September 29,2001
当我们将它设置成星期五以后就成了:
Friday,September 28,2001
Friday,September 13,2002
Friday,December 13,2002
Friday,June 13,2003
Friday,February 13,2004
Friday,August 13,2004
Friday,May 13,2005
Friday,January 13,2006
Friday,October 13,2006
Friday,April 13,2007
Friday,July 13,2007
Friday,June 13,2008