Java example source code file (ZonedChronology.java)
This example Java source code file (ZonedChronology.java) is included in the alvinalexander.com
"Java Source Code
Warehouse" project. The intent of this project is to help you "Learn
Java by Example" TM.
/*
* Copyright 2001-2015 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.chrono;
import java.util.HashMap;
import java.util.Locale;
import org.joda.time.Chronology;
import org.joda.time.DateTimeConstants;
import org.joda.time.DateTimeField;
import org.joda.time.DateTimeZone;
import org.joda.time.DurationField;
import org.joda.time.IllegalFieldValueException;
import org.joda.time.IllegalInstantException;
import org.joda.time.ReadablePartial;
import org.joda.time.field.BaseDateTimeField;
import org.joda.time.field.BaseDurationField;
/**
* Wraps another Chronology to add support for time zones.
* <p>
* ZonedChronology is thread-safe and immutable.
*
* @author Brian S O'Neill
* @author Stephen Colebourne
* @since 1.0
*/
public final class ZonedChronology extends AssembledChronology {
/** Serialization lock */
private static final long serialVersionUID = -1079258847191166848L;
/**
* Avoid calculation errors near zero.
*/
private static final long NEAR_ZERO = 7L * 24 * 60 * 60 * 1000;
/**
* Create a ZonedChronology for any chronology, overriding any time zone it
* may already have.
*
* @param base base chronology to wrap
* @param zone the time zone
* @throws IllegalArgumentException if chronology or time zone is null
*/
public static ZonedChronology getInstance(Chronology base, DateTimeZone zone) {
if (base == null) {
throw new IllegalArgumentException("Must supply a chronology");
}
base = base.withUTC();
if (base == null) {
throw new IllegalArgumentException("UTC chronology must not be null");
}
if (zone == null) {
throw new IllegalArgumentException("DateTimeZone must not be null");
}
return new ZonedChronology(base, zone);
}
static boolean useTimeArithmetic(DurationField field) {
// Use time of day arithmetic rules for unit durations less than
// typical time zone offsets.
return field != null && field.getUnitMillis() < DateTimeConstants.MILLIS_PER_HOUR * 12;
}
/**
* Restricted constructor
*
* @param base base chronology to wrap
* @param zone the time zone
*/
private ZonedChronology(Chronology base, DateTimeZone zone) {
super(base, zone);
}
public DateTimeZone getZone() {
return (DateTimeZone)getParam();
}
public Chronology withUTC() {
return getBase();
}
public Chronology withZone(DateTimeZone zone) {
if (zone == null) {
zone = DateTimeZone.getDefault();
}
if (zone == getParam()) {
return this;
}
if (zone == DateTimeZone.UTC) {
return getBase();
}
return new ZonedChronology(getBase(), zone);
}
public long getDateTimeMillis(int year, int monthOfYear, int dayOfMonth,
int millisOfDay)
throws IllegalArgumentException
{
return localToUTC(getBase().getDateTimeMillis
(year, monthOfYear, dayOfMonth, millisOfDay));
}
public long getDateTimeMillis(int year, int monthOfYear, int dayOfMonth,
int hourOfDay, int minuteOfHour,
int secondOfMinute, int millisOfSecond)
throws IllegalArgumentException
{
return localToUTC(getBase().getDateTimeMillis
(year, monthOfYear, dayOfMonth,
hourOfDay, minuteOfHour, secondOfMinute, millisOfSecond));
}
public long getDateTimeMillis(long instant,
int hourOfDay, int minuteOfHour,
int secondOfMinute, int millisOfSecond)
throws IllegalArgumentException
{
return localToUTC(getBase().getDateTimeMillis
(instant + getZone().getOffset(instant),
hourOfDay, minuteOfHour, secondOfMinute, millisOfSecond));
}
/**
* @param localInstant the instant from 1970-01-01T00:00:00 local time
* @return the instant from 1970-01-01T00:00:00Z
*/
private long localToUTC(long localInstant) {
if (localInstant == Long.MAX_VALUE) {
return Long.MAX_VALUE;
} else if (localInstant == Long.MIN_VALUE) {
return Long.MIN_VALUE;
}
DateTimeZone zone = getZone();
int offset = zone.getOffsetFromLocal(localInstant);
long utcInstant = localInstant - offset;
if (localInstant > NEAR_ZERO && utcInstant < 0) {
return Long.MAX_VALUE;
} else if (localInstant < -NEAR_ZERO && utcInstant > 0) {
return Long.MIN_VALUE;
}
int offsetBasedOnUtc = zone.getOffset(utcInstant);
if (offset != offsetBasedOnUtc) {
throw new IllegalInstantException(localInstant, zone.getID());
}
return utcInstant;
}
protected void assemble(Fields fields) {
// Keep a local cache of converted fields so as not to create redundant
// objects.
HashMap<Object, Object> converted = new HashMap
Other Java examples (source code examples)
Here is a short list of links related to this Java ZonedChronology.java source code file: