java中时期相关类——Date、DateFormat、Calendar

核心提示:java中时期相关类Date、DateFormat、Calendar Java 语言的Calendar(日历),Date(日期),和DateFormat(日期格式)组成了Java标准的一个基本但是非常重要的部分。日期是商业逻辑计算一个关键的部分。所有的开发者都应该能够计算未来的日期,定制日期的显示格式,

java中时期相关类——Date、DateFormat、Calendar

 

Java 语言的Calendar(日历),Date(日期),和DateFormat(日期格式)组成了Java标准的一个基本但是非常重要的部分。日期是商业逻辑计算一个关键的部分。所有的开发者都应该能够计算未来的日期,定制日期的显示格式,并将文本数据解析成日期对象。    

 

       个人见解:

           Date表示一个日期对象

           对Date对象进行格式化输出或者解析某种格式的日期字符串为日期对象,使用DateFormat及其子类SimpleDateFormat(在字符串和Date类型间转换)

           对已有的日期对象进行更改、计算,使用Calendar及其子类GregorianCalendar

           其中,类DateFormat和类Calendar都不能被实例化,因为二者均只有protected的构造函数,但可以通过相应的静态方法(如getDateTimeInstance()、getInstance()等)来获得实例。

 

创建一个日期对象    

 

        让我们看一个使用系统的当前日期和时间创建一个日期对象并返回一个长整数。这个时间通常被称为Java 虚拟机(JVM)主机环境的系统时间。    

 

  1. import java.util.Date;        
  2.       
  3. public class DateExample1 {        
  4.       
  5. public static void main(String[] args) {        
  6.       
  7. // Get the system date/time        
  8.       
  9. Date date = new Date();        
  10.       
  11. System.out.println(date.getTime());        
  12.       
  13. } }        

   
      在星期六,2001年9月29日,下午大约是6:50的样子,上面的例子在系统输出设备上显示的结果是 1001803809710。值得注意的是我们使用了Date 构造函数创建一个日期对象,这个构造函数没有接受任何参数,而这个构造函数在内部使用了System.currentTimeMillis() 方法来从系统获取日期。现在我们已经知道了如何获取从1970年1月1日开始经历的毫秒数了。我们如何才能以一种用户明白的格式来显示这个日期呢? 在这里类java.text.SimpleDateFormat 和它的抽象基类 java.text.DateFormat 就派得上用场了。    


   
日期数据的定制格式     
   
       假如我们希望定制日期数据的格式,比方星期六-9月-29日-2001年. 下面的例子展示了如何完成这个工作:     

  1.       
  2. import java.text.SimpleDateFormat;        
  3.       
  4. import java.util.Date;        
  5.       
  6. public class DateExample2 {        
  7.       
  8. public static void main(String[] args) {        
  9.       
  10. SimpleDateFormat bartDateFormat = new SimpleDateFormat("EEEE-MMMM-dd-yyyy");        
  11.       
  12. Date date = new Date();        
  13.       
  14. System.out.println(bartDateFormat.format(date));        
  15.       
  16. } }        
  17.       

      只要通过向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 的日期对象。

  1. import java.text.SimpleDateFormat;        
  2.       
  3. import java.util.Date;        
  4.       
  5. public class DateExample3 {        
  6.       
  7. public static void main(String[] args) {        
  8.       
  9. // Create a date formatter that can parse dates of        
  10.       
  11. // the form MM-dd-yyyy.        
  12.       
  13. SimpleDateFormat bartDateFormat = new SimpleDateFormat("MM-dd-yyyy");        
  14.       
  15. // Create a string containing a text date to be parsed.        
  16.       
  17. String dateStringToParse = "9-29-2001";        
  18.       
  19. try {        
  20.       
  21. // Parse the text version of the date.        
  22.       
  23. // We have to perform the parse method in a        
  24.       
  25. // try-catch construct in case dateStringToParse        
  26.       
  27. // does not contain a date in the format we are expecting.        
  28.       
  29. Date date = bartDateFormat.parse(dateStringToParse);        
  30.       
  31. // Now send the parsed date as a long value        
  32.       
  33. // to the system output.        
  34.       
  35. System.out.println(date.getTime());        
  36.       
  37. }        
  38.       
  39. catch (Exception ex) {        
  40.       
  41. System.out.println(ex.getMessage());        
  42.       
  43. }        
  44.       
  45. } }        

 

   
