use of org.joda.time.Chronology in project joda-time by JodaOrg.
the class DateTimeFormatter method parseLocalDateTime.
/**
* Parses only the local date-time from the given text, returning a new LocalDateTime.
* <p>
* This will parse the text fully according to the formatter, using the UTC zone.
* Once parsed, only the local date-time will be used.
* This means that any parsed time-zone or offset field is completely ignored.
* It also means that the zone and offset-parsed settings are ignored.
*
* @param text the text to parse, not null
* @return the parsed date-time, never null
* @throws UnsupportedOperationException if parsing is not supported
* @throws IllegalArgumentException if the text to parse is invalid
* @since 2.0
*/
public LocalDateTime parseLocalDateTime(String text) {
InternalParser parser = requireParser();
// always use UTC, avoiding DST gaps
Chronology chrono = selectChronology(null).withUTC();
DateTimeParserBucket bucket = new DateTimeParserBucket(0, chrono, iLocale, iPivotYear, iDefaultYear);
int newPos = parser.parseInto(bucket, text, 0);
if (newPos >= 0) {
if (newPos >= text.length()) {
long millis = bucket.computeMillis(true, text);
if (bucket.getOffsetInteger() != null) {
// treat withOffsetParsed() as being true
int parsedOffset = bucket.getOffsetInteger();
DateTimeZone parsedZone = DateTimeZone.forOffsetMillis(parsedOffset);
chrono = chrono.withZone(parsedZone);
} else if (bucket.getZone() != null) {
chrono = chrono.withZone(bucket.getZone());
}
return new LocalDateTime(millis, chrono);
}
} else {
newPos = ~newPos;
}
throw new IllegalArgumentException(FormatUtils.createErrorMessage(text, newPos));
}
use of org.joda.time.Chronology in project joda-time by JodaOrg.
the class DateTimeFormatter method parseMutableDateTime.
/**
* Parses a date-time from the given text, returning a new MutableDateTime.
* <p>
* The parse will use the zone and chronology specified on this formatter.
* <p>
* If the text contains a time zone string then that will be taken into
* account in adjusting the time of day as follows.
* If the {@link #withOffsetParsed()} has been called, then the resulting
* DateTime will have a fixed offset based on the parsed time zone.
* Otherwise the resulting DateTime will have the zone of this formatter,
* but the parsed zone may have caused the time to be adjusted.
*
* @param text the text to parse, not null
* @return the parsed date-time, never null
* @throws UnsupportedOperationException if parsing is not supported
* @throws IllegalArgumentException if the text to parse is invalid
*/
public MutableDateTime parseMutableDateTime(String text) {
InternalParser parser = requireParser();
Chronology chrono = selectChronology(null);
DateTimeParserBucket bucket = new DateTimeParserBucket(0, chrono, iLocale, iPivotYear, iDefaultYear);
int newPos = parser.parseInto(bucket, text, 0);
if (newPos >= 0) {
if (newPos >= text.length()) {
long millis = bucket.computeMillis(true, text);
if (iOffsetParsed && bucket.getOffsetInteger() != null) {
int parsedOffset = bucket.getOffsetInteger();
DateTimeZone parsedZone = DateTimeZone.forOffsetMillis(parsedOffset);
chrono = chrono.withZone(parsedZone);
} else if (bucket.getZone() != null) {
chrono = chrono.withZone(bucket.getZone());
}
MutableDateTime dt = new MutableDateTime(millis, chrono);
if (iZone != null) {
dt.setZone(iZone);
}
return dt;
}
} else {
newPos = ~newPos;
}
throw new IllegalArgumentException(FormatUtils.createErrorMessage(text, newPos));
}
use of org.joda.time.Chronology in project joda-time by JodaOrg.
the class DateTimeFormatter method parseInto.
//-----------------------------------------------------------------------
/**
* Parses a datetime from the given text, at the given position, saving the
* result into the fields of the given ReadWritableInstant. If the parse
* succeeds, the return value is the new text position. Note that the parse
* may succeed without fully reading the text and in this case those fields
* that were read will be set.
* <p>
* Only those fields present in the string will be changed in the specified
* instant. All other fields will remain unaltered. Thus if the string only
* contains a year and a month, then the day and time will be retained from
* the input instant. If this is not the behaviour you want, then reset the
* fields before calling this method, or use {@link #parseDateTime(String)}
* or {@link #parseMutableDateTime(String)}.
* <p>
* If it fails, the return value is negative, but the instant may still be
* modified. To determine the position where the parse failed, apply the
* one's complement operator (~) on the return value.
* <p>
* This parse method ignores the {@link #getDefaultYear() default year} and
* parses using the year from the supplied instant based on the chronology
* and time-zone of the supplied instant.
* <p>
* The parse will use the chronology of the instant.
*
* @param instant an instant that will be modified, not null
* @param text the text to parse
* @param position position to start parsing from
* @return new position, negative value means parse failed -
* apply complement operator (~) to get position of failure
* @throws UnsupportedOperationException if parsing is not supported
* @throws IllegalArgumentException if the instant is null
* @throws IllegalArgumentException if any field is out of range
*/
public int parseInto(ReadWritableInstant instant, String text, int position) {
InternalParser parser = requireParser();
if (instant == null) {
throw new IllegalArgumentException("Instant must not be null");
}
long instantMillis = instant.getMillis();
Chronology chrono = instant.getChronology();
int defaultYear = DateTimeUtils.getChronology(chrono).year().get(instantMillis);
long instantLocal = instantMillis + chrono.getZone().getOffset(instantMillis);
chrono = selectChronology(chrono);
DateTimeParserBucket bucket = new DateTimeParserBucket(instantLocal, chrono, iLocale, iPivotYear, defaultYear);
int newPos = parser.parseInto(bucket, text, position);
instant.setMillis(bucket.computeMillis(false, text));
if (iOffsetParsed && bucket.getOffsetInteger() != null) {
int parsedOffset = bucket.getOffsetInteger();
DateTimeZone parsedZone = DateTimeZone.forOffsetMillis(parsedOffset);
chrono = chrono.withZone(parsedZone);
} else if (bucket.getZone() != null) {
chrono = chrono.withZone(bucket.getZone());
}
instant.setChronology(chrono);
if (iZone != null) {
instant.setZone(iZone);
}
return newPos;
}
use of org.joda.time.Chronology in project joda-time by JodaOrg.
the class DateTimeFormatter method parseMillis.
/**
* Parses a datetime from the given text, returning the number of
* milliseconds since the epoch, 1970-01-01T00:00:00Z.
* <p>
* The parse will use the ISO chronology, and the default time zone.
* If the text contains a time zone string then that will be taken into account.
*
* @param text the text to parse, not null
* @return parsed value expressed in milliseconds since the epoch
* @throws UnsupportedOperationException if parsing is not supported
* @throws IllegalArgumentException if the text to parse is invalid
*/
public long parseMillis(String text) {
InternalParser parser = requireParser();
Chronology chrono = selectChronology(iChrono);
DateTimeParserBucket bucket = new DateTimeParserBucket(0, chrono, iLocale, iPivotYear, iDefaultYear);
return bucket.doParseMillis(parser, text);
}
use of org.joda.time.Chronology in project joda-time by JodaOrg.
the class BasePeriod method toDurationTo.
/**
* Gets the total millisecond duration of this period relative to an
* end instant.
* <p>
* This method subtracts the period from the specified instant in order
* to calculate the duration.
* <p>
* An instant must be supplied as the duration of a period varies.
* For example, a period of 1 month could vary between the equivalent of
* 28 and 31 days in milliseconds due to different length months.
* Similarly, a day can vary at Daylight Savings cutover, typically between
* 23 and 25 hours.
*
* @param endInstant the instant to subtract the period from, thus obtaining the duration
* @return the total length of the period as a duration relative to the end instant
* @throws ArithmeticException if the millis exceeds the capacity of the duration
*/
public Duration toDurationTo(ReadableInstant endInstant) {
long endMillis = DateTimeUtils.getInstantMillis(endInstant);
Chronology chrono = DateTimeUtils.getInstantChronology(endInstant);
long startMillis = chrono.add(this, endMillis, -1);
return new Duration(startMillis, endMillis);
}
Aggregations