Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
   /*
    *  Copyright 2001-2011 Stephen Colebourne
    *
    *  Licensed under the Apache License, Version 2.0 (the "License");
    *  you may not use this file except in compliance with the License.
    *  You may obtain a copy of the License at
    *
    *      http://www.apache.org/licenses/LICENSE-2.0
    *
   *  Unless required by applicable law or agreed to in writing, software
   *  distributed under the License is distributed on an "AS IS" BASIS,
   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   *  See the License for the specific language governing permissions and
   *  limitations under the License.
   */
  package org.joda.time;
  
  import java.util.Date;
  import java.util.Locale;
  
TimeOfDay is an immutable partial supporting the hour, minute, second and millisecond fields.

NOTE: This class only supports the four fields listed above. Thus, you cannot query the millisOfDay or secondOfDay fields for example. The new LocalTime class removes this restriction.

Calculations on TimeOfDay are performed using a Chronology. This chronology is set to be in the UTC time zone for all calculations.

Each individual field can be queried in two ways:

  • getHourOfDay()
  • hourOfDay().get()
The second technique also provides access to other useful methods on the field:
  • numeric value - hourOfDay().get()
  • text value - hourOfDay().getAsText()
  • short text value - hourOfDay().getAsShortText()
  • maximum/minimum values - hourOfDay().getMaximumValue()
  • add/subtract - hourOfDay().addToCopy()
  • set - hourOfDay().setCopy()

TimeOfDay is thread-safe and immutable, provided that the Chronology is as well. All standard Chronology classes supplied are thread-safe and immutable.

