<%@ page contentType="text/html; charset=gb2312"%> Java基础-Java中的Calendar和Date类
网站公告:   ◆北天JAVA技术网热情为java爱好者服务,本网内容包括JAVA(JSP、servlet、EJB、webservice、j2ee、javabean、应用服务器、JavaScript),数据库(MYSQL、SQL Server、Sybase、Oracle、DB2、数据库综合知识),设计研究(设计模式、Struts、Spring、Hibernate、设计框架、设计综合知识),WEB2.0新技术(主要介绍AJAX),以及各种技术的入门、实例、例子等等,欢迎各位多来坐坐!◆  诚邀各位JAVA爱好者加盟!◆  本网站内容丰富,更新快,保证每周20篇以上!  
加入收藏
设为首页
联系站长
承接项目
  相关资源:网站首页 | 免费培训学院 | 技术论坛 | JAVA聊天室 | 作家专栏 | 开发工具 | 认证考试 | 会员俱乐部
  JAVA技术初学者园地 | jsp与servlet | javascript | Java源代码 | EJB | web service | 应用服务器 | JAVA综合知识
  设计研究设计模式 | 设计框架 | Struts | Spring | Hibernate | 开源项目 | 面向对象设计 | 设计综合知识
  数 据 库MYSQL | SQL Server | Sybase | Oracle | DB2 | Informix | Access | 数据库综合知识
  其他资源:AJAX新技术 | 网站开发 | ERP软件 | OA办公软件 | 商业智能BI | 开发综合知识 | 承接项目 | 项目试用

 
 
Java基础-Java中的Calendar和Date类
     发布者: 发布时间:2007-08-08

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 创建许多方法了

 
(转载文章请保留出处:北天JAVA技术网(www.java114.com))
 
更多精彩文章:
Vector、ArrayList、List使用深入剖析
在硝烟四起的java IDE市场中,谁是赢家?
从追MM谈Java的23种设计模式
Hibernate入门之自己写的小例子的总结
Struts+spring+hibernate学习笔记!
spring入门编程问题集锦
 
最近评论:
        
春暖花开
wow power leveling2 wow power leveling gvf wow power levelingfcvg wow power leveling wow power leveling wow powerlevelingfcgv wow powerlevelingwq4 wow powerleveling wow powerleveling wow powerleveling world of warcraft power leveling world of warcraft power leveling world of warcraft power leveling world of warcraft power levelingszr world of warcraft power leveling world of warcraft powerleveling world of warcraft powerlevelinge5t world of warcraft powerleveling world of warcraft powerleveling world of warcraft powerleveling wow gold wow gold wow goldrfy5 wow gold wow gold world of warcraft goldre world of warcraft goldgh world of warcraft gold world of warcraft goldfr world of warcraft gold AOC Power Levelinggvgv AGE OF CONAN Power Levelingllwwbb928 d7e6c7xk
        
鍥炲
        
那个雨天的想法!
wow gold,wow power leveling.wow power leveling,wow power leveling, max(6311)
        
如果真的有来生!
四川旅游,九寨沟旅游,稻城亚丁旅游,四姑娘山旅游,海螺沟旅游,西藏旅游, max(7458)
        
如果真的有来生!
四川旅游,九寨沟旅游,稻城亚丁旅游,四姑娘山旅游,海螺沟旅游,西藏旅游, max(4719)
        
那天的情景!
Maple Story mesos,MapleStory mesos,ms mesos,mesos,SilkRoad Gold, max(9016)
        
轻轻走过你的窗前!
world of warcraft gold,cheap world of warcraft gold,warcraft gold,world of warcraft gold,cheap world of warcraft gold,warcraft gold max(5018)
        
快乐情人节!
wow gold,wow gold,wow gold,wow gold,wow gold,wow gold,wow gold buy wow gold for cheap. max(2833)
        
昨夜的狂想曲!
wow gold,WoW Gold,world of warcraft gold,WoW Gold, max(6449)
        
没有情人的情人节!
wow gold,wow power leveling.wow power leveling,wow power leveling, max(5509)
        
标 题:   
内 容:   
 
                                  
 
免责声明:该文章由网友发表,如果对您造成侵权,请联系站长

首页 - 承接项目 - 网站地图 - 联系我们 -
版权所有北天JAVA技术工作室 ICP证号:粤ICP备06079815号