GmtCalendar is a useful class for working with times that are based using GMT time. : Time « Development Class « Java






GmtCalendar is a useful class for working with times that are based using GMT time.

     
//package org.mbari.util;

import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Locale;
import java.util.TimeZone;

/**
 * GmtCalendar is a useful class for working with times that are based using GMT
 * time. For example, if a data loger collects info and returns it as GMT using
 * GregorianCalendar is a pain because it will assumes that user supplied times
 * are from the default TimeZone. This will cause an ofset in the time of each
 * data sample. To avoid this GmtCalendar uses the GMT as it's default TimeZone.
 * Note that it will return time in milliseconds UTC but any dates produced by
 * using DateFormat will give the sample time based on the local time.
 * 
 * @author Brian Schlining
 * @version 30 Nov 1999
 * @created October 31, 2000
 */

public class GmtCalendar extends GregorianCalendar {

  /**
   * Constructs a GmtCalendar with the given date set in the GMT time zone
   * with the default locale.
   * 
   * @param year
   *            the value used to set the YEAR time field in the calendar.
   * @param month
   *            the value used to set the MONTH time field in the calendar.
   *            Month value is 0-based. e.g., 0 for January.
   * @param date
   *            the value used to set the DATE time field in the calendar.
   */

  public GmtCalendar(int year, int month, int date) {

    super(TimeZone.getTimeZone("GMT"), Locale.getDefault());

    this.set(ERA, AD);

    this.set(YEAR, year);

    this.set(MONTH, month);

    this.set(DATE, date);

  }

  /**
   * Constructs a GregorianCalendar with the given date and time set for the
   * GMT time zone with the default locale.
   * 
   * @param year
   *            the value used to set the YEAR time field in the calendar.
   * @param month
   *            the value used to set the MONTH time field in the calendar.
   *            Month value is 0-based. e.g., 0 for January.
   * @param date
   *            the value used to set the DATE time field in the calendar.
   * @param hour
   *            the value used to set the HOUR_OF_DAY time field in the
   *            calendar.
   * @param minute
   *            the value used to set the MINUTE time field in the calendar.
   */

  public GmtCalendar(int year, int month, int date, int hour, int minute) {

    super(TimeZone.getTimeZone("GMT"), Locale.getDefault());

    this.set(ERA, AD);

    this.set(YEAR, year);

    this.set(MONTH, month);

    this.set(DATE, date);

    this.set(HOUR_OF_DAY, hour);

    this.set(MINUTE, minute);

  }

  /**
   * Constructs a GregorianCalendar with the given date and time set for the
   * GMT time zone with the default locale.
   * 
   * @param year
   *            the value used to set the YEAR time field in the calendar.
   * @param month
   *            the value used to set the MONTH time field in the calendar.
   *            Month value is 0-based. e.g., 0 for January.
   * @param date
   *            the value used to set the DATE time field in the calendar.
   * @param hour
   *            the value used to set the HOUR_OF_DAY time field in the
   *            calendar.
   * @param minute
   *            the value used to set the MINUTE time field in the calendar.
   * @param second
   *            the value used to set the SECOND time field in the calendar.
   */

  public GmtCalendar(int year, int month, int date, int hour, int minute,
      int second) {

    super(TimeZone.getTimeZone("GMT"), Locale.getDefault());

    this.set(ERA, AD);

    this.set(YEAR, year);

    this.set(MONTH, month);

    this.set(DATE, date);

    this.set(HOUR_OF_DAY, hour);

    this.set(MINUTE, minute);

    this.set(SECOND, second);

  }

  /**
   * Constructor for the GmtCalendar object
   * 
   * @param date
   *            Description of the Parameter
   */
  public GmtCalendar(Date date) {
    super(TimeZone.getTimeZone("GMT"), Locale.getDefault());
    this.clear();
    this.setTimeZone(TimeZone.getTimeZone("GMT"));
    this.setTime(date);
    this.setTime(this.getTime());
  }

  /**
   * Constructor for the GmtCalendar object
   * 
   * @param millisec
   *            Description of the Parameter
   */
  public GmtCalendar(long millisec) {
    super(TimeZone.getTimeZone("GMT"), Locale.getDefault());
    this.clear();
    this.setTimeZone(TimeZone.getTimeZone("GMT"));
    this.setTimeInMillis(millisec);
    this.setTime(this.getTime());
  }

}

   
    
    
    
    
  








Related examples in the same category

1.Get Time From Date
2.ISO8601 Date Time Format
3.Time Format
4.Time Formatter
5.Returns time string
6.Returns the given date with the time values cleared
7.Convert the time to the midnight of the currently set date
8.Compare both times and dates
9.Tells you if the date part of a datetime is in a certain time range
10.Returns the given date with time set to the end of the day
11.Convert milliseconds to readable string
12.Determines whether or not a date has any time values (hour, minute, seconds or millisecondsReturns the given date with the time values cleared
13.Returns a formatted String from time
14.Time library
15.Elapsed time in hours/minutes/seconds
16.Sets the time on the same day to 00:00:00.000
17.Determines if given times span at least an entire day
18.Converts a given time in milliseconds into a XMLGregorianCalendar object.
19.Time Distance
20.Time Formatter
21.Format time
22.A utility class for representing a span of time.
23.Time Period
24.Represents the TSTInfo strcture within a time-stamp token (RFC 3161).
25.SimpleTimer enables bounded and unbounded waits.
26.Takes a time in milliseconds and returns an hours, minutes and seconds representation.
27.Format time in milliseconds