使用标准的日期格式化过程     
   
      既然我们已经可以生成和解析定制的日期格式了,让我们来看一看如何使用内建的格式化过程。方法 DateFormat.getDateTimeInstance() 让我们得以用几种不同的方法获得标准的日期格式化过程。下面是我们获取了四个内建的日期格式化过程。它们包括一个短的,中等的,长的,和完整的日期格式。

  1. import java.text.DateFormat;        
  2.       
  3. import java.util.Date;        
  4.       
  5. public class DateExample4 {        
  6.       
  7. public static void main(String[] args) {        
  8.       
  9. Date date = new Date();        
  10.       
  11. DateFormat shortDateFormat = DateFormat.getDateTimeInstance(        
  12.       
  13. DateFormat.SHORT, DateFormat.SHORT);        
  14.       
  15. DateFormat mediumDateFormat = DateFormat.getDateTimeInstance(        
  16.       
  17. DateFormat.MEDIUM, DateFormat.MEDIUM);        
  18.       
  19. DateFormat longDateFormat = DateFormat.getDateTimeInstance(        
  20.       
  21. DateFormat.LONG, DateFormat.LONG);        
  22.       
  23. DateFormat fullDateFormat = DateFormat.getDateTimeInstance(        
  24.       
  25. DateFormat.FULL, DateFormat.FULL);        
  26.       
  27. System.out.println(shortDateFormat.format(date));        
  28.       
  29. System.out.println(mediumDateFormat.format(date));        
  30.       
  31. System.out.println(longDateFormat.format(date));        
  32.       
  33. System.out.println(fullDateFormat.format(date));        
  34.       
  35. }        
  36.       
  37. }        
  38.       

       注意我们在对 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号。

  1. import java.util.GregorianCalendar;        
  2.       
  3. import java.util.Date;        
  4.       
  5. import java.text.DateFormat;        
  6.       
  7. public class DateExample5 {        
  8.       
  9. public static void main(String[] args) {        
  10.       
  11. DateFormat dateFormat = DateFormat.getDateInstance(DateFormat.FULL);        
  12.       
  13. // Create our Gregorian Calendar.        
  14.       
  15. GregorianCalendar cal = new GregorianCalendar();        
  16.       
  17. // Set the date and time of our calendar        
  18.       
  19. // to the system´s date and time        
  20.       
  21. cal.setTime(new Date());        
  22.       
  23. System.out.println("System Date: " + dateFormat.format(cal.getTime()));        
  24.       
  25. // Set the day of week to FRIDAY        
  26.       
  27. cal.set(GregorianCalendar.DAY_OF_WEEK, GregorianCalendar.FRIDAY);        
  28.       
  29. System.out.println("After Setting Day of Week to Friday: " +dateFormat.format(cal.getTime()));        
  30.       
  31. int friday13Counter = 0;        
  32.       
  33. while (friday13Counter <= 10) {        
  34.       
  35. // Go to the next Friday by adding 7 days.        
  36.       
  37. cal.add(GregorianCalendar.DAY_OF_MONTH,7);        
  38.       
  39. // If the day of month is 13 we have        
  40.       
  41. // another Friday the 13th.        
  42.       
  43. if (cal.get(GregorianCalendar.DAY_OF_MONTH) == 13) { friday13Counter++;        
  44.       
  45. System.out.println(dateFormat.format(cal.getTime()));        
  46.       
  47. }        
  48.       
  49. }        
  50.       
  51. }        
  52.       
  53. }       


   
      在这个例子中我们作了有趣的函数调用:

  1. cal.set(GregorianCalendar.DAY_OF_WEEK,        
  2.       
  3. GregorianCalendar.FRIDAY);        

   
和:

  1. 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    

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值