您的位置:首页 > 编程语言 > Java开发

java calendar资料

2008-06-28 11:34 302 查看

java calendar资料

<script type="text/javascript">
<!--
google_ad_client = "pub-7000344574295148";
google_alternate_ad_url = "http://www.web162.com";
google_ad_width = 468;
google_ad_height = 15;
google_ad_format = "468x15_0ads_al_s";
google_ad_channel = "";
google_color_border = "F4F4F4";
google_color_bg = "F4F4F4";
google_color_link = "0066CC";
google_color_text = "000000";
google_color_url = "008000";
//-->
</script>
<script src="http://pagead2.googlesyndication.com/pagead/show_ads.js" type="text/javascript">
</script>

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() 方法来从系统获取日期.如果用

System.out.println(new Date());

则输出形式为:Tue Nov 08 14:28:07 CST 2005

那么, 现在我们已经知道了如何获取从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. 请察看Sun 公司的Web 站点获取日期格式化选项的完整的指示.

三、将文本数据解析成日期对象r

假设我们有一个文本字符串包含了一个格式化了的日期对象, 而我们希望解析这个
字符串并从文本日期数据创建一个日期对象. 我们将再次以格式化字符串
"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. 要知道获取时间和日期格式化过程的更多的方法和选项, 请看Sun 公司Web
站点上的解释.

 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息