Java基础-Java中的Calendar和Date类

发布时间:2006.04.30 08:24     来源:51cto    作者:
  
Java 语言的Calendar(日历),Date(日期),和DateFormat(日期格式)组成了Java标准的一个基本但是非常重要的部分。日期是商业逻辑计算一个关键的部分。所有的开发者都应该能够计算未来的日期,定制日期的显示格式,并将文本数据解析成日期对象。
­
创建一个日期对象
­
让我们看一个使用系统的当前日期和时间创建一个日期对象并返回一个长整数。这个时间通常被称为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
­
时间掌握在你的手里
­
有了这些Date 和Calendar 类的例子,你应该能够使用 java.util.Date,java.text.SimpleDateFormat,和 java.util.GregorianCalendar 创建许多方法了。
 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值