Class Time


  • public final class Time
    extends java.lang.Object
    Time identifies a date and time.

    Here is an example of Time being used to display the current date:

     Time t = new Time();
     ...
     label.setText("Today is " + t.year + "/" + t.month + "/" + t.day);
     
    You can also call the update method to update the time with the current system values.
    • Field Summary

      Fields 
      Modifier and Type Field Description
      int day
      The day in the range of 1 to the last day in the month.
      int hour
      The hour in the range of 0 to 23.
      int millis
      Milliseconds in the range of 0 to 999.
      int minute
      The minute in the range of 0 to 59.
      int month
      The month in the range of 1 to 12.
      int second
      The second in the range of 0 to 59.
      static int SECONDS_PER_DAY  
      int year
      The year as its full set of digits (year 2010 is 2010).
    • Constructor Summary

      Constructors 
      Constructor Description
      Time()
      Constructs a time object set to the current date and time.
      Time​(char[] sqlTime)
      Creates a Time object from an SQL String.
      Time​(int yyyymmdd, int hhmmssmmm)
      Constructs a time object from the given date and time values.
      Time​(int year, int month, int day, int hour, int minute, int second, int millis)
      Constructs a new time with the given values.
      Time​(long yyyymmddhhmmss)
      Constructs a time object from the given value.
      Time​(long time, boolean b)
      Creates a time in the SQL' format.
      Time​(java.lang.String iso8601)
      Creates a Time object with a String in the given Iso8601 format:
      Time​(java.lang.String time, boolean hasYear, boolean hasMonth, boolean hasDay, boolean hasHour, boolean hasMinute, boolean hasSeconds)
      Constructs a Time object, parsing the String and placing the fields depending on the flags that were set, using the Settings.timeSeparator as spliter.
      Time​(java.lang.String time, boolean hasYear, boolean hasMonth, boolean hasDay, boolean hasHour, boolean hasMinute, boolean hasSeconds, byte dateFormat)
      Constructs a Time object, parsing the String and placing the fields depending on the flags that were set, using the Settings.timeSeparator as spliter.
      Time​(Date d)
      Constructs a time object from a Date, zeroing the hour/minute/second and millis
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      java.lang.StringBuffer dump​(java.lang.StringBuffer sb, java.lang.String timeSeparator, boolean includeMillis)
      Dumps the time into the given StringBuffer.
      boolean equals​(java.lang.Object o)  
      long getSQLLong()
      Returns the time in the format YYYYMMDDHHmmSSmmm as a long value.
      java.lang.String getSQLString()
      Returns this date in the format YYYY-MM-DD HH:mm:SS.mmm
      java.lang.String getSQLString​(java.lang.StringBuffer sb)
      Returns this date in the format YYYY-MM-DD HH:mm:SS.mmm
      long getTime()
      Returns the number of millis since 1/1/1970
      long getTimeLong()
      Returns the time in the format YYYYMMDDHHMMSS as a long value.
      void inc​(int hours, int minutes, int seconds)
      Increments or decrements the fields below.
      boolean isValid()
      Returns true if the time is valid.
      java.lang.String toIso8601()
      Converts this time object to a string in the Iso8601 format:
      java.lang.String toString()
      Returns the time in format specified in totalcross.sys.Settings (does NOT include the date neither the millis).
      java.lang.String toString​(java.lang.String timeSeparator)
      Returns the time in format specified in totalcross.sys.Settings (does NOT include the date neither the millis).
      void update()
      Updates the internal fields with the current timestamp.
      • Methods inherited from class java.lang.Object

        clone, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
    • Field Detail

      • year

        public int year
        The year as its full set of digits (year 2010 is 2010).
      • month

        public int month
        The month in the range of 1 to 12.
      • day

        public int day
        The day in the range of 1 to the last day in the month.
      • hour

        public int hour
        The hour in the range of 0 to 23.
      • minute

        public int minute
        The minute in the range of 0 to 59.
      • second

        public int second
        The second in the range of 0 to 59.
      • millis

        public int millis
        Milliseconds in the range of 0 to 999.
    • Constructor Detail

      • Time

        public Time()
        Constructs a time object set to the current date and time.
      • Time

        public Time​(Date d)
        Constructs a time object from a Date, zeroing the hour/minute/second and millis
        Since:
        TotalCross 2.0
      • Time

        public Time​(long yyyymmddhhmmss)
        Constructs a time object from the given value.
        Since:
        SuperWaba 4.0
        See Also:
        getTimeLong()
      • Time

        public Time​(int yyyymmdd,
                    int hhmmssmmm)
        Constructs a time object from the given date and time values.
        Since:
        TotalCross 1.0 beta 4
      • Time

        public Time​(char[] sqlTime)
             throws InvalidDateException
        Creates a Time object from an SQL String. It accepts any character as separator and also with less fields, based that the sequence is always YY-MM-DD hh:mm:ss.mmm.
        Throws:
        InvalidDateException
        Since:
        TotalCross 3.1
      • Time

        public Time​(long time,
                    boolean b)
             throws InvalidDateException
        Creates a time in the SQL' format.
        Parameters:
        time - Number of millis since 1/1/1970
        b - Here only to differ from the other constructor that receives a long and its ignored.
        Throws:
        InvalidDateException
        Since:
        TotalCross 2.1
      • Time

        public Time​(java.lang.String time,
                    boolean hasYear,
                    boolean hasMonth,
                    boolean hasDay,
                    boolean hasHour,
                    boolean hasMinute,
                    boolean hasSeconds)
             throws InvalidNumberException
        Constructs a Time object, parsing the String and placing the fields depending on the flags that were set, using the Settings.timeSeparator as spliter. The number of parts must match the number of true flags, or an ArrayIndexOutOfBoundsException will be thrown. AM/PM is supported.
        Throws:
        InvalidNumberException
        Since:
        TotalCross 1.3
      • Time

        public Time​(java.lang.String time,
                    boolean hasYear,
                    boolean hasMonth,
                    boolean hasDay,
                    boolean hasHour,
                    boolean hasMinute,
                    boolean hasSeconds,
                    byte dateFormat)
             throws InvalidNumberException
        Constructs a Time object, parsing the String and placing the fields depending on the flags that were set, using the Settings.timeSeparator as spliter. The number of parts must match the number of true flags, or an ArrayIndexOutOfBoundsException will be thrown. The dateFormat is only considered if hasYear, hasMonth and hasDay are all true. AM/PM is supported.
        Throws:
        InvalidNumberException
        Since:
        TotalCross 2.1
      • Time

        public Time​(int year,
                    int month,
                    int day,
                    int hour,
                    int minute,
                    int second,
                    int millis)
        Constructs a new time with the given values. The values are not checked.
        Since:
        SuperWaba 3.5
    • Method Detail

      • update

        public void update()
        Updates the internal fields with the current timestamp.
        Since:
        TotalCross 1.23
      • getTimeLong

        public long getTimeLong()
        Returns the time in the format YYYYMMDDHHMMSS as a long value. It does not include the millis.
        Since:
        SuperWaba 4.0
      • getSQLLong

        public long getSQLLong()
        Returns the time in the format YYYYMMDDHHmmSSmmm as a long value. It does include the millis.
        Since:
        TotalCross 2.0
      • getSQLString

        public java.lang.String getSQLString()
        Returns this date in the format YYYY-MM-DD HH:mm:SS.mmm
        Since:
        TotalCross 2.0
      • getSQLString

        public java.lang.String getSQLString​(java.lang.StringBuffer sb)
        Returns this date in the format YYYY-MM-DD HH:mm:SS.mmm
        Since:
        TotalCross 2.0
      • toString

        public java.lang.String toString()
        Returns the time in format specified in totalcross.sys.Settings (does NOT include the date neither the millis). To return the date, use class totalcross.util.Date. So, to get a String with the date and time, use:
         Time t = new Time();
         String dateAndTime = new Date(t) + " " + t;
         
        Overrides:
        toString in class java.lang.Object
      • toString

        public java.lang.String toString​(java.lang.String timeSeparator)
        Returns the time in format specified in totalcross.sys.Settings (does NOT include the date neither the millis). To return the date, use class totalcross.util.Date. So, to get a String with the date and time, use:
         Time t = new Time();
         String dateAndTime = new Date(t) + " " + t;
         
        Since:
        TotalCross 1.15
      • dump

        public java.lang.StringBuffer dump​(java.lang.StringBuffer sb,
                                           java.lang.String timeSeparator,
                                           boolean includeMillis)
        Dumps the time into the given StringBuffer.
        Since:
        TotalCross 1.24
      • equals

        public boolean equals​(java.lang.Object o)
        Overrides:
        equals in class java.lang.Object
      • toIso8601

        public java.lang.String toIso8601()
        Converts this time object to a string in the Iso8601 format:
        YYYYMMDDTHH:MM:SS
        .
        Since:
        SuperWaba 5.61
      • isValid

        public boolean isValid()
        Returns true if the time is valid. Note that the date part is NOT checked, only hour, minute, second and millis are checked against valid ranges.
        Since:
        TotalCross 1.22
      • inc

        public void inc​(int hours,
                        int minutes,
                        int seconds)
        Increments or decrements the fields below. Note that this method DOES update the day/month/year fields, constructing a Date object and manipulating it; consider reusing a Time object if you use this method.

        Parameters can be positive (to increment), zero (to keep it), or negative (to decrement).
        Since:
        TotalCross 1.24