Package ucar.nc2.time

Class CalendarDate

  • All Implemented Interfaces:
    Comparable<CalendarDate>

    @Immutable
    public class CalendarDate
    extends Object
    implements Comparable<CalendarDate>
    A Calendar Date. Replaces java.util.Date. Allows non-standard calendars. Default is Calendar.gregorian. Always in UTC time zone.
    Since:
    3/21/11
    • Method Detail

      • present

        public static CalendarDate present()
        Get a CalendarDate representing the present moment
        Returns:
        CalendarDate representing the present moment in UTC
      • of

        public static CalendarDate of​(Calendar cal,
                                      int year,
                                      int monthOfYear,
                                      int dayOfMonth,
                                      int hourOfDay,
                                      int minuteOfHour,
                                      int secondOfMinute)
        Get Calendar date from fields. Uses UTZ time zone
        Parameters:
        cal - calendar to use, or null for default
        year - any integer
        monthOfYear - 1-12
        dayOfMonth - 1-31
        hourOfDay - 0-23
        minuteOfHour - 0-59
        secondOfMinute - 0-59
        Returns:
        CalendarDate
      • withDoy

        public static CalendarDate withDoy​(Calendar cal,
                                           int year,
                                           int doy,
                                           int hourOfDay,
                                           int minuteOfHour,
                                           int secondOfMinute)
      • of

        public static CalendarDate of​(Date date)
        Create CalendarDate from a java.util.Date. Uses standard Calendar.
        Parameters:
        date - java.util.Date
        Returns:
        CalendarDate in UTC
      • of

        public static CalendarDate of​(long msecs)
        Create CalendarDate from msecs since epoch Uses standard Calendar.
        Parameters:
        msecs - milliseconds from 1970-01-01T00:00:00Z
        Returns:
        CalendarDate in UTC
      • of

        public static CalendarDate of​(Calendar cal,
                                      long msecs)
        Create CalendarDate from msecs since epoch Uses the given Calendar.
        Parameters:
        cal - calendar to use, or null for default
        msecs - milliseconds from 1970-01-01T00:00:00Z
        Returns:
        CalendarDate in UTC time zone.
      • parseUdunitsOrIso

        @Nullable
        public static CalendarDate parseUdunitsOrIso​(String calendarName,
                                                     String isoOrUdunits)
        Get CalendarDate from ISO date string
        Parameters:
        calendarName - get Calendar from Calendar.get(calendarName). may be null
        isoOrUdunits - ISO or udunits date string
        Returns:
        CalendarDate or null if not valid
      • parseISOformat

        public static CalendarDate parseISOformat​(String calendarName,
                                                  String isoDateString)
        Get CalendarDate from ISO date string
        Parameters:
        calendarName - get Calendar from Calendar.get(calendarName). may be null
        isoDateString - ISO date string
        Returns:
        CalendarDate
      • parseUdunits

        public static CalendarDate parseUdunits​(String calendarName,
                                                String udunits)
        Get CalendarDate from udunit date string
        Parameters:
        calendarName - get Calendar from Calendar.get(calendarName). may be null
        udunits - must be value (space) udunits string
        Returns:
        CalendarDate
      • getCalendar

        public Calendar getCalendar()
      • getMillis

        public long getMillis()
        Gets the milliseconds of the datetime instant from the Java epoch of 1970-01-01T00:00:00Z.
        Returns:
        the number of milliseconds since 1970-01-01T00:00:00Z
      • hashCode

        public int hashCode()
        Overrides:
        hashCode in class Object
      • toString

        public String toString()
        ISO formatted string
        Overrides:
        toString in class Object
        Returns:
        ISO8601 format (yyyy-MM-ddTHH:mm:ss.SSSZ)
      • getTimeUnits

        public String getTimeUnits()
        udunits formatting
        Returns:
        udunits formatted date
      • getHourOfDay

        public int getHourOfDay()
        Get the hour of day (0-23) field for this chronology.
        Returns:
        hour of day (0-23)
      • getDayOfMonth

        public int getDayOfMonth()
      • truncate

        public CalendarDate truncate​(CalendarPeriod.Field fld)
        truncate the CalendarDate, by zeroing all the fields that are less than the field. So 2013-03-01T19:30 becomes 2013-03-01T00:00 if the field is "day"
        Parameters:
        fld - set to 0 all fields less than this one
        Returns:
        truncated result
      • toDate

        public Date toDate()
        Get the equivilent java.util.Date
        Returns:
        the equivalent Date
      • getDifferenceInMsecs

        public long getDifferenceInMsecs​(CalendarDate o)
        Get difference between two calendar dates in millisecs
        Parameters:
        o - other calendar date
        Returns:
        (this minus o) difference in millisecs
      • getDifference

        public long getDifference​(CalendarDate o,
                                  CalendarPeriod.Field fld)
        Get difference between two calendar dates in given Field units
        Parameters:
        o - other calendar date
        Returns:
        (this minus o) difference in units of this Field