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