关于Date类和CregorianCalendar类的学习

来源:互联网 发布:rhapsody 软件下载 编辑:程序博客网 时间:2024/04/28 07:37
首先使用这两个类前,切记import java.util.*

 The standard Java library contains a Date class. Its objects describe points in time, such as "December 31, 1999, 23:59:59 GMT".

Although you don't need to know this when you use the Date class, the time is represented by the number of milliseconds (positive or negative) from a fixed point, the so-called epoch, which is 00:00:00 UTC, January 1, 1970. UTC is the Coordinated Universal Time, the scientific time standard that is, for practical purposes, the same as the more familiar GMT or Greenwich Mean Time.

But as it turns out, the Date class is not very useful for manipulating dates. The designers of the Java library take the point of view that a date description such as "December 31, 1999, 23:59:59" is an arbitrary convention, governed by a calendar. This particular description follows the Gregorian calendar, which is the calendar used in most places of the world. The same point in time would be described quite differently in the Chinese or Hebrew lunar calendars, not to mention the calendar used by your customers from Mars.

The library designers decided to separate the concerns of keeping time and attaching names to points in time. Therefore, the standard Java library contains two separate classes: the Date class, which represents a point in time, and the GregorianCalendar class, which expresses dates in the familiar calendar notation. In fact, the GregorianCalendar class extends a more generic Calendar class that describes the properties of calendars in general. In theory, you can extend the Calendar class and implement the Chinese lunar calendar or a Martian calendar. However, the standard library does not contain any calendar implementations besides the Gregorian calendar.

Separating time measurement from calendars is good object-oriented design. In general, it is a good idea to use separate classes to express different concepts.

The Date class has only a small number of methods that allow you to compare two points in time. For example, the before and after methods tell you if one point in time comes before or after another.

java.util.GregorianCalendar 1.1
  • GregorianCalendar()

    constructs a calendar object that represents the current time in the default time zone with the default locale.

  • GregorianCalendar(int year, int month, int day)

    constructs a Gregorian calendar with the given date.

    Parameters:

    year

    the year of the date

    month

    the month of the date. This value is 0-based; for example, 0 for January

    day

    the day of the month


  • GregorianCalendar(int year, int month, int day, int hour, int minutes, int seconds)

    constructs a Gregorian calendar with the given date and time.

    Parameters:

    year

    the year of the date

    month

    the month of the date. This value is 0-based; for example, 0 for January

    day

    the day of the month

    hour

    the hour (between 0 and 23)

    minutes

    the minutes (between 0 and 59)

    seconds

    the seconds (between 0 and 59)


  • int get(int field)

    gets the value of a particular field.

  • void set(int field, int value)

    sets the value of a particular field.

    Parameters:

    field

    one of Calendar.ERA, Calendar.YEAR, Calendar.MONTH, Calendar.WEEK_OF_YEAR, Calendar.WEEK_OF_MONTH, Calendar.DAY_OF_MONTH, Calendar.DAY_OF_YEAR, Calendar.DAY_OF_WEEK, Calendar.DAY_OF_WEEK_IN_MONTH, Calendar.AM_PM, Calendar.HOUR, Calendar.HOUR_OF_DAY, Calendar.MINUTE, Calendar.SECOND, Calendar.MILLISECOND, Calendar.ZONE_OFFSET, Calendar.DST_OFFSET

    Parameters:

    field

    one of the constants accepted by get

    value

    the new value


  • void set(int year, int month, int day)

    sets the date fields to a new date.

    Parameters:

    year

    the year of the date

    month

    the month of the date. This value is 0-based; for example, 0 for January

    day

    the day of the month


  • void set(int year, int month, int day, int hour, int minutes, int seconds)

    sets the date and time fields to new values.

    Parameters:

    year

    the year of the date

    month

    the month of the date. This value is 0-based; for example, 0 for January

    day

    the day of the month

    hour

    the hour (between 0 and 23)

    minutes

    the minutes (between 0 and 59)

    seconds

    the seconds (between 0 and 59)


  • void add(int field, int amount)

    is a date arithmetic method. Adds the specified amount of time to the given time field. For example, to add 7 days to the current calendar date, call c.add(Calendar.DAY_OF_MONTH, 7).

    Parameters:

    field

    the field to modify (using one of the constants documented in the get method)

    amount

    the amount by which the field should be changed (can be negative)


  • void setTime(Date time)

    sets this calendar to the given point in time.

    Parameters:

    time

    a point in time


  • Date getTime()

    gets the point in time that is represented by the current value of this calendar object.

Code Example:
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();
   }
}



原创粉丝点击