date 类从java 开发包(jdk) 1.0 就开始进化, 当时它只包含了几个取得或者设置一个日期数据的各个部分的方法, 比如说月, 日, 和年。这些方法现在遭到了批评并且已经被转移到了calendar类里去了, 我们将在本文中进一步讨论它。
这种改进旨在更好的处理日期数据的国际化格式. 就象在jdk 1.1中一样, date 类实际上只是一个包裹类, 它包含的是一个长整型数据, 表示的是从gmt(格林尼治标准时间)1970年, 1 月 1日00:00:00这一刻之前或者是之后经历的毫秒数。
创建一个日期对象
让我们看一个使用系统的当前日期和时间创建一个日期对象并返回一个长整数的简单例子. 这个时间通常被称为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. 请察看sun 公司的web 站点获取日期格式化选项的完整的指示.
将文本数据解析成日期对象
假设我们有一个文本字符串包含了一个格式化了的日期对象, 而我们希望解析这个字符串并从文本日期数据创建一个日期对象. 我们将再次以格式化字符串"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 站点上的解释.
运行我们的例子程序的时候, 它将向标准输出设备输出下面的内容:
| 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 |
闽公网安备 35060202000074号