Date类&GregorianCalendar类

Date类

Constructors always have the same name as the class name. Thus, the constructor for the Date class is called Date. To construct a Date object, you combine the constructor with the new operator, as follows:

new Date()
  

This expression constructs a new object. The object is initialized to the current date and time.

If you like, you can pass the object to a method:

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

Alternatively, you can apply a method to the object that you just constructed. One of the methods of the Date class is the toString method. That method yields a string representation of the date. Here is how you would apply the toString method to a newly constructed Date object.

String s = new Date().toString();
  

Simply store the object in a variable: Date birthday = new Date();

  

There is an important difference between objects and object variables. For example, the statement

Date deadline; // deadline doesn't refer to any object

defines an object variable, deadline, that can refer to objects of type Date. It is important to realize that the variable deadline is not an object and, in fact, does not yet even refer to an object. You cannot use any Date methods on this variable at this time. The statement

s = deadline.toString(); // not yet

would cause a compile-time error.

You must first initialize the deadline variable. You have two choices. Of course, you can initialize the variable with a newly constructed object:

deadline = new Date();

Or you can set the variable to refer to an existing object:

deadline = birthday;

 

Variables are not automatically initialized to null. You must initialize them, either by calling new or by setting them to null.

 

Gregorian类

The GregorianCalendar class has many more methods than the Date class. In particular, it has several useful constructors. The expression

new GregorianCalendar()

constructs a new object that represents the date and time at which the object was constructed.

You can construct a calendar object for midnight on a specific date by supplying year, month, and day:

new GregorianCalendar(1999, 11, 31)

 

Somewhat curiously, the months are counted from 0. Therefore, 11 is December. For greater clarity, there are constants like Calendar.DECEMBER.

new GregorianCalendar(1999, Calendar.DECEMBER, 31)

 

You can also set the time:

new GregorianCalendar(1999, Calendar.DECEMBER, 31, 23, 59, 59)
Of course, you will usually want to store the constructed object in an object variable:
GregorianCalendar deadline = new GregorianCalendar(. . .);

 

GregorianCalendar now = new GregorianCalendar();
int month = now.get(Calendar.MONTH);
int weekday = now.get(Calendar.DAY_OF_WEEK);

deadline.set(Calendar.YEAR, 2001);
deadline.set(Calendar.MONTH, Calendar.APRIL);
deadline.set(Calendar.DAY_OF_MONTH, 15);

deadline.set(2001, Calendar.APRIL, 15);

Finally, you can add a number of days, weeks, months, and so on, to a given calendar object.

deadline.add(Calendar.MONTH, 3); // move deadline by 3 months

If you add a negative number, then the calendar is moved backwards.

 

Date time = calendar.getTime();
calendar.setTime(time);

GregorianCalendar calendar = new GregorianCalendar(year, month, day);
Date hireDay = calendar.getTime();

GregorianCalendar calendar = new GregorianCalendar();
calendar.setTime(hireDay);
int year = calendar.get(Calendar.YEAR);

 

/**
   @version 1.31 2004-02-19
   @author Cay Horstmann
*/

import java.util.*;

public class CalendarTest
{  
   public static void main(String[] args)
   {  
      // construct d as current date
      GregorianCalendar d = new GregorianCalendar();

      int today = d.get(Calendar.DAY_OF_MONTH);
      int month = d.get(Calendar.MONTH);

      // set d to start date of the month
      d.set(Calendar.DAY_OF_MONTH, 1);

      int weekday = d.get(Calendar.DAY_OF_WEEK);

      // print heading
      System.out.println("Sun Mon Tue Wed Thu Fri Sat");

      // indent first line of calendar
      for (int i = Calendar.SUNDAY; i < weekday; i++ )
         System.out.print("    ");

      do
      {  
         // print day
         int day = d.get(Calendar.DAY_OF_MONTH);
         System.out.printf("%3d", day);

         // mark current day with *
         if (day == today)
            System.out.print("*");
         else
            System.out.print(" ");

         // start a new line after every Saturday
         if (weekday == Calendar.SATURDAY)
            System.out.println();

         // advance d to the next day
         d.add(Calendar.DAY_OF_MONTH, 1);
         weekday = d.get(Calendar.DAY_OF_WEEK);
      } 
      while (d.get(Calendar.MONTH) == month);
      // the loop exits when d is day 1 of the next month

      // print final end of line if necessary
      if (weekday != Calendar.SUNDAY)
         System.out.println();
   }
}

 

 

posted @ 2010-09-12 18:52  露初晞  Views(2347)  Comments(0)    收藏  举报