Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

org.joda.time
Class DateMidnight  view DateMidnight download DateMidnight.java

java.lang.Object
  extended byorg.joda.time.base.AbstractInstant
      extended byorg.joda.time.base.AbstractDateTime
          extended byorg.joda.time.base.BaseDateTime
              extended byorg.joda.time.DateMidnight
All Implemented Interfaces:
java.lang.Comparable, ReadableDateTime, ReadableInstant, java.io.Serializable

public final class DateMidnight
extends org.joda.time.base.BaseDateTime
implements ReadableDateTime, java.io.Serializable

DateMidnight defines a date where the time component is fixed at midnight. The class uses a time zone, thus midnight is local unless a UTC time zone is used.

It is important to emphasise that this class represents the time of midnight on any given day. Note that midnight is defined as 00:00, which is at the very start of a day.

This class does not represent a day, but the millisecond instant at midnight. If you need a class that represents the whole day, then an Interval or a YearMonthDay may be more suitable.

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 compatable with the modern Gregorian calendar.

Each individual field can be queried in two ways:

The second technique also provides access to other useful methods on the field:

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

Since:
1.0

Nested Class Summary
static class DateMidnight.Property
          DateMidnight.Property binds a DateMidnight to a DateTimeField allowing powerful datetime functionality to be easily accessed.
 
Field Summary
private static long serialVersionUID
          Serialization lock
 
Fields inherited from class org.joda.time.base.BaseDateTime
 
Constructor Summary
DateMidnight()
          Constructs an instance set to the current system millisecond time using ISOChronology in the default time zone.
DateMidnight(Chronology chronology)
          Constructs an instance set to the current system millisecond time using the specified chronology.
DateMidnight(DateTimeZone zone)
          Constructs an instance set to the current system millisecond time using ISOChronology in the specified time zone.
DateMidnight(int year, int monthOfYear, int dayOfMonth)
          Constructs an instance from datetime field values using ISOChronology in the default time zone.
DateMidnight(int year, int monthOfYear, int dayOfMonth, Chronology chronology)
          Constructs an instance from datetime field values using the specified chronology.
DateMidnight(int year, int monthOfYear, int dayOfMonth, DateTimeZone zone)
          Constructs an instance from datetime field values using ISOChronology in the specified time zone.
DateMidnight(long instant)
          Constructs an instance set to the milliseconds from 1970-01-01T00:00:00Z using ISOChronology in the default time zone.
DateMidnight(long instant, Chronology chronology)
          Constructs an instance set to the milliseconds from 1970-01-01T00:00:00Z using the specified chronology.
DateMidnight(long instant, DateTimeZone zone)
          Constructs an instance set to the milliseconds from 1970-01-01T00:00:00Z using ISOChronology in the specified time zone.
DateMidnight(java.lang.Object instant)
          Constructs an instance from an Object that represents a datetime.
DateMidnight(java.lang.Object instant, Chronology chronology)
          Constructs an instance from an Object that represents a datetime, using the specified chronology.
DateMidnight(java.lang.Object instant, DateTimeZone zone)
          Constructs an instance from an Object that represents a datetime, forcing the time zone to that specified.
 
Method Summary
 DateMidnight.Property centuryOfEra()
          Get the century of era property.
protected  long checkInstant(long instant, Chronology chronology)
          Rounds the specified instant to midnight.
 DateMidnight.Property dayOfMonth()
          Get the day of month property.
 DateMidnight.Property dayOfWeek()
          Get the day of week property.
 DateMidnight.Property dayOfYear()
          Get the day of year property.
 DateMidnight.Property era()
          Get the era property.
 DateMidnight minus(long duration)
          Gets a copy of this datetime with the specified duration take away.
 DateMidnight minus(ReadableDuration duration)
          Gets a copy of this datetime with the specified duration take away.
 DateMidnight minus(ReadablePeriod period)
          Gets a copy of this datetime with the specified period take away.
 DateMidnight minusDays(int days)
          Returns a new datetime minus the specified number of days.
 DateMidnight minusMonths(int months)
          Returns a new datetime minus the specified number of months.
 DateMidnight minusWeeks(int weeks)
          Returns a new datetime minus the specified number of weeks.
 DateMidnight minusYears(int years)
          Returns a new datetime minus the specified number of years.
 DateMidnight.Property monthOfYear()
          Get the month of year property.
 DateMidnight plus(long duration)
          Gets a copy of this datetime with the specified duration added.
 DateMidnight plus(ReadableDuration duration)
          Gets a copy of this datetime with the specified duration added.
 DateMidnight plus(ReadablePeriod period)
          Gets a copy of this datetime with the specified period added.
 DateMidnight plusDays(int days)
          Returns a new datetime plus the specified number of days.
 DateMidnight plusMonths(int months)
          Returns a new datetime plus the specified number of months.
 DateMidnight plusWeeks(int weeks)
          Returns a new datetime plus the specified number of weeks.
 DateMidnight plusYears(int years)
          Returns a new datetime plus the specified number of years.
 DateMidnight.Property property(DateTimeFieldType type)
          Gets the property object for the specified type, which contains many useful methods.
 Interval toInterval()
          Converts this object to an Interval encompassing the whole of this day.
 YearMonthDay toYearMonthDay()
          Converts this object to a YearMonthDay using the same millis and chronology.
 DateMidnight.Property weekOfWeekyear()
          Get the week of a week based year property.
 DateMidnight.Property weekyear()
          Get the year of a week based year property.
 DateMidnight withChronology(Chronology newChronology)
          Gets a copy of this instant with a different chronology, potentially changing the day in unexpected ways.
 DateMidnight withDurationAdded(long durationToAdd, int scalar)
          Gets a copy of this datetime with the specified duration added.
 DateMidnight withDurationAdded(ReadableDuration durationToAdd, int scalar)
          Gets a copy of this datetime with the specified duration added.
 DateMidnight withField(DateTimeFieldType fieldType, int value)
          Gets a copy of this datetime with the specified field set to a new value.
 DateMidnight withFieldAdded(DurationFieldType fieldType, int amount)
          Gets a copy of this datetime with the value of the specified field increased.
 DateMidnight withFields(ReadablePartial partial)
          Gets a copy of this datetime with the partial set of fields replacing those from this instance.
 DateMidnight withMillis(long newMillis)
          Gets a copy of this instant with different millis.
 DateMidnight withPeriodAdded(ReadablePeriod period, int scalar)
          Gets a copy of this datetime with the specified period added.
 DateMidnight withZoneRetainFields(DateTimeZone newZone)
          Gets a copy of this instant with a different time zone, preserving the day The returned object will have a local time of midnight in the new zone on the same day as the original instant.
 DateMidnight.Property year()
          Get the year property.
 DateMidnight.Property yearOfCentury()
          Get the year of century property.
 DateMidnight.Property yearOfEra()
          Get the year of era property.
 
Methods inherited from class org.joda.time.base.BaseDateTime
checkChronology, getChronology, getMillis, setChronology, setMillis
 
Methods inherited from class org.joda.time.base.AbstractDateTime
get, getCenturyOfEra, getDayOfMonth, getDayOfWeek, getDayOfYear, getEra, getHourOfDay, getMillisOfDay, getMillisOfSecond, getMinuteOfDay, getMinuteOfHour, getMonthOfYear, getSecondOfDay, getSecondOfMinute, getWeekOfWeekyear, getWeekyear, getYear, getYearOfCentury, getYearOfEra, toCalendar, toGregorianCalendar, toString, toString
 
Methods inherited from class org.joda.time.base.AbstractInstant
compareTo, equals, get, getZone, hashCode, isAfter, isAfter, isAfterNow, isBefore, isBefore, isBeforeNow, isEqual, isEqual, isEqualNow, isSupported, toDate, toDateTime, toDateTime, toDateTime, toDateTimeISO, toInstant, toMutableDateTime, toMutableDateTime, toMutableDateTime, toMutableDateTimeISO, toString, toString
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface org.joda.time.ReadableDateTime
getCenturyOfEra, getDayOfMonth, getDayOfWeek, getDayOfYear, getEra, getHourOfDay, getMillisOfDay, getMillisOfSecond, getMinuteOfDay, getMinuteOfHour, getMonthOfYear, getSecondOfDay, getSecondOfMinute, getWeekOfWeekyear, getWeekyear, getYear, getYearOfCentury, getYearOfEra, toDateTime, toMutableDateTime, toString, toString
 
Methods inherited from interface org.joda.time.ReadableInstant
compareTo, equals, get, getChronology, getMillis, getZone, hashCode, isAfter, isBefore, isEqual, isSupported, toInstant, toString
 

Field Detail

serialVersionUID

private static final long serialVersionUID
Serialization lock

See Also:
Constant Field Values
Constructor Detail

DateMidnight

public DateMidnight()
Constructs an instance set to the current system millisecond time using ISOChronology in the default time zone. The constructed object will have a local time of midnight.


DateMidnight

public DateMidnight(DateTimeZone zone)
Constructs an instance set to the current system millisecond time using ISOChronology in the specified time zone. The constructed object will have a local time of midnight.

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


DateMidnight

public DateMidnight(Chronology chronology)
Constructs an instance set to the current system millisecond time using the specified chronology. The constructed object will have a local time of midnight.

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


DateMidnight

public DateMidnight(long instant)
Constructs an instance set to the milliseconds from 1970-01-01T00:00:00Z using ISOChronology in the default time zone. The constructed object will have a local time of midnight.


DateMidnight

public DateMidnight(long instant,
                    DateTimeZone zone)
Constructs an instance set to the milliseconds from 1970-01-01T00:00:00Z using ISOChronology in the specified time zone. The constructed object will have a local time of midnight.

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


DateMidnight

public DateMidnight(long instant,
                    Chronology chronology)
Constructs an instance set to the milliseconds from 1970-01-01T00:00:00Z using the specified chronology. The constructed object will have a local time of midnight.

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


DateMidnight

public DateMidnight(java.lang.Object instant)
Constructs an instance from an Object that represents a datetime. The constructed object will have a local time of midnight.

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.


DateMidnight

public DateMidnight(java.lang.Object instant,
                    DateTimeZone zone)
Constructs an instance from an Object that represents a datetime, forcing the time zone to that specified. The constructed object will have a local time of midnight.

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.


DateMidnight

public DateMidnight(java.lang.Object instant,
                    Chronology chronology)
Constructs an instance from an Object that represents a datetime, using the specified chronology. The constructed object will have a local time of midnight.

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.


DateMidnight

public DateMidnight(int year,
                    int monthOfYear,
                    int dayOfMonth)
Constructs an instance from datetime field values using ISOChronology in the default time zone. The constructed object will have a local time of midnight.


DateMidnight

public DateMidnight(int year,
                    int monthOfYear,
                    int dayOfMonth,
                    DateTimeZone zone)
Constructs an instance from datetime field values using ISOChronology in the specified time zone. The constructed object will have a local time of midnight.

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


DateMidnight

public DateMidnight(int year,
                    int monthOfYear,
                    int dayOfMonth,
                    Chronology chronology)
Constructs an instance from datetime field values using the specified chronology. The constructed object will have a local time of midnight.

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

Method Detail

checkInstant

protected long checkInstant(long instant,
                            Chronology chronology)
Rounds the specified instant to midnight.


withMillis

public DateMidnight withMillis(long newMillis)
Gets a copy of this instant with different millis. The returned object will have a local time of midnight.

Only the millis will change, the chronology and time zone are kept. The returned object will be either be a new instance or this.


withChronology

public DateMidnight withChronology(Chronology newChronology)
Gets a copy of this instant with a different chronology, potentially changing the day in unexpected ways.

This method creates a new DateMidnight using the midnight millisecond value and the new chronology. If the same or similar chronology is specified, but with a different time zone, the day may change. This occurs because the new DateMidnight rounds down the millisecond value to get to midnight, and the time zone change may result in a rounding down to a different day.

For example, changing time zone from London (+00:00) to Paris (+01:00) will retain the same day, but changing from Paris to London will change the day. (When its midnight in London its the same day in Paris, but when its midnight in Paris its still the previous day in London)

To avoid these unusual effects, use withZoneRetainFields(DateTimeZone) 55 to change time zones.


withZoneRetainFields

public DateMidnight withZoneRetainFields(DateTimeZone newZone)
Gets a copy of this instant with a different time zone, preserving the day The returned object will have a local time of midnight in the new zone on the same day as the original instant.


withFields

public DateMidnight withFields(ReadablePartial partial)
Gets a copy of this datetime with the partial set of fields replacing those from this instance.

For example, if the partial is a YearMonthDay then the date fields would be changed in the returned instance. If the partial is null, then this is returned.


withField

public DateMidnight withField(DateTimeFieldType fieldType,
                              int value)
Gets a copy of this datetime with the specified field set to a new value.

For example, if the field type is dayOfMonth then the day of month field would be changed in the returned instance. If the field type is null, then this is returned.

These three lines are equivalent:

 DateTime updated = dt.withField(DateTimeFieldType.dayOfMonth(), 6);
 DateTime updated = dt.dayOfMonth().setCopy(6);
 DateTime updated = dt.property(DateTimeFieldType.dayOfMonth()).setCopy(6);
 


withFieldAdded

public DateMidnight withFieldAdded(DurationFieldType fieldType,
                                   int amount)
Gets a copy of this datetime with the value of the specified field increased.

If the addition is zero or the field is null, then this is returned.

These three lines are equivalent:

 DateMidnight added = dt.withFieldAdded(DateTimeFieldType.year(), 6);
 DateMidnight added = dt.plusYears(6);
 DateMidnight added = dt.year().addToCopy(6);
 


withDurationAdded

public DateMidnight withDurationAdded(long durationToAdd,
                                      int scalar)
Gets a copy of this datetime with the specified duration added.

If the addition is zero, then this is returned.


withDurationAdded

public DateMidnight withDurationAdded(ReadableDuration durationToAdd,
                                      int scalar)
Gets a copy of this datetime with the specified duration added.

If the addition is zero, then this is returned.


withPeriodAdded

public DateMidnight withPeriodAdded(ReadablePeriod period,
                                    int scalar)
Gets a copy of this datetime with the specified period added.

If the addition is zero, then this is returned.

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


plus

public DateMidnight plus(long duration)
Gets a copy of this datetime with the specified duration added.

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


plus

public DateMidnight plus(ReadableDuration duration)
Gets a copy of this datetime with the specified duration added.

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


plus

public DateMidnight plus(ReadablePeriod period)
Gets a copy of this datetime with the specified period added.

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 plusYears(int) 55 .


plusYears

public DateMidnight plusYears(int years)
Returns a new datetime plus the specified number of years.

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

The following three lines are identical in effect:

 DateMidnight added = dt.plusYears(6);
 DateMidnight added = dt.plus(Period.years(6));
 DateMidnight added = dt.withFieldAdded(DurationFieldType.years(), 6);
 

Since:
1.1

plusMonths

public DateMidnight plusMonths(int months)
Returns a new datetime plus the specified number of months.

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

The following three lines are identical in effect:

 DateMidnight added = dt.plusMonths(6);
 DateMidnight added = dt.plus(Period.months(6));
 DateMidnight added = dt.withFieldAdded(DurationFieldType.months(), 6);
 

Since:
1.1

plusWeeks

public DateMidnight plusWeeks(int weeks)
Returns a new datetime plus the specified number of weeks.

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

