Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
   /*
    *  Copyright 2001-2010 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.Locale;
  
  import  org.joda.convert.FromString;
  import  org.joda.convert.ToString;
MutableDateTime is the standard implementation of a modifiable datetime class. It holds the datetime as milliseconds from the Java epoch of 1970-01-01T00:00:00Z.

This class uses a Chronology internally. The Chronology determines how the millisecond instant value is converted into the date time fields. The default Chronology is ISOChronology which is the agreed international standard and compatible with the modern Gregorian calendar.

Each individual field can be accessed in two ways:

  • getHourOfDay()
  • hourOfDay().get()
The second technique also provides access to other useful methods on the field:
  • get numeric value
  • set numeric value
  • add to numeric value
  • add to numeric value wrapping with the field
  • get text value
  • get short text value
  • set text value
  • field maximum value
  • field minimum value

MutableDateTime is mutable and not thread-safe, unless concurrent threads are not invoking mutator methods.

Author(s):
Guy Allard
Brian S O'Neill
Stephen Colebourne
Mike Schrag
Since:
1.0
See also:
DateTime
  
  public class MutableDateTime
          extends BaseDateTime
          implements ReadWritableDateTimeCloneableSerializable {

    
Serialization version
  
      private static final long serialVersionUID = 2852608688135209575L;

    
Rounding is disabled
  
      public static final int ROUND_NONE = 0;
    
Rounding mode as described by DateTimeField.roundFloor
  
      public static final int ROUND_FLOOR = 1;
    
Rounding mode as described by DateTimeField.roundCeiling
  
      public static final int ROUND_CEILING = 2;
    
Rounding mode as described by DateTimeField.roundHalfFloor
  
      public static final int ROUND_HALF_FLOOR = 3;
    
Rounding mode as described by DateTimeField.roundHalfCeiling
  
      public static final int ROUND_HALF_CEILING = 4;
    
Rounding mode as described by DateTimeField.roundHalfEven
  
      public static final int ROUND_HALF_EVEN = 5;

    
The field to round on
  
      private DateTimeField iRoundingField;
    
The mode of rounding
  
      private int iRoundingMode;
  
      //-----------------------------------------------------------------------
      
Obtains a MutableDateTime set to the current system millisecond time using ISOChronology in the default time zone.

Returns:
the current date-time, not null
Since:
2.0
 
     public static MutableDateTime now() {
         return new MutableDateTime();
     }

    
Obtains a MutableDateTime set to the current system millisecond time using ISOChronology in the specified time zone.

Parameters:
zone the time zone, not null
Returns:
the current date-time, not null
Since:
2.0
 
     public static MutableDateTime now(DateTimeZone zone) {
         if (zone == null) {
             throw new NullPointerException("Zone must not be null");
         }
         return new MutableDateTime(zone);
     }

    
Obtains a MutableDateTime set to the current system millisecond time using the specified chronology.

Parameters:
chronology the chronology, not null
Returns:
the current date-time, not null
Since:
2.0
 
     public static MutableDateTime now(Chronology chronology) {
         if (chronology == null) {
             throw new NullPointerException("Chronology must not be null");
         }
         return new MutableDateTime(chronology);
     }
 
     //-----------------------------------------------------------------------
     
Parses a MutableDateTime from the specified string.

This uses ISODateTimeFormat.dateTimeParser().

Parameters:
str the string to parse, not null
Since:
2.0
 
     @FromString
     public static MutableDateTime parse(String str) {
         return parse(str, ISODateTimeFormat.dateTimeParser().withOffsetParsed());
     }

    
Parses a MutableDateTime from the specified string using a formatter.

Parameters:
str the string to parse, not null
formatter the formatter to use, not null
Since:
2.0
 
     public static MutableDateTime parse(String strDateTimeFormatter formatter) {
         return formatter.parseDateTime(str).toMutableDateTime();
     }
 
     //-----------------------------------------------------------------------
     
Constructs an instance set to the current system millisecond time using ISOChronology in the default time zone.

See also:
now()
 
     public MutableDateTime() {
         super();
     }

    
Constructs an instance set to the current system millisecond time using ISOChronology in the specified time zone.

If the specified time zone is null, the default zone is used.

Parameters:
zone the time zone, null means default zone
See also:
now(DateTimeZone)
 
     public MutableDateTime(DateTimeZone zone) {
         super(zone);
     }

    
Constructs an instance set to the current system millisecond time using the specified chronology.

If the chronology is null, ISOChronology in the default time zone is used.

Parameters:
chronology the chronology, null means ISOChronology in default zone
See also:
now(Chronology)
 
     public MutableDateTime(Chronology chronology) {
         super(chronology);
     }
 
     //-----------------------------------------------------------------------
     
Constructs an instance set to the milliseconds from 1970-01-01T00:00:00Z using ISOChronology in the default time zone.

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

    
Constructs an instance set to the milliseconds from 1970-01-01T00:00:00Z using ISOChronology in the specified time zone.

If the specified time zone is null, the default zone is used.

Parameters:
instant the milliseconds from 1970-01-01T00:00:00Z
zone the time zone, null means default zone
 
     public MutableDateTime(long instantDateTimeZone zone) {
         super(instantzone);
     }

    
Constructs an instance set to the milliseconds from 1970-01-01T00:00:00Z using the specified chronology.

If the chronology is null, ISOChronology in the default time zone is used.

Parameters:
instant the milliseconds from 1970-01-01T00:00:00Z
chronology the chronology, null means ISOChronology in default zone
 
     public MutableDateTime(long instantChronology chronology) {
         super(instantchronology);
     }
 
     //-----------------------------------------------------------------------
     
Constructs an instance from an Object that represents a datetime.

If the object implies a chronology (such as GregorianCalendar does), then that chronology will be used. Otherwise, ISO default is used. Thus if a GregorianCalendar is passed in, the chronology used will be GJ, but if a Date is passed in the chronology will be ISO.

The recognised object types are defined in ConverterManager and include ReadableInstant, String, Calendar and Date.

Parameters:
instant the datetime object, null means now
Throws:
IllegalArgumentException if the instant is invalid
 
     public MutableDateTime(Object instant) {
         super(instant, (Chronologynull);
     }

    
Constructs an instance from an Object that represents a datetime, forcing the time zone to that specified.

If the object implies a chronology (such as GregorianCalendar does), then that chronology will be used, but with the time zone adjusted. Otherwise, ISO is used in the specified time zone. If the specified time zone is null, the default zone is used. Thus if a GregorianCalendar is passed in, the chronology used will be GJ, but if a Date is passed in the chronology will be ISO.

The recognised object types are defined in ConverterManager and include ReadableInstant, String, Calendar and Date.

Parameters:
instant the datetime object, null means now
zone the time zone, null means default time zone
Throws:
IllegalArgumentException if the instant is invalid
 
     public MutableDateTime(Object instantDateTimeZone zone) {
         super(instantzone);
     }

    
Constructs an instance from an Object that represents a datetime, using the specified chronology.

If the chronology is null, ISO in the default time zone is used. Any chronology implied by the object (such as GregorianCalendar does) is ignored.

The recognised object types are defined in ConverterManager and include ReadableInstant, String, Calendar and Date.

Parameters:
instant the datetime object, null means now
chronology the chronology, null means ISOChronology in default zone
Throws:
IllegalArgumentException if the instant is invalid
 
     public MutableDateTime(Object instantChronology chronology) {
         super(instant, DateTimeUtils.getChronology(chronology));
     }
 
     //-----------------------------------------------------------------------
     
Constructs an instance from datetime field values using ISOChronology in the default time zone.

Parameters:
year the year
monthOfYear the month of the year
dayOfMonth the day of the month
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 MutableDateTime(
             int year,
             int monthOfYear,
             int dayOfMonth,
             int hourOfDay,
             int minuteOfHour,
             int secondOfMinute,
             int millisOfSecond) {
         super(yearmonthOfYeardayOfMonthhourOfDayminuteOfHoursecondOfMinutemillisOfSecond);
     }

    
Constructs an instance from datetime field values using ISOChronology in the specified time zone.

If the specified time zone is null, the default zone is used.

Parameters:
year the year
monthOfYear the month of the year
dayOfMonth the day of the month
hourOfDay the hour of the day
minuteOfHour the minute of the hour
secondOfMinute the second of the minute
millisOfSecond the millisecond of the second
zone the time zone, null means default time zone
 
     public MutableDateTime(
             int year,
             int monthOfYear,
             int dayOfMonth,
             int hourOfDay,
             int minuteOfHour,
             int secondOfMinute,
             int millisOfSecond,
             DateTimeZone zone) {
         super(yearmonthOfYeardayOfMonth,
               hourOfDayminuteOfHoursecondOfMinutemillisOfSecondzone);
     }

    
Constructs an instance from datetime field values using the specified chronology.

If the chronology is null, ISOChronology in the default time zone is used.

Parameters:
year the year
monthOfYear the month of the year
dayOfMonth the day of the month
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 default zone
 
     public MutableDateTime(
             int year,
             int monthOfYear,
             int dayOfMonth,
             int hourOfDay,
             int minuteOfHour,
             int secondOfMinute,
             int millisOfSecond,
             Chronology chronology) {
         super(yearmonthOfYeardayOfMonth,
               hourOfDayminuteOfHoursecondOfMinutemillisOfSecondchronology);
     }
 
     //-----------------------------------------------------------------------
     
Gets the field used for rounding this instant, returning null if rounding is not enabled.

Returns:
the rounding field
 
     public DateTimeField getRoundingField() {
         return ;
     }

    
Gets the rounding mode for this instant, returning ROUND_NONE if rounding is not enabled.

Returns:
the rounding mode constant
 
     public int getRoundingMode() {
         return ;
     }

    
Sets the status of rounding to use the specified field and ROUND_FLOOR mode. A null field will disable rounding. Once set, the instant is then rounded using the new field and mode.

Enabling rounding will cause all subsequent calls to setMillis(long) to be rounded. This can be used to control the precision of the instant, for example by setting a rounding field of minuteOfDay, the seconds and milliseconds will always be zero.

Parameters:
field rounding field or null to disable
 
     public void setRounding(DateTimeField field) {
         setRounding(field.);
     }

    
Sets the status of rounding to use the specified field and mode. A null field or mode of ROUND_NONE will disable rounding. Once set, the instant is then rounded using the new field and mode.

Enabling rounding will cause all subsequent calls to setMillis(long) to be rounded. This can be used to control the precision of the instant, for example by setting a rounding field of minuteOfDay, the seconds and milliseconds will always be zero.

Parameters:
field rounding field or null to disable
mode rounding mode or ROUND_NONE to disable
Throws:
IllegalArgumentException if mode is unknown, no exception if field is null
 
     public void setRounding(DateTimeField fieldint mode) {
         if (field != null && (mode <  || mode > )) {
             throw new IllegalArgumentException("Illegal rounding mode: " + mode);
         }
          = (mode ==  ? null : field);
          = (field == null ?  : mode);
         setMillis(getMillis());
     }
 
     //-----------------------------------------------------------------------
     
Set the milliseconds of the datetime.

All changes to the millisecond field occurs via this method.

Parameters:
instant the milliseconds since 1970-01-01T00:00:00Z to set the datetime to
 
     public void setMillis(long instant) {
         switch () {
             case :
                 break;
             case :
                 instant = .roundFloor(instant);
                 break;
             case :
                 instant = .roundCeiling(instant);
                 break;
             case :
                 instant = .roundHalfFloor(instant);
                 break;
             case :
                 instant = .roundHalfCeiling(instant);
                 break;
             case :
                 instant = .roundHalfEven(instant);
                 break;
         }
 
         super.setMillis(instant);
     }

    
Sets the millisecond instant of this instant from another.

This method does not change the chronology of this instant, just the millisecond instant.

Parameters:
instant the instant to use, null means now
 
     public void setMillis(ReadableInstant instant) {
         long instantMillis = DateTimeUtils.getInstantMillis(instant);
         setMillis(instantMillis);  // set via this class not super
     }
 
     //-----------------------------------------------------------------------
     
Add an amount of time to the datetime.

Parameters:
duration the millis to add
Throws:
ArithmeticException if the result exceeds the capacity of the instant
 
     public void add(long duration) {
         setMillis(FieldUtils.safeAdd(getMillis(), duration));  // set via this class not super
     }

    
Adds a duration to this instant.

This will typically change the value of most fields.

Parameters:
duration the duration to add, null means add zero
Throws:
ArithmeticException if the result exceeds the capacity of the instant
 
     public void add(ReadableDuration duration) {
         add(duration, 1);
     }

    
Adds a duration to this instant specifying how many times to add.

This will typically change the value of most fields.

Parameters:
duration the duration to add, null means add zero
scalar direction and amount to add, which may be negative
Throws:
ArithmeticException if the result exceeds the capacity of the instant
 
     public void add(ReadableDuration durationint scalar) {
         if (duration != null) {
             add(FieldUtils.safeMultiply(duration.getMillis(), scalar));
         }
     }

    
Adds a period to this instant.

This will typically change the value of most fields.

Parameters:
period the period to add, null means add zero
Throws:
ArithmeticException if the result exceeds the capacity of the instant
 
     public void add(ReadablePeriod period) {
         add(period, 1);
     }

    
Adds a period to this instant specifying how many times to add.

This will typically change the value of most fields.

Parameters:
period the period to add, null means add zero
scalar direction and amount to add, which may be negative
Throws:
ArithmeticException if the result exceeds the capacity of the instant
 
     public void add(ReadablePeriod periodint scalar) {
         if (period != null) {
             setMillis(getChronology().add(periodgetMillis(), scalar));  // set via this class not super
         }
     }
 
     //-----------------------------------------------------------------------
     
Set the chronology of the datetime.

All changes to the chronology occur via this method.

Parameters:
chronology the chronology to use, null means ISOChronology in default zone
 
     public void setChronology(Chronology chronology) {
         super.setChronology(chronology);
     }
 
     //-----------------------------------------------------------------------
     
Sets the time zone of the datetime, changing the chronology and field values.

Changing the zone using this method retains the millisecond instant. The millisecond instant is adjusted in the new zone to compensate. chronology. Setting the time zone does not affect the millisecond value of this instant.

If the chronology already has this time zone, no change occurs.

Parameters:
newZone the time zone to use, null means default zone
See also:
setZoneRetainFields
 
     public void setZone(DateTimeZone newZone) {
         newZone = DateTimeUtils.getZone(newZone);
         Chronology chrono = getChronology();
         if (chrono.getZone() != newZone) {
             setChronology(chrono.withZone(newZone));  // set via this class not super
         }
     }

    
Sets the time zone of the datetime, changing the chronology and millisecond.

Changing the zone using this method retains the field values. The millisecond instant is adjusted in the new zone to compensate.

If the chronology already has this time zone, no change occurs.

Parameters:
newZone the time zone to use, null means default zone
See also:
setZone
 
     public void setZoneRetainFields(DateTimeZone newZone) {
         newZone = DateTimeUtils.getZone(newZone);
         DateTimeZone originalZone = DateTimeUtils.getZone(getZone());
         if (newZone == originalZone) {
             return;
         }
         
         long millis = originalZone.getMillisKeepLocal(newZonegetMillis());
         setChronology(getChronology().withZone(newZone));  // set via this class not super
         setMillis(millis);
     }
 
     //-----------------------------------------------------------------------
     
Sets the value of one of the fields of the instant, such as hourOfDay.

Parameters:
type a field type, usually obtained from DateTimeFieldType, not null
value the value to set the field to
Throws:
IllegalArgumentException if the value is null or invalid
 
     public void set(DateTimeFieldType typeint value) {
         if (type == null) {
             throw new IllegalArgumentException("Field must not be null");
         }
         setMillis(type.getField(getChronology()).set(getMillis(), value));
     }

    
Adds to the instant specifying the duration and multiple to add.

Parameters:
type a field type, usually obtained from DateTimeFieldType, not null
amount the amount to add of this duration
Throws:
IllegalArgumentException if the value is null or invalid
ArithmeticException if the result exceeds the capacity of the instant
 
     public void add(DurationFieldType typeint amount) {
         if (type == null) {
             throw new IllegalArgumentException("Field must not be null");
         }
         setMillis(type.getField(getChronology()).add(getMillis(), amount));
     }
 
     //-----------------------------------------------------------------------
     
Set the year to the specified value.

Parameters:
year the year
Throws:
IllegalArgumentException if the value is invalid
 
     public void setYear(final int year) {
         setMillis(getChronology().year().set(getMillis(), year));
     }

    
Add a number of years to the date.

Parameters:
years the years to add
Throws:
IllegalArgumentException if the value is invalid
 
     public void addYears(final int years) {
         setMillis(getChronology().years().add(getMillis(), years));
     }
 
     //-----------------------------------------------------------------------
     
Set the weekyear to the specified value.

Parameters:
weekyear the weekyear
Throws:
IllegalArgumentException if the value is invalid
 
     public void setWeekyear(final int weekyear) {
         setMillis(getChronology().weekyear().set(getMillis(), weekyear));
     }

    
Add a number of weekyears to the date.

Parameters:
weekyears the weekyears to add
Throws:
IllegalArgumentException if the value is invalid
 
     public void addWeekyears(final int weekyears) {
         setMillis(getChronology().weekyears().add(getMillis(), weekyears));
     }
 
     //-----------------------------------------------------------------------
     
Set the month of the year to the specified value.

Parameters:
monthOfYear the month of the year
Throws:
IllegalArgumentException if the value is invalid
 
     public void setMonthOfYear(final int monthOfYear) {
         setMillis(getChronology().monthOfYear().set(getMillis(), monthOfYear));
     }

    
Add a number of months to the date.

Parameters:
months the months to add
Throws:
IllegalArgumentException if the value is invalid
 
     public void addMonths(final int months) {
         setMillis(getChronology().months().add(getMillis(), months));
     }
 
     //-----------------------------------------------------------------------
     
Set the week of weekyear to the specified value.

Parameters:
weekOfWeekyear the week of the weekyear
Throws:
IllegalArgumentException if the value is invalid
 
     public void setWeekOfWeekyear(final int weekOfWeekyear) {
         setMillis(getChronology().weekOfWeekyear().set(getMillis(), weekOfWeekyear));
     }

    
Add a number of weeks to the date.

Parameters:
weeks the weeks to add
Throws:
IllegalArgumentException if the value is invalid
 
     public void addWeeks(final int weeks) {
         setMillis(getChronology().weeks().add(getMillis(), weeks));
     }
 
     //-----------------------------------------------------------------------
     
Set the day of year to the specified value.

Parameters:
dayOfYear the day of the year
Throws:
IllegalArgumentException if the value is invalid
 
     public void setDayOfYear(final int dayOfYear) {
         setMillis(getChronology().dayOfYear().set(getMillis(), dayOfYear));
     }

    
Set the day of the month to the specified value.

Parameters:
dayOfMonth the day of the month
Throws:
IllegalArgumentException if the value is invalid
 
     public void setDayOfMonth(final int dayOfMonth) {
         setMillis(getChronology().dayOfMonth().set(getMillis(), dayOfMonth));
     }

    
Set the day of week to the specified value.

Parameters:
dayOfWeek the day of the week
Throws:
IllegalArgumentException if the value is invalid
 
     public void setDayOfWeek(final int dayOfWeek) {
         setMillis(getChronology().dayOfWeek().set(getMillis(), dayOfWeek));
     }

    
Add a number of days to the date.

Parameters:
days the days to add
Throws:
IllegalArgumentException if the value is invalid
 
     public void addDays(final int days) {
         setMillis(getChronology().days().add(getMillis(), days));
     }
 
     //-----------------------------------------------------------------------
     
Set the hour of the day to the specified value.

Parameters:
hourOfDay the hour of day
Throws:
IllegalArgumentException if the value is invalid
 
     public void setHourOfDay(final int hourOfDay) {
         setMillis(getChronology().hourOfDay().set(getMillis(), hourOfDay));
     }

    
Add a number of hours to the date.

Parameters:
hours the hours to add
Throws:
IllegalArgumentException if the value is invalid
 
     public void addHours(final int hours) {
         setMillis(getChronology().hours().add(getMillis(), hours));
     }
     
     //-----------------------------------------------------------------------
     
Set the minute of the day to the specified value.

Parameters:
minuteOfDay the minute of day
Throws:
IllegalArgumentException if the value is invalid
 
     public void setMinuteOfDay(final int minuteOfDay) {
         setMillis(getChronology().minuteOfDay().set(getMillis(), minuteOfDay));
     }

    
Set the minute of the hour to the specified value.

Parameters:
minuteOfHour the minute of hour
Throws:
IllegalArgumentException if the value is invalid
 
     public void setMinuteOfHour(final int minuteOfHour) {
         setMillis(getChronology().minuteOfHour().set(getMillis(), minuteOfHour));
     }

    
Add a number of minutes to the date.

Parameters:
minutes the minutes to add
Throws:
IllegalArgumentException if the value is invalid
 
     public void addMinutes(final int minutes) {
         setMillis(getChronology().minutes().add(getMillis(), minutes));
     }
 
     //-----------------------------------------------------------------------
     
Set the second of the day to the specified value.

Parameters:
secondOfDay the second of day
Throws:
IllegalArgumentException if the value is invalid
 
     public void setSecondOfDay(final int secondOfDay) {
         setMillis(getChronology().secondOfDay().set(getMillis(), secondOfDay));
     }

    
Set the second of the minute to the specified value.

Parameters:
secondOfMinute the second of minute
Throws:
IllegalArgumentException if the value is invalid
 
     public void setSecondOfMinute(final int secondOfMinute) {
         setMillis(getChronology().secondOfMinute().set(getMillis(), secondOfMinute));
     }

    
Add a number of seconds to the date.

Parameters:
seconds the seconds to add
Throws:
IllegalArgumentException if the value is invalid
 
     public void addSeconds(final int seconds) {
         setMillis(getChronology().seconds().add(getMillis(), seconds));
     }
 
     //-----------------------------------------------------------------------
     
Set the millis of the day to the specified value.

Parameters:
millisOfDay the millis of day
Throws:
IllegalArgumentException if the value is invalid
 
     public void setMillisOfDay(final int millisOfDay) {
         setMillis(getChronology().millisOfDay().set(getMillis(), millisOfDay));
     }

    
Set the millis of the second to the specified value.

Parameters:
millisOfSecond the millis of second
Throws:
IllegalArgumentException if the value is invalid
 
     public void setMillisOfSecond(final int millisOfSecond) {
         setMillis(getChronology().millisOfSecond().set(getMillis(), millisOfSecond));
     }

    
Add a number of milliseconds to the date. The implementation of this method differs from the add(long) method in that a DateTimeField performs the addition.

Parameters:
millis the milliseconds to add
Throws:
IllegalArgumentException if the value is invalid
 
     public void addMillis(final int millis) {
         setMillis(getChronology().millis().add(getMillis(), millis));
     }
 
     //-----------------------------------------------------------------------
     
Set the date from milliseconds. The time part of this object will be unaffected.

Parameters:
instant an instant to copy the date from, time part ignored
Throws:
IllegalArgumentException if the value is invalid
 
     public void setDate(final long instant) {
         setMillis(getChronology().millisOfDay().set(instantgetMillisOfDay()));
     }

    
Set the date from another instant. The time part of this object will be unaffected.

Parameters:
instant an instant to copy the date from, time part ignored
Throws:
IllegalArgumentException if the object is invalid
 
     public void setDate(final ReadableInstant instant) {
         long instantMillis = DateTimeUtils.getInstantMillis(instant);
         Chronology instantChrono = DateTimeUtils.getInstantChronology(instant);
         DateTimeZone zone = instantChrono.getZone();
         if (zone != null) {
             instantMillis = zone.getMillisKeepLocal(.instantMillis);
         }
         setDate(instantMillis);
     }

    
Set the date from fields. The time part of this object will be unaffected.

Parameters:
year the year
monthOfYear the month of the year
dayOfMonth the day of the month
Throws:
IllegalArgumentException if the value is invalid
 
     public void setDate(
             final int year,
             final int monthOfYear,
             final int dayOfMonth) {
         Chronology c = getChronology();
         long instantMidnight = c.getDateTimeMillis(yearmonthOfYeardayOfMonth, 0);
         setDate(instantMidnight);
     }
 
     //-----------------------------------------------------------------------
     
Set the time from milliseconds. The date part of this object will be unaffected.

Parameters:
millis an instant to copy the time from, date part ignored
Throws:
IllegalArgumentException if the value is invalid
 
     public void setTime(final long millis) {
         int millisOfDay = ISOChronology.getInstanceUTC().millisOfDay().get(millis);
         setMillis(getChronology().millisOfDay().set(getMillis(), millisOfDay));
     }

    
Set the time from another instant. The date part of this object will be unaffected.

Parameters:
instant an instant to copy the time from, date part ignored
Throws:
IllegalArgumentException if the object is invalid
 
     public void setTime(final ReadableInstant instant) {
         long instantMillis = DateTimeUtils.getInstantMillis(instant);
         Chronology instantChrono = DateTimeUtils.getInstantChronology(instant);
         DateTimeZone zone = instantChrono.getZone();
         if (zone != null) {
             instantMillis = zone.getMillisKeepLocal(.instantMillis);
         }
         setTime(instantMillis);
     }

    
Set the time from fields. The date part of this object will be unaffected.

Parameters:
hour the hour
minuteOfHour the minute of the hour
secondOfMinute the second of the minute
millisOfSecond the millisecond of the second
Throws:
IllegalArgumentException if the value is invalid
 
     public void setTime(
             final int hour,
             final int minuteOfHour,
             final int secondOfMinute,
             final int millisOfSecond) {
         long instant = getChronology().getDateTimeMillis(
             getMillis(), hourminuteOfHoursecondOfMinutemillisOfSecond);
         setMillis(instant);
     }

    
Set the date and time from fields.

Parameters:
year the year
monthOfYear the month of the year
dayOfMonth the day of the month
hourOfDay the hour of the day
minuteOfHour the minute of the hour
secondOfMinute the second of the minute
millisOfSecond the millisecond of the second
Throws:
IllegalArgumentException if the value is invalid
 
     public void setDateTime(
             final int year,
             final int monthOfYear,
             final int dayOfMonth,
             final int hourOfDay,
             final int minuteOfHour,
             final int secondOfMinute,
            final int millisOfSecond) {
        long instant = getChronology().getDateTimeMillis(
            yearmonthOfYeardayOfMonthhourOfDayminuteOfHoursecondOfMinutemillisOfSecond);
        setMillis(instant);
    }
    //-----------------------------------------------------------------------
    
Gets the property object for the specified type, which contains many useful methods.

Parameters:
type the field type to get the chronology for
Returns:
the property object
Throws:
IllegalArgumentException if the field is null or unsupported
Since:
1.2
    public Property property(DateTimeFieldType type) {
        if (type == null) {
            throw new IllegalArgumentException("The DateTimeFieldType must not be null");
        }
        DateTimeField field = type.getField(getChronology());
        if (field.isSupported() == false) {
            throw new IllegalArgumentException("Field '" + type + "' is not supported");
        }
        return new Property(thisfield);
    }

    
Get the era property.

Returns:
the era property
    public Property era() {
        return new Property(thisgetChronology().era());
    }

    
Get the century of era property.

Returns:
the year of era property
    public Property centuryOfEra() {
        return new Property(thisgetChronology().centuryOfEra());
    }

    
Get the year of century property.

Returns:
the year of era property
    public Property yearOfCentury() {
        return new Property(thisgetChronology().yearOfCentury());
    }

    
Get the year of era property.

Returns:
the year of era property
    public Property yearOfEra() {
        return new Property(thisgetChronology().yearOfEra());
    }

    
Get the year property.

Returns:
the year property
    public Property year() {
        return new Property(thisgetChronology().year());
    }

    
Get the year of a week based year property.

Returns:
the year of a week based year property
    public Property weekyear() {
        return new Property(thisgetChronology().weekyear());
    }

    
Get the month of year property.

Returns:
the month of year property
    public Property monthOfYear() {
        return new Property(thisgetChronology().monthOfYear());
    }

    
Get the week of a week based year property.

Returns:
the week of a week based year property
    public Property weekOfWeekyear() {
        return new Property(thisgetChronology().weekOfWeekyear());
    }

    
Get the day of year property.

Returns:
the day of year property
    public Property dayOfYear() {
        return new Property(thisgetChronology().dayOfYear());
    }

    
Get the day of month property.

The values for day of month are defined in DateTimeConstants.

Returns:
the day of month property
    public Property dayOfMonth() {
        return new Property(thisgetChronology().dayOfMonth());
    }

    
Get the day of week property.

The values for day of week are defined in DateTimeConstants.

Returns:
the day of week property
    public Property dayOfWeek() {
        return new Property(thisgetChronology().dayOfWeek());
    }
    //-----------------------------------------------------------------------
    
Get the hour of day field property

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

    
Get the minute of day property

Returns:
the minute of day property
    public Property minuteOfDay() {
        return new Property(thisgetChronology().minuteOfDay());
    }

    
Get the minute of hour field property

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

    
Get the second of day property

Returns:
the second of day property
    public Property secondOfDay() {
        return new Property(thisgetChronology().secondOfDay());
    }

    
Get the second of minute field property

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

    
Get the millis of day property

Returns:
the millis of day property
    public Property millisOfDay() {
        return new Property(thisgetChronology().millisOfDay());
    }

    
Get the millis of second property

Returns:
the millis of second property
    public Property millisOfSecond() {
        return new Property(thisgetChronology().millisOfSecond());
    }
    //-----------------------------------------------------------------------
    
Clone this object without having to cast the returned object.

Returns:
a clone of the this object.
    public MutableDateTime copy() {
        return (MutableDateTimeclone();
    }
    //-----------------------------------------------------------------------
    
Clone this object.

Returns:
a clone of this object.
    public Object clone() {
        try {
            return super.clone();
        } catch (CloneNotSupportedException ex) {
            throw new InternalError("Clone error");
        }
    }

    
Output the date time in ISO8601 format (yyyy-MM-ddTHH:mm:ss.SSSZZ).

Returns:
ISO8601 time formatted string.
    @ToString
    public String toString() {
        return ISODateTimeFormat.dateTime().print(this);
    }

    
MutableDateTime.Property binds a MutableDateTime to a DateTimeField allowing powerful datetime functionality to be easily accessed.

The example below shows how to use the property to change the value of a MutableDateTime object.

 MutableDateTime dt = new MutableDateTime(1972, 12, 3, 13, 32, 19, 123);
 dt.year().add(20);
 dt.second().roundFloor().minute().set(10);
 

MutableDateTime.Propery itself is thread-safe and immutable, but the MutableDateTime being operated on is not.

Author(s):
Stephen Colebourne
Brian S O'Neill
Since:
1.0
    public static final class Property extends AbstractReadableInstantFieldProperty {
        
        
Serialization version
        private static final long serialVersionUID = -4481126543819298617L;
        
        
The instant this property is working against
        private MutableDateTime iInstant;
        
The field this property is working against
        private DateTimeField iField;
        
        
Constructor.

Parameters:
instant the instant to set
field the field to use
        Property(MutableDateTime instantDateTimeField field) {
            super();
             = instant;
             = field;
        }
        
        
Writes the property in a safe serialization format.
        private void writeObject(ObjectOutputStream oosthrows IOException {
            oos.writeObject();
            oos.writeObject(.getType());
        }

        
Reads the property from a safe serialization format.
        private void readObject(ObjectInputStream oosthrows IOExceptionClassNotFoundException {
             = (MutableDateTimeoos.readObject();
            DateTimeFieldType type = (DateTimeFieldTypeoos.readObject();
             = type.getField(.getChronology());
        }
        //-----------------------------------------------------------------------
        
Gets the field being used.

Returns:
the field
        public DateTimeField getField() {
            return ;
        }
        
        
Gets the milliseconds of the datetime that this property is linked to.

Returns:
the milliseconds
        protected long getMillis() {
            return .getMillis();
        }
        
        
Gets the chronology of the datetime that this property is linked to.

Returns:
the chronology
Since:
1.4
        protected Chronology getChronology() {
            return .getChronology();
        }
        
        
Gets the mutable datetime being used.

Returns:
the mutable datetime
        public MutableDateTime getMutableDateTime() {
            return ;
        }
        
        //-----------------------------------------------------------------------
        
Adds a value to the millis value.

Parameters:
value the value to add
Returns:
the mutable datetime being used, so calls can be chained
See also:
DateTimeField.add(long,int)
        public MutableDateTime add(int value) {
            .setMillis(getField().add(.getMillis(), value));
            return ;
        }
        
        
Adds a value to the millis value.

Parameters:
value the value to add
Returns:
the mutable datetime being used, so calls can be chained
See also:
DateTimeField.add(long,long)
        public MutableDateTime add(long value) {
            .setMillis(getField().add(.getMillis(), value));
            return ;
        }
        
        
Adds a value, possibly wrapped, to the millis value.

Parameters:
value the value to add
Returns:
the mutable datetime being used, so calls can be chained
See also:
DateTimeField.addWrapField
        public MutableDateTime addWrapField(int value) {
            .setMillis(getField().addWrapField(.getMillis(), value));
            return ;
        }
        
        //-----------------------------------------------------------------------
        
Sets a value.

Parameters:
value the value to set.
Returns:
the mutable datetime being used, so calls can be chained
See also:
DateTimeField.set(long,int)
        public MutableDateTime set(int value) {
            .setMillis(getField().set(.getMillis(), value));
            return ;
        }
        
        
Sets a text value.

Parameters:
text the text value to set
locale optional locale to use for selecting a text symbol
Returns:
the mutable datetime being used, so calls can be chained
Throws:
IllegalArgumentException if the text value isn't valid
See also:
DateTimeField.set(long,java.lang.String,java.util.Locale)
        public MutableDateTime set(String textLocale locale) {
            .setMillis(getField().set(.getMillis(), textlocale));
            return ;
        }
        
        
Sets a text value.

Parameters:
text the text value to set
Returns:
the mutable datetime being used, so calls can be chained
Throws:
IllegalArgumentException if the text value isn't valid
See also:
DateTimeField.set(long,java.lang.String)
        public MutableDateTime set(String text) {
            set(textnull);
            return ;
        }
        
        //-----------------------------------------------------------------------
        
Round to the lowest whole unit of this field.

Returns:
the mutable datetime being used, so calls can be chained
See also:
DateTimeField.roundFloor
        public MutableDateTime roundFloor() {
            return ;
        }

        
Round to the highest whole unit of this field.

Returns:
the mutable datetime being used, so calls can be chained
See also:
DateTimeField.roundCeiling
        public MutableDateTime roundCeiling() {
            return ;
        }
        
        
Round to the nearest whole unit of this field, favoring the floor if halfway.

Returns:
the mutable datetime being used, so calls can be chained
See also:
DateTimeField.roundHalfFloor
        public MutableDateTime roundHalfFloor() {
            return ;
        }
        
        
Round to the nearest whole unit of this field, favoring the ceiling if halfway.

Returns:
the mutable datetime being used, so calls can be chained
See also:
DateTimeField.roundHalfCeiling
        public MutableDateTime roundHalfCeiling() {
            return ;
        }

        
Round to the nearest whole unit of this field. If halfway, the ceiling is favored over the floor only if it makes this field's value even.

Returns:
the mutable datetime being used, so calls can be chained
See also:
DateTimeField.roundHalfEven
        public MutableDateTime roundHalfEven() {
            return ;
        }
    }
New to GrepCode? Check out our FAQ X