Deprecated:
Use LocalTime which has a much better internal implementation and has been available since 1.3
Author(s):
Stephen Colebourne
Brian S O'Neill
Since:
1.0
  
  public final class TimeOfDay
          extends BasePartial
          implements ReadablePartialSerializable {
      // NOTE: No toDateTime(YearMonthDay) as semantics are confusing when
      // different chronologies
  
    
Serialization version
  
      private static final long serialVersionUID = 3633353405803318660L;
    
The singleton set of field types
  
      private static final DateTimeFieldType[] FIELD_TYPES = new DateTimeFieldType[] {
          DateTimeFieldType.hourOfDay(),
          DateTimeFieldType.minuteOfHour(),
          DateTimeFieldType.secondOfMinute(),
          DateTimeFieldType.millisOfSecond(),
      };

    
Constant for midnight.
  
      public static final TimeOfDay MIDNIGHT = new TimeOfDay(0, 0, 0, 0);

    
The index of the hourOfDay field in the field array
  
      public static final int HOUR_OF_DAY = 0;
    
The index of the minuteOfHour field in the field array
  
      public static final int MINUTE_OF_HOUR = 1;
    
The index of the secondOfMinute field in the field array
  
      public static final int SECOND_OF_MINUTE = 2;
    
The index of the millisOfSecond field in the field array
  
      public static final int MILLIS_OF_SECOND = 3;
  
      //-----------------------------------------------------------------------
      
Constructs a TimeOfDay from a java.util.Calendar using exactly the same field values avoiding any time zone effects.

Each field is queried from the Calendar and assigned to the TimeOfDay. This is useful to ensure that the field values are the same in the created TimeOfDay no matter what the time zone is. For example, if the Calendar states that the time is 04:29, then the created TimeOfDay will always have the time 04:29 irrespective of time zone issues.

This factory method ignores the type of the calendar and always creates a TimeOfDay with ISO chronology.

Parameters:
calendar the Calendar to extract fields from
Returns:
the created TimeOfDay
Throws:
java.lang.IllegalArgumentException if the calendar is null
java.lang.IllegalArgumentException if the time is invalid for the ISO chronology
Since:
1.2
 
     public static TimeOfDay fromCalendarFields(Calendar calendar) {
         if (calendar == null) {
             throw new IllegalArgumentException("The calendar must not be null");
         }
         return new TimeOfDay(
             calendar.get(.),
             calendar.get(.),
             calendar.get(.),
             calendar.get(.)
         );
     }

    
Constructs a TimeOfDay from a java.util.Date using exactly the same field values avoiding any time zone effects.

Each field is queried from the Date and assigned to the TimeOfDay. This is useful to ensure that the field values are the same in the created TimeOfDay no matter what the time zone is. For example, if the Calendar states that the time is 04:29, then the created TimeOfDay will always have the time 04:29 irrespective of time zone issues.

This factory method always creates a TimeOfDay with ISO chronology.

Parameters:
date the Date to extract fields from
Returns:
the created TimeOfDay
Throws:
java.lang.IllegalArgumentException if the calendar is null
java.lang.IllegalArgumentException if the date is invalid for the ISO chronology
Since:
1.2
 
     public static TimeOfDay fromDateFields(Date date) {
         if (date == null) {
             throw new IllegalArgumentException("The date must not be null");
         }
         return new TimeOfDay(
             date.getHours(),
             date.getMinutes(),
             date.getSeconds(),
             (((int) (date.getTime() % 1000)) + 1000) % 1000
         );
     }
 
     //-----------------------------------------------------------------------
     
Constructs a TimeOfDay from the specified millis of day using the ISO chronology.

The millisOfDay value may exceed the number of millis in one day, but additional days will be ignored. This method uses the UTC time zone internally.

Parameters:
millisOfDay the number of milliseconds into a day to convert
 
     public static TimeOfDay fromMillisOfDay(long millisOfDay) {
         return fromMillisOfDay(millisOfDaynull);
     }

    
Constructs a TimeOfDay from the specified millis of day using the specified chronology.

The millisOfDay value may exceed the number of millis in one day, but additional days will be ignored. This method uses the UTC time zone internally.

Parameters:
millisOfDay the number of milliseconds into a day to convert
chrono the chronology, null means ISO chronology
 
     public static TimeOfDay fromMillisOfDay(long millisOfDayChronology chrono) {
         chrono = DateTimeUtils.getChronology(chrono);
         chrono = chrono.withUTC();
         return new TimeOfDay(millisOfDaychrono);
     }
 
     // Constructors
     //-----------------------------------------------------------------------
     
Constructs a TimeOfDay with the current time, using ISOChronology in the default zone to extract the fields.

The constructor uses the default time zone, resulting in the local time being initialised. Once the constructor is complete, all further calculations are performed without reference to a timezone (by switching to UTC).

 
     public TimeOfDay() {
         super();
     }

    
Constructs a TimeOfDay with the current time, using ISOChronology in the specified zone to extract the fields.

The constructor uses the specified time zone to obtain the current time. Once the constructor is complete, all further calculations are performed without reference to a timezone (by switching to UTC).

Parameters:
zone the zone to use, null means default zone
Since:
1.1
 
     public TimeOfDay(DateTimeZone zone) {
         super(ISOChronology.getInstance(zone));
     }

    
Constructs a TimeOfDay with the current time, using the specified chronology and zone to extract the fields.

The constructor uses the time zone of the chronology specified. Once the constructor is complete, all further calculations are performed without reference to a timezone (by switching to UTC).

Parameters:
chronology the chronology, null means ISOChronology in the default zone
 
     public TimeOfDay(Chronology chronology) {
         super(chronology);
     }

    
Constructs a TimeOfDay extracting the partial fields from the specified milliseconds using the ISOChronology in the default zone.

The constructor uses the default time zone, resulting in the local time being initialised. Once the constructor is complete, all further calculations are performed without reference to a timezone (by switching to UTC).

Parameters:
instant the milliseconds from 1970-01-01T00:00:00Z
 
     public TimeOfDay(long instant) {
         super(instant);
     }

    
Constructs a TimeOfDay extracting the partial fields from the specified milliseconds using the chronology provided.

The constructor uses the time zone of the chronology specified. Once the constructor is complete, all further calculations are performed without reference to a timezone (by switching to UTC).

Parameters:
instant the milliseconds from 1970-01-01T00:00:00Z
chronology the chronology, null means ISOChronology in the default zone
 
     public TimeOfDay(long instantChronology chronology) {
         super(instantchronology);
     }

    
Constructs a TimeOfDay from an Object that represents a time.

The recognised object types are defined in ConverterManager and include ReadableInstant, String, Calendar and Date. The String formats are described by org.joda.time.format.ISODateTimeFormat.timeParser().

The chronology used will be derived from the object, defaulting to ISO.

NOTE: Prior to v1.3 the string format was described by org.joda.time.format.ISODateTimeFormat.dateTimeParser(). Dates are now rejected.

Parameters:
instant the datetime object, null means now
Throws:
java.lang.IllegalArgumentException if the instant is invalid
 
     public TimeOfDay(Object instant) {
         super(instantnull, ISODateTimeFormat.timeParser());
     }

    
Constructs a TimeOfDay from an Object that represents a time, using the specified chronology.

The recognised object types are defined in ConverterManager and include ReadableInstant, String, Calendar and Date. The String formats are described by org.joda.time.format.ISODateTimeFormat.timeParser().

The constructor uses the time zone of the chronology specified. Once the constructor is complete, all further calculations are performed without reference to a timezone (by switching to UTC). The specified chronology overrides that of the object.

NOTE: Prior to v1.3 the string format was described by org.joda.time.format.ISODateTimeFormat.dateTimeParser(). Dates are now rejected.

Parameters:
instant the datetime object, null means now
chronology the chronology, null means ISO default
Throws:
java.lang.IllegalArgumentException if the instant is invalid
 
     public TimeOfDay(Object instantChronology chronology) {
         super(instant, DateTimeUtils.getChronology(chronology), ISODateTimeFormat.timeParser());
     }

    
Constructs a TimeOfDay with specified hour and minute and zero seconds and milliseconds using ISOChronology in the default zone.

The constructor uses the no time zone initialising the fields as provided. Once the constructor is complete, all further calculations are performed without reference to a timezone (by switching to UTC).

Parameters:
hourOfDay the hour of the day
minuteOfHour the minute of the hour
 
     public TimeOfDay(int hourOfDayint minuteOfHour) {
         this(hourOfDayminuteOfHour, 0, 0, null);
     }

    
Constructs a TimeOfDay with specified hour and minute and zero seconds and milliseconds.

The constructor uses the time zone of the chronology specified. Once the constructor is complete, all further calculations are performed without reference to a timezone (by switching to UTC).

Parameters:
hourOfDay the hour of the day
minuteOfHour the minute of the hour
chronology the chronology, null means ISOChronology in the default zone
 
     public TimeOfDay(int hourOfDayint minuteOfHourChronology chronology) {
         this(hourOfDayminuteOfHour, 0, 0, chronology);
     }

    
Constructs a TimeOfDay with specified time field values and zero milliseconds using ISOChronology in the default zone.

The constructor uses the no time zone initialising the fields as provided. Once the constructor is complete, all further calculations are performed without reference to a timezone (by switching to UTC).

Parameters:
hourOfDay the hour of the day
minuteOfHour the minute of the hour
secondOfMinute the second of the minute
 
     public TimeOfDay(int hourOfDayint minuteOfHourint secondOfMinute) {
         this(hourOfDayminuteOfHoursecondOfMinute, 0, null);
     }

    
Constructs a TimeOfDay with specified time field values and zero milliseconds.

The constructor uses the time zone of the chronology specified. Once the constructor is complete, all further calculations are performed without reference to a timezone (by switching to UTC).

Parameters:
hourOfDay the hour of the day
minuteOfHour the minute of the hour
secondOfMinute the second of the minute
chronology the chronology, null means ISOChronology in the default zone
 
     public TimeOfDay(int hourOfDayint minuteOfHourint secondOfMinuteChronology chronology) {
         this(hourOfDayminuteOfHoursecondOfMinute, 0, chronology);
     }

    
Constructs a TimeOfDay with specified time field values using ISOChronology in the default zone.

The constructor uses the no time zone initialising the fields as provided. Once the constructor is complete, all further calculations are performed without reference to a timezone (by switching to UTC).

Parameters:
hourOfDay the hour of the day
minuteOfHour the minute of the hour
secondOfMinute the second of the minute
millisOfSecond the millisecond of the second
 
     public TimeOfDay(int hourOfDayint minuteOfHourint secondOfMinuteint millisOfSecond) {
         this(hourOfDayminuteOfHoursecondOfMinutemillisOfSecondnull);
     }

    
Constructs a TimeOfDay with specified time field values and chronology.

The constructor uses the time zone of the chronology specified. Once the constructor is complete, all further calculations are performed without reference to a timezone (by switching to UTC).

Parameters:
hourOfDay the hour of the day
minuteOfHour the minute of the hour
secondOfMinute the second of the minute
millisOfSecond the millisecond of the second
chronology the chronology, null means ISOChronology in the default zone
 
     public TimeOfDay(int hourOfDayint minuteOfHour,
             int secondOfMinuteint millisOfSecondChronology chronology) {
         super(new int[] {hourOfDayminuteOfHoursecondOfMinutemillisOfSecond}, chronology);
     }

    
Constructs a TimeOfDay with chronology from this instance and new values.

Parameters:
partial the partial to base this new instance on
values the new set of values
 
     TimeOfDay(TimeOfDay partialint[] values) {
         super(partialvalues);
     }

    
Constructs a TimeOfDay with values from this instance and a new chronology.

Parameters:
partial the partial to base this new instance on
chrono the new chronology
 
     TimeOfDay(TimeOfDay partialChronology chrono) {
         super(partialchrono);
     }
 
     //-----------------------------------------------------------------------
     
Gets the number of fields in this partial.

Returns:
the field count
 
     public int size() {
         return 4;
     }

    
Gets the field for a specific index in the chronology specified.

This method must not use any instance variables.

Parameters:
index the index to retrieve
chrono the chronology to use
Returns:
the field
 
     protected DateTimeField getField(int indexChronology chrono) {
         switch (index) {
             case :
                 return chrono.hourOfDay();
             case :
                 return chrono.minuteOfHour();
             case :
                 return chrono.secondOfMinute();
             case :
                 return chrono.millisOfSecond();
             default:
                 throw new IndexOutOfBoundsException("Invalid index: " + index);
         }
     }

    
Gets the field type at the specified index.

Parameters:
index the index to retrieve
Returns:
the field at the specified index
Throws:
java.lang.IndexOutOfBoundsException if the index is invalid
 
     public DateTimeFieldType getFieldType(int index) {
         return [index];
     }

    
Gets an array of the field type of each of the fields that this partial supports.

The fields are returned largest to smallest, Hour, Minute, Second, Millis.

Returns:
the array of field types (cloned), largest to smallest
 
     public DateTimeFieldType[] getFieldTypes() {
         return (DateTimeFieldType[]) .clone();
     }
 
     //-----------------------------------------------------------------------
     
Returns a copy of this time with the specified chronology. This instance is immutable and unaffected by this method call.

This method retains the values of the fields, thus the result will typically refer to a different instant.

The time zone of the specified chronology is ignored, as TimeOfDay operates without a time zone.

Parameters:
newChronology the new chronology, null means ISO
Returns:
a copy of this datetime with a different chronology
Throws:
java.lang.IllegalArgumentException if the values are invalid for the new chronology
 
     public TimeOfDay withChronologyRetainFields(Chronology newChronology) {
         newChronology = DateTimeUtils.getChronology(newChronology);
         newChronology = newChronology.withUTC();
         if (newChronology == getChronology()) {
             return this;
         } else {
             TimeOfDay newTimeOfDay = new TimeOfDay(thisnewChronology);
             newChronology.validate(newTimeOfDaygetValues());
             return newTimeOfDay;
         }
     }

    
Returns a copy of this time with the specified field set to a new value.

For example, if the field type is minuteOfHour then the day would be changed in the returned instance.

These three lines are equivalent:

 TimeOfDay updated = tod.withField(DateTimeFieldType.minuteOfHour(), 6);
 TimeOfDay updated = tod.minuteOfHour().setCopy(6);
 TimeOfDay updated = tod.property(DateTimeFieldType.minuteOfHour()).setCopy(6);
 

Parameters:
fieldType the field type to set, not null
value the value to set
Returns:
a copy of this instance with the field set
Throws:
java.lang.IllegalArgumentException if the value is null or invalid
 
     public TimeOfDay withField(DateTimeFieldType fieldTypeint value) {
         int index = indexOfSupported(fieldType);
         if (value == getValue(index)) {
             return this;
         }
         int[] newValues = getValues();
         newValues = getField(index).set(thisindexnewValuesvalue);
         return new TimeOfDay(thisnewValues);
     }

    
Returns a copy of this time with the value of the specified field increased, wrapping to what would be a new day if required.

If the addition is zero, then this is returned.

These three lines are equivalent:

 TimeOfDay added = tod.withFieldAdded(DurationFieldType.minutes(), 6);
 TimeOfDay added = tod.plusMinutes(6);
 TimeOfDay added = tod.minuteOfHour().addToCopy(6);
 

Parameters:
fieldType the field type to add to, not null
amount the amount to add
Returns:
a copy of this instance with the field updated
Throws:
java.lang.IllegalArgumentException if the value is null or invalid
java.lang.ArithmeticException if the new datetime exceeds the capacity
 
     public TimeOfDay withFieldAdded(DurationFieldType fieldTypeint amount) {
         int index = indexOfSupported(fieldType);
         if (amount == 0) {
             return this;
         }
         int[] newValues = getValues();
         newValues = getField(index).addWrapPartial(thisindexnewValuesamount);
         return new TimeOfDay(thisnewValues);
     }

    
Returns a copy of this time with the specified period added, wrapping to what would be a new day if required.

If the addition is zero, then this is returned. Fields in the period that aren't present in the partial are ignored.

This method is typically used to add multiple copies of complex period instances. Adding one field is best achieved using methods like withFieldAdded(org.joda.time.DurationFieldType,int) or plusHours(int).

Parameters:
period the period to add to this one, null means zero
scalar the amount of times to add, such as -1 to subtract once
Returns:
a copy of this instance with the period added
Throws:
java.lang.ArithmeticException if the new datetime exceeds the capacity
 
     public TimeOfDay withPeriodAdded(ReadablePeriod periodint scalar) {
         if (period == null || scalar == 0) {
             return this;
         }
         int[] newValues = getValues();
         for (int i = 0; i < period.size(); i++) {
             DurationFieldType fieldType = period.getFieldType(i);
             int index = indexOf(fieldType);
             if (index >= 0) {
                 newValues = getField(index).addWrapPartial(thisindexnewValues,
                         FieldUtils.safeMultiply(period.getValue(i), scalar));
             }
         }
         return new TimeOfDay(thisnewValues);
     }
 
     //-----------------------------------------------------------------------
     
Returns a copy of this time with the specified period added, wrapping to what would be a new day if required.

If the amount is zero or null, then this is returned.

This method is typically used to add complex period instances. Adding one field is best achieved using methods like plusHours(int).

Parameters:
period the duration to add to this one, null means zero
Returns:
a copy of this instance with the period added
Throws:
java.lang.ArithmeticException if the new datetime exceeds the capacity of a long
 
     public TimeOfDay plus(ReadablePeriod period) {
         return withPeriodAdded(period, 1);
     }
 
     //-----------------------------------------------------------------------
     
Returns a copy of this time plus the specified number of hours.

This time instance is immutable and unaffected by this method call.

The following three lines are identical in effect:

 TimeOfDay added = dt.plusHours(6);
 TimeOfDay added = dt.plus(Period.hours(6));
 TimeOfDay added = dt.withFieldAdded(DurationFieldType.hours(), 6);
 

Parameters:
hours the amount of hours to add, may be negative
Returns:
the new time plus the increased hours
Since:
1.1
 
     public TimeOfDay plusHours(int hours) {
         return withFieldAdded(DurationFieldType.hours(), hours);
     }

    
Returns a copy of this time plus the specified number of minutes.

This time instance is immutable and unaffected by this method call.

The following three lines are identical in effect:

 TimeOfDay added = dt.plusMinutes(6);
 TimeOfDay added = dt.plus(Period.minutes(6));
 TimeOfDay added = dt.withFieldAdded(DurationFieldType.minutes(), 6);
 

Parameters:
minutes the amount of minutes to add, may be negative
Returns:
the new time plus the increased minutes
Since:
1.1
 
     public TimeOfDay plusMinutes(int minutes) {
         return withFieldAdded(DurationFieldType.minutes(), minutes);
     }

    
Returns a copy of this time plus the specified number of seconds.

This time instance is immutable and unaffected by this method call.

The following three lines are identical in effect:

 TimeOfDay added = dt.plusSeconds(6);
 TimeOfDay added = dt.plus(Period.seconds(6));
 TimeOfDay added = dt.withFieldAdded(DurationFieldType.seconds(), 6);
 

Parameters:
seconds the amount of seconds to add, may be negative
Returns:
the new time plus the increased seconds
Since:
1.1
 
     public TimeOfDay plusSeconds(int seconds) {
         return withFieldAdded(DurationFieldType.seconds(), seconds);
     }

    
Returns a copy of this time plus the specified number of millis.

This time instance is immutable and unaffected by this method call.

The following three lines are identical in effect:

 TimeOfDay added = dt.plusMillis(6);
 TimeOfDay added = dt.plus(Period.millis(6));
 TimeOfDay added = dt.withFieldAdded(DurationFieldType.millis(), 6);
 

Parameters:
millis the amount of millis to add, may be negative
Returns:
the new time plus the increased millis
Since:
1.1
 
     public TimeOfDay plusMillis(int millis) {
         return withFieldAdded(DurationFieldType.millis(), millis);
     }
 
     //-----------------------------------------------------------------------
     
Returns a copy of this time with the specified period taken away, wrapping to what would be a new day if required.

If the amount is zero or null, then this is returned.

This method is typically used to subtract complex period instances. Subtracting one field is best achieved using methods like minusHours(int).

Parameters:
period the period to reduce this instant by
Returns:
a copy of this instance with the period taken away
Throws:
java.lang.ArithmeticException if the new time exceeds capacity
 
     public TimeOfDay minus(ReadablePeriod period) {
         return withPeriodAdded(period, -1);
     }
 
     //-----------------------------------------------------------------------
     
Returns a copy of this time minus the specified number of hours.

This time instance is immutable and unaffected by this method call.

The following three lines are identical in effect:

 TimeOfDay subtracted = dt.minusHours(6);
 TimeOfDay subtracted = dt.minus(Period.hours(6));
 TimeOfDay subtracted = dt.withFieldAdded(DurationFieldType.hours(), -6);
 

Parameters:
hours the amount of hours to subtract, may be negative
Returns:
the new time minus the increased hours
Since:
1.1
 
     public TimeOfDay minusHours(int hours) {
         return withFieldAdded(DurationFieldType.hours(), FieldUtils.safeNegate(hours));
     }

    
Returns a copy of this time minus the specified number of minutes.

This time instance is immutable and unaffected by this method call.

The following three lines are identical in effect:

 TimeOfDay subtracted = dt.minusMinutes(6);
 TimeOfDay subtracted = dt.minus(Period.minutes(6));
 TimeOfDay subtracted = dt.withFieldAdded(DurationFieldType.minutes(), -6);
 

Parameters:
minutes the amount of minutes to subtract, may be negative
Returns:
the new time minus the increased minutes
Since:
1.1
 
     public TimeOfDay minusMinutes(int minutes) {
         return withFieldAdded(DurationFieldType.minutes(), FieldUtils.safeNegate(minutes));
     }

    
Returns a copy of this time minus the specified number of seconds.

This time instance is immutable and unaffected by this method call.

The following three lines are identical in effect:

 TimeOfDay subtracted = dt.minusSeconds(6);
 TimeOfDay subtracted = dt.minus(Period.seconds(6));
 TimeOfDay subtracted = dt.withFieldAdded(DurationFieldType.seconds(), -6);
 

Parameters:
seconds the amount of seconds to subtract, may be negative
Returns:
the new time minus the increased seconds
Since:
1.1
 
     public TimeOfDay minusSeconds(int seconds) {
         return withFieldAdded(DurationFieldType.seconds(), FieldUtils.safeNegate(seconds));
     }

    
Returns a copy of this time minus the specified number of millis.

This time instance is immutable and unaffected by this method call.

The following three lines are identical in effect:

 TimeOfDay subtracted = dt.minusMillis(6);
 TimeOfDay subtracted = dt.minus(Period.millis(6));
 TimeOfDay subtracted = dt.withFieldAdded(DurationFieldType.millis(), -6);
 

Parameters:
millis the amount of millis to subtract, may be negative
Returns:
the new time minus the increased millis
Since:
1.1
 
     public TimeOfDay minusMillis(int millis) {
         return withFieldAdded(DurationFieldType.millis(), FieldUtils.safeNegate(millis));
     }
 
     //-----------------------------------------------------------------------
     
Gets the property object for the specified type, which contains many useful methods.

Parameters:
type the field type to get the property for
Returns:
the property object
Throws:
java.lang.IllegalArgumentException if the field is null or unsupported
 
     public Property property(DateTimeFieldType type) {
         return new Property(thisindexOfSupported(type));
     }
 
     //-----------------------------------------------------------------------
     
Converts this object to a LocalTime with the same time and chronology.

Returns:
a LocalTime with the same time and chronology
Since:
1.3
 
     public LocalTime toLocalTime() {
         return new LocalTime(getHourOfDay(), getMinuteOfHour(),
                 getSecondOfMinute(), getMillisOfSecond(), getChronology());
     }
 
     //-----------------------------------------------------------------------
     
Converts this partial to a full datetime using the default time zone setting the time fields from this instance and the date fields from the current time.

Returns:
this date as a datetime with the time as the current time
 
     public DateTime toDateTimeToday() {
         return toDateTimeToday(null);
     }

    
Converts this partial to a full datetime using the specified time zone setting the time fields from this instance and the date fields from the current time.

This method uses the chronology from this instance plus the time zone specified.

Parameters:
zone the zone to use, null means default
Returns:
this date as a datetime with the time as the current time
 
     public DateTime toDateTimeToday(DateTimeZone zone) {
         Chronology chrono = getChronology().withZone(zone);
         long instantMillis = DateTimeUtils.currentTimeMillis();
         long resolved = chrono.set(thisinstantMillis);
         return new DateTime(resolvedchrono);
     }
 
     //-----------------------------------------------------------------------
     
Get the hour of day (0-23) field value.

Returns:
the hour of day
 
     public int getHourOfDay() {
         return getValue();
     }

    
Get the minute of hour field value.

Returns:
the minute of hour
 
     public int getMinuteOfHour() {
         return getValue();
     }

    
Get the second of minute field value.

Returns:
the second of minute
 
     public int getSecondOfMinute() {
         return getValue();
     }

    
Get the millis of second field value.

Returns:
the millis of second
 
     public int getMillisOfSecond() {
         return getValue();
     }
 
     //-----------------------------------------------------------------------
     
Returns a copy of this time with the hour of day field updated.

TimeOfDay is immutable, so there are no set methods. Instead, this method returns a new instance with the value of hour of day changed.

Parameters:
hour the hour of day to set
Returns:
a copy of this object with the field set
Throws:
java.lang.IllegalArgumentException if the value is invalid
Since:
1.3
 
     public TimeOfDay withHourOfDay(int hour) {
         int[] newValues = getValues();
         newValues = getChronology().hourOfDay().set(thisnewValueshour);
         return new TimeOfDay(thisnewValues);
     }

    
Returns a copy of this time with the minute of hour field updated.

TimeOfDay is immutable, so there are no set methods. Instead, this method returns a new instance with the value of minute of hour changed.

Parameters:
minute the minute of hour to set
Returns:
a copy of this object with the field set
Throws:
java.lang.IllegalArgumentException if the value is invalid
Since:
1.3
 
     public TimeOfDay withMinuteOfHour(int minute) {
         int[] newValues = getValues();
         newValues = getChronology().minuteOfHour().set(thisnewValuesminute);
         return new TimeOfDay(thisnewValues);
     }

    
Returns a copy of this time with the second of minute field updated.

TimeOfDay is immutable, so there are no set methods. Instead, this method returns a new instance with the value of second of minute changed.

Parameters:
second the second of minute to set
Returns:
a copy of this object with the field set
Throws:
java.lang.IllegalArgumentException if the value is invalid
Since:
1.3
 
     public TimeOfDay withSecondOfMinute(int second) {
         int[] newValues = getValues();
         newValues = getChronology().secondOfMinute().set(thisnewValuessecond);
         return new TimeOfDay(thisnewValues);
     }

    
Returns a copy of this time with the millis of second field updated.

TimeOfDay is immutable, so there are no set methods. Instead, this method returns a new instance with the value of millis of second changed.

Parameters:
millis the millis of second to set
Returns:
a copy of this object with the field set
Throws:
java.lang.IllegalArgumentException if the value is invalid
Since:
1.3
 
     public TimeOfDay withMillisOfSecond(int millis) {
         int[] newValues = getValues();
         newValues = getChronology().millisOfSecond().set(thisnewValuesmillis);
         return new TimeOfDay(thisnewValues);
     }
 
     //-----------------------------------------------------------------------
     
Get the hour of day field property which provides access to advanced functionality.

Returns:
the hour of day property
 
     public Property hourOfDay() {
         return new Property(this);
     }

    
Get the minute of hour field property which provides access to advanced functionality.

Returns:
the minute of hour property
 
     public Property minuteOfHour() {
         return new Property(this);
     }

    
Get the second of minute field property which provides access to advanced functionality.

Returns:
the second of minute property
 
     public Property secondOfMinute() {
         return new Property(this);
     }

    
Get the millis of second property which provides access to advanced functionality.

Returns:
the millis of second property
 
     public Property millisOfSecond() {
         return new Property(this);
     }
 
     //-----------------------------------------------------------------------
     
Output the time in the ISO8601 format THH:mm:ss.SSS.

Returns:
ISO8601 formatted string
    public String toString() {
        return ISODateTimeFormat.tTime().print(this);
    }
    //-----------------------------------------------------------------------
    
The property class for TimeOfDay.

This class binds a TimeOfDay to a DateTimeField.

Deprecated:
Use LocalTime which has a much better internal implementation
Author(s):
Stephen Colebourne
Since:
1.0
    public static class Property extends AbstractPartialFieldProperty implements Serializable {

        
Serialization version
        private static final long serialVersionUID = 5598459141741063833L;

        
The partial
        private final TimeOfDay iTimeOfDay;
        
The field index
        private final int iFieldIndex;

        
Constructs a property.

Parameters:
partial the partial instance
fieldIndex the index in the partial
        Property(TimeOfDay partialint fieldIndex) {
            super();
             = partial;
             = fieldIndex;
        }

        
Gets the field that this property uses.

Returns:
the field
        public DateTimeField getField() {
            return .getField();
        }

        
Gets the partial that this property belongs to.

Returns:
the partial
        protected ReadablePartial getReadablePartial() {
            return ;
        }

        
Gets the partial that this property belongs to.

Returns:
the partial
        public TimeOfDay getTimeOfDay() {
            return ;
        }

        
Gets the value of this field.

Returns:
the field value
        public int get() {
            return .getValue();
        }
        //-----------------------------------------------------------------------
        
Adds to the value of this field in a copy of this TimeOfDay, wrapping to what would be the next day if necessary.

The value will be added to this field. If the value is too large to be added solely to this field then it will affect larger fields. Smaller fields are unaffected.

If the result would be too large, beyond 23:59:59:999, then the calculation wraps to 00:00:00.000. For the alternate strict behaviour with no wrapping see addNoWrapToCopy(int).

The TimeOfDay attached to this property is unchanged by this call. Instead, a new instance is returned.

Parameters:
valueToAdd the value to add to the field in the copy
Returns:
a copy of the TimeOfDay with the field value changed
Throws:
java.lang.IllegalArgumentException if the value isn't valid
        public TimeOfDay addToCopy(int valueToAdd) {
            int[] newValues = .getValues();
            newValues = getField().addWrapPartial(newValuesvalueToAdd);
            return new TimeOfDay(newValues);
        }

        
Adds to the value of this field in a copy of this TimeOfDay, throwing an Exception if the bounds are exceeded.

The value will be added to this field. If the value is too large to be added solely to this field then it will affect larger fields. Smaller fields are unaffected.

If the result would be too large (beyond 23:59:59:999) or too small (less than 00:00:00.000) then an Execption is thrown. For the alternate behaviour which wraps to the next 'day', see addToCopy(int).

The TimeOfDay attached to this property is unchanged by this call. Instead, a new instance is returned.

Parameters:
valueToAdd the value to add to the field in the copy
Returns:
a copy of the TimeOfDay with the field value changed
Throws:
java.lang.IllegalArgumentException if the value isn't valid
        public TimeOfDay addNoWrapToCopy(int valueToAdd) {
            int[] newValues = .getValues();
            newValues = getField().add(newValuesvalueToAdd);
            return new TimeOfDay(newValues);
        }

        
Adds to the value of this field in a copy of this TimeOfDay wrapping within this field if the maximum value is reached.

The value will be added to this field. If the value is too large to be added solely to this field then it wraps within this field. Other fields are unaffected.

For example, 12:59:37 addWrapField one minute returns 12:00:37.

The TimeOfDay attached to this property is unchanged by this call. Instead, a new instance is returned.

Parameters:
valueToAdd the value to add to the field in the copy
Returns:
a copy of the TimeOfDay with the field value changed
Throws:
java.lang.IllegalArgumentException if the value isn't valid
        public TimeOfDay addWrapFieldToCopy(int valueToAdd) {
            int[] newValues = .getValues();
            newValues = getField().addWrapField(newValuesvalueToAdd);
            return new TimeOfDay(newValues);
        }
        //-----------------------------------------------------------------------
        
Sets this field in a copy of the TimeOfDay.

The TimeOfDay attached to this property is unchanged by this call. Instead, a new instance is returned.

Parameters:
value the value to set the field in the copy to
Returns:
a copy of the TimeOfDay with the field value changed
Throws:
java.lang.IllegalArgumentException if the value isn't valid
        public TimeOfDay setCopy(int value) {
            int[] newValues = .getValues();
            newValues = getField().set(newValuesvalue);
            return new TimeOfDay(newValues);
        }

        
Sets this field in a copy of the TimeOfDay to a parsed text value.

The TimeOfDay attached to this property is unchanged by this call. Instead, a new instance is returned.

Parameters:
text the text value to set
locale optional locale to use for selecting a text symbol
Returns:
a copy of the TimeOfDay with the field value changed
Throws:
java.lang.IllegalArgumentException if the text value isn't valid
        public TimeOfDay setCopy(String textLocale locale) {
            int[] newValues = .getValues();
            newValues = getField().set(newValuestextlocale);
            return new TimeOfDay(newValues);
        }

        
Sets this field in a copy of the TimeOfDay to a parsed text value.

The TimeOfDay attached to this property is unchanged by this call. Instead, a new instance is returned.

Parameters:
text the text value to set
Returns:
a copy of the TimeOfDay with the field value changed
Throws:
java.lang.IllegalArgumentException if the text value isn't valid
        public TimeOfDay setCopy(String text) {
            return setCopy(textnull);
        }
        //-----------------------------------------------------------------------
        
Returns a new TimeOfDay with this field set to the maximum value for this field.

The TimeOfDay attached to this property is unchanged by this call.

Returns:
a copy of the TimeOfDay with this field set to its maximum
Since:
1.2
        public TimeOfDay withMaximumValue() {
            return setCopy(getMaximumValue());
        }

        
Returns a new TimeOfDay with this field set to the minimum value for this field.

The TimeOfDay attached to this property is unchanged by this call.

Returns:
a copy of the TimeOfDay with this field set to its minimum
Since:
1.2
        public TimeOfDay withMinimumValue() {
            return setCopy(getMinimumValue());
        }
    }
New to GrepCode? Check out our FAQ X