The following three lines are identical in effect:

 DateMidnight added = dt.plusWeeks(6);
 DateMidnight added = dt.plus(Period.weeks(6));
 DateMidnight added = dt.withFieldAdded(DurationFieldType.weeks(), 6);
 

Since:
1.1

plusDays

public DateMidnight plusDays(int days)
Returns a new datetime plus the specified number of days.

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

The following three lines are identical in effect:

 DateMidnight added = dt.plusDays(6);
 DateMidnight added = dt.plus(Period.days(6));
 DateMidnight added = dt.withFieldAdded(DurationFieldType.days(), 6);
 

Since:
1.1

minus

public DateMidnight minus(long duration)
Gets a copy of this datetime with the specified duration take away.

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


minus

public DateMidnight minus(ReadableDuration duration)
Gets a copy of this datetime with the specified duration take away.

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


minus

public DateMidnight minus(ReadablePeriod period)
Gets a copy of this datetime with the specified period take away.

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 minusYears(int) 55 .


minusYears

public DateMidnight minusYears(int years)
Returns a new datetime minus the specified number of years.

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

The following three lines are identical in effect:

 DateTime subtracted = dt.minusYears(6);
 DateTime subtracted = dt.minus(Period.years(6));
 DateTime subtracted = dt.withFieldAdded(DurationFieldType.years(), -6);
 

Since:
1.1

minusMonths

public DateMidnight minusMonths(int months)
Returns a new datetime minus the specified number of months.

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

The following three lines are identical in effect:

 DateMidnight subtracted = dt.minusMonths(6);
 DateMidnight subtracted = dt.minus(Period.months(6));
 DateMidnight subtracted = dt.withFieldAdded(DurationFieldType.months(), -6);
 

Since:
1.1

minusWeeks

public DateMidnight minusWeeks(int weeks)
Returns a new datetime minus the specified number of weeks.

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

The following three lines are identical in effect:

 DateMidnight subtracted = dt.minusWeeks(6);
 DateMidnight subtracted = dt.minus(Period.weeks(6));
 DateMidnight subtracted = dt.withFieldAdded(DurationFieldType.weeks(), -6);
 

Since:
1.1

minusDays

public DateMidnight minusDays(int days)
Returns a new datetime minus the specified number of days.

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

The following three lines are identical in effect:

 DateMidnight subtracted = dt.minusDays(6);
 DateMidnight subtracted = dt.minus(Period.days(6));
 DateMidnight subtracted = dt.withFieldAdded(DurationFieldType.days(), -6);
 

Since:
1.1

property

public DateMidnight.Property property(DateTimeFieldType type)
Gets the property object for the specified type, which contains many useful methods.


toYearMonthDay

public YearMonthDay toYearMonthDay()
Converts this object to a YearMonthDay using the same millis and chronology.


toInterval

public Interval toInterval()
Converts this object to an Interval encompassing the whole of this day.

The interval starts at midnight 00:00 and ends at 00:00 the following day, (which is not included in the interval, as intervals are half-open).


era

public DateMidnight.Property era()
Get the era property.


centuryOfEra

public DateMidnight.Property centuryOfEra()
Get the century of era property.


yearOfCentury

public DateMidnight.Property yearOfCentury()
Get the year of century property.


yearOfEra

public DateMidnight.Property yearOfEra()
Get the year of era property.


year

public DateMidnight.Property year()
Get the year property.


weekyear

public DateMidnight.Property weekyear()
Get the year of a week based year property.


monthOfYear

public DateMidnight.Property monthOfYear()
Get the month of year property.


weekOfWeekyear

public DateMidnight.Property weekOfWeekyear()
Get the week of a week based year property.


dayOfYear

public DateMidnight.Property dayOfYear()
Get the day of year property.


dayOfMonth

public DateMidnight.Property dayOfMonth()
Get the day of month property.


dayOfWeek

public DateMidnight.Property dayOfWeek()
Get the day of week property.