Java中处理日期的类主要有Date、Calendar和DateFormat三个,在JDK中它们对应的包的位置和相关的派生类如下所示:  1、 Date:具体类java.util.Date ;  2、 Calendar:抽象类java.text.DateFormat,它派生的一个子类为java.text.SimpleDateFormat  3、 DateFormat:抽象类java.util.Calendar,它派生的一个子类为java.util.GregorianCalendar  (注:如果有朋友对具体类和抽象类不了解,可以参考相关Java资料)  现在对以上三个类进行如下分析:  一、对Date类的分析  Date 类从JDK 1.0开始进化, 当时它只包含了几个简单的处理日期数据的方法。 由于这些方法实用性差,现在基本上被Calendar类中各方法所代替了。这种改进目的是为了更好的处理日期数据国际化格式。 Date 类实际上只是一个包裹类, 它包含一个长整型数据, 表示的是从GMT(格林尼治标准时间)1970年, 1 月 1日00:00:00这一刻之前或者是之后经历的毫秒数.  为了加深对Date类的理解,列举如下一个简单例子来说明Date的使用:  import java.util.Date;  public class DateTest1 {  public static void main(String[] args) {  /** Get the system Date **/  Date date = new Date();  System.out.println(date.getTime());  }  }  系统输出如下结果:  1001803809710  在这个例子中,值得我们注意的是我们使用了Date 构造函数创建一个日期对象, 在没有任何参数的情况下,这个构造函数会自动调用System.currentTimeMillis() 方法来获取当前系统日期.。  二、对DateFormat类的分析  如果我们希望定制日期数据的格式, 比如显示的日期格式为:2006年06月23日,抽象类java.util.Calendar和它的子类java.util.GregorianCalendar能完成这一功能。  下面的例子展示了如何完成这个工作:  import java.util.SimpleDateFormat;  import java.util.Date;  public class DateTest2 {  public static void main(String[] args) {  SimpleDateFormat format = new SimpleDateFormat("yyyy年MM月dd日",Locale.US);  Date date = new Date();  System.out.println(bartDateFormat.format(date));  }  }  只要通过向SimpleDateFormat 的构造函数传递格式字符串" yyyy年MM月dd日", 我们就可以指明自己想要的格式. 其中yyyy是年,MM是月, dd是日. 字符的个数决定了日期是如何格式化的,如果格式为"yyyy年MM月dd日"就会显示 06年06月23日;如果是"yy-MM-dd"就显示06-06-23。  三、对Calendar 类的分析  如果想设置、获取、操纵一个日期对象的各个特定部分,比如获得小时, 日, 分钟、计算一个月的某一天说星期几等等。这样的情况就需要抽象类java.util.Calendar和它的子类来处理。为了能清楚地说明问题,列举下面的例子, 它的功能是计算第十个星期五是13号.  import java.util.GregorianCalendar;  import java.util.Date;  import java.text.DateFormat;  public class CalendarTest{  public static void main(String[] args) {  DateFormat dateFormat = DateFormat.getDateInstance(DateFormat.FULL);  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()));  }  }  }  }  输出结果是:  System Date: Saturday, September 29, 2005  四、总结  在Java通过一下三个类的使用,一般可以满足各种形式的日期使用。实际上,Java中日期使用的方法还有很多,如果有兴趣的朋友请参考JDK帮助手册。

解决方案 »

  1.   

    补充一下:
    1.java.util.Date包含了日期和时间
    有三个子类:
    java.sql.Date:继承了日期那一部分
    java.sql.Time:继承了时间那一部分
    java.sql.Timestamp:继承了日期和时间
    ResultSet.getDate()返回的是一个java.sql.Date
    ResultSet.getTime()返回的是一个java.sql.Time
    ResultSet.getTimestamp()返回的是一个java.sql.Timestamp
    2.常用的格式化日期的类:java.text.DateFormat这是一个抽象类,一般使用他的子类:java.text.SimpleDateFormat
    3.对日期进行处理一般用java.util.Calendar这个类
    4.如果涉及到时区问题,使用java.util.GregorianCalendar这个类
    5.时区是这个类:java.util.TimeZone
      

  2.   

    最好也把java.sql.Date中的Date也放进去比较下就好了,不错,值得学习
      

  3.   

    Prior to JDK 1.1, the class Date had two additional functions. It allowed the interpretation of dates as year, month, day, hour, minute, and second values. It also allowed the formatting and parsing of date strings. Unfortunately, the API for these functions was not amenable to internationalization. As of JDK 1.1, the Calendar class should be used to convert between dates and time fields and the DateFormat class should be used to format and parse date strings. The corresponding methods in Date are deprecated.