Java Code Examples for org.joda.time.DateTimeZone#getDefault()

The following examples show how to use org.joda.time.DateTimeZone#getDefault() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ISOChronology.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Gets an instance of the ISOChronology in the given time zone.
 * 
 * @param zone  the time zone to get the chronology in, null is default
 * @return a chronology in the specified time zone
 */
public static ISOChronology getInstance(DateTimeZone zone) {
    if (zone == null) {
        zone = DateTimeZone.getDefault();
    }
    int index = System.identityHashCode(zone) & (FAST_CACHE_SIZE - 1);
    ISOChronology chrono = cFastCache[index];
    if (chrono != null && chrono.getZone() == zone) {
        return chrono;
    }
    synchronized (cCache) {
        chrono = cCache.get(zone);
        if (chrono == null) {
            chrono = new ISOChronology(ZonedChronology.getInstance(INSTANCE_UTC, zone));
            cCache.put(zone, chrono);
        }
    }
    cFastCache[index] = chrono;
    return chrono;
}
 
Example 2
Source File: TestISOPeriodFormat.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
protected void setUp() throws Exception {
    DateTimeUtils.setCurrentMillisFixed(TEST_TIME_NOW);
    originalDateTimeZone = DateTimeZone.getDefault();
    originalTimeZone = TimeZone.getDefault();
    originalLocale = Locale.getDefault();
    DateTimeZone.setDefault(LONDON);
    TimeZone.setDefault(TimeZone.getTimeZone("Europe/London"));
    Locale.setDefault(Locale.UK);
}
 
Example 3
Source File: TestDateTimeFormatStyle.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
protected void setUp() throws Exception {
    DateTimeUtils.setCurrentMillisFixed(TEST_TIME_NOW);
    originalDateTimeZone = DateTimeZone.getDefault();
    originalTimeZone = TimeZone.getDefault();
    originalLocale = Locale.getDefault();
    DateTimeZone.setDefault(LONDON);
    TimeZone.setDefault(TimeZone.getTimeZone("Europe/London"));
    Locale.setDefault(UK);
}
 
Example 4
Source File: TestDateUtils.java    From joda-time-android with Apache License 2.0 5 votes vote down vote up
@Before
public void setUp() throws Exception {
    // Init zone info
    Context context = InstrumentationRegistry.getInstrumentation().getContext();
    JodaTimeAndroid.init(context);

    // Force the system into 24-hour time for tests
    ContentResolver cr = context.getContentResolver();
    mOldTime1224Setting = Settings.System.getString(cr, Settings.System.TIME_12_24);
    Settings.System.putString(cr, Settings.System.TIME_12_24, "24");

    // Force all tests to be in the US locale; that way we can test output in consistent manner
    Application app = (Application) ApplicationProvider.getApplicationContext();
    Resources res = app.getBaseContext().getResources();
    Configuration config = res.getConfiguration();
    Locale.setDefault(Locale.US);
    config.locale = Locale.US;
    res.updateConfiguration(config, res.getDisplayMetrics());

    // Force the default timezone
    mDefaultJodaTz = DateTimeZone.forID("America/New_York");
    mOldDefaultJodaTz = DateTimeZone.getDefault();
    DateTimeZone.setDefault(mDefaultJodaTz);

    // ...And for the system as well
    mDefaultSystemTz = TimeZone.getTimeZone("America/Chicago");
    mOldDefaultSystemTz = TimeZone.getDefault();
    TimeZone.setDefault(mDefaultSystemTz);

    // Force current "now" time, so all tests can be consistent
    mNow = new DateTime(YEAR, MONTH_OF_YEAR, DAY_OF_MONTH, HOUR_OF_DAY,
        MINUTE_OF_HOUR, SECOND_OF_MINUTE, MILLIS_OF_SECOND, mDefaultJodaTz);
    DateTimeUtils.setCurrentMillisFixed(mNow.getMillis());
}
 
Example 5
Source File: TestCopticChronology.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
protected void setUp() throws Exception {
    DateTimeUtils.setCurrentMillisFixed(TEST_TIME_NOW);
    originalDateTimeZone = DateTimeZone.getDefault();
    originalTimeZone = TimeZone.getDefault();
    originalLocale = Locale.getDefault();
    DateTimeZone.setDefault(LONDON);
    TimeZone.setDefault(TimeZone.getTimeZone("Europe/London"));
    Locale.setDefault(Locale.UK);
}
 
Example 6
Source File: TestDateFunctionsUtils.java    From dremio-oss with Apache License 2.0 5 votes vote down vote up
@Test
public void stringToTimestampChangeTZOffset() {
  DateTimeZone defaultTZ = DateTimeZone.getDefault();
  try {
    assertEquals(calculateTime("2008-09-15 10:53:00"), formatTimeStamp("2008-09-15T15:53:00+0500", DateFunctionsUtils.getSQLFormatterForFormatString("YYYY-MM-DD\"T\"HH24:MI:SSTZO")));
    assertEquals(calculateTime("2011-01-01 04:00:00"), formatTimeStamp("2010-12-31T23:00:00-0500", DateFunctionsUtils.getSQLFormatterForFormatString("YYYY-MM-DD\"T\"HH24:MI:SSTZO")));
    assertEquals(calculateTime("2010-12-31 23:00:00"), formatTimeStamp("2010-12-31T23:00:00", DateFunctionsUtils.getSQLFormatterForFormatString("YYYY-MM-DD\"T\"HH24:MI:SS")));
  } finally {
    DateTimeZone.setDefault(defaultTZ);
  }
}
 
Example 7
Source File: LenientChronology.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
public Chronology withZone(DateTimeZone zone) {
    if (zone == null) {
        zone = DateTimeZone.getDefault();
    }
    if (zone == DateTimeZone.UTC) {
        return withUTC();
    }
    if (zone == getZone()) {
        return this;
    }
    return LenientChronology.getInstance(getBase().withZone(zone));
}
 
Example 8
Source File: TestBuddhistChronology.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
protected void setUp() throws Exception {
    DateTimeUtils.setCurrentMillisFixed(TEST_TIME_NOW);
    originalDateTimeZone = DateTimeZone.getDefault();
    originalTimeZone = TimeZone.getDefault();
    originalLocale = Locale.getDefault();
    DateTimeZone.setDefault(LONDON);
    TimeZone.setDefault(TimeZone.getTimeZone("Europe/London"));
    Locale.setDefault(Locale.UK);
}
 
Example 9
Source File: CalendarConverter.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Gets the chronology.
 * <p>
 * If a chronology is specified then it is used.
 * Otherwise, it is the GJChronology if a GregorianCalendar is used,
 * BuddhistChronology if a BuddhistCalendar is used or ISOChronology otherwise.
 * The time zone is extracted from the calendar if possible, default used if not.
 * 
 * @param object  the Calendar to convert, must not be null
 * @param chrono  the chronology to use, null means use Calendar
 * @return the chronology, never null
 * @throws NullPointerException if the object is null
 * @throws ClassCastException if the object is an invalid type
 */
public Chronology getChronology(Object object, Chronology chrono) {
    if (chrono != null) {
        return chrono;
    }
    Calendar cal = (Calendar) object;
    DateTimeZone zone = null;
    try {
        zone = DateTimeZone.forTimeZone(cal.getTimeZone());
        
    } catch (IllegalArgumentException ex) {
        zone = DateTimeZone.getDefault();
    }
    return getChronology(cal, zone);
}
 
Example 10
Source File: TestStringConverter.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
protected void setUp() throws Exception {
    zone = DateTimeZone.getDefault();
    locale = Locale.getDefault();
    DateTimeZone.setDefault(LONDON);
    Locale.setDefault(Locale.UK);
    
    JULIAN = JulianChronology.getInstance();
    ISO = ISOChronology.getInstance();
}
 
Example 11
Source File: TestGJChronology.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
protected void setUp() throws Exception {
    DateTimeUtils.setCurrentMillisFixed(TEST_TIME_NOW);
    originalDateTimeZone = DateTimeZone.getDefault();
    originalTimeZone = TimeZone.getDefault();
    originalLocale = Locale.getDefault();
    DateTimeZone.setDefault(LONDON);
    TimeZone.setDefault(TimeZone.getTimeZone("Europe/London"));
    Locale.setDefault(Locale.UK);
}
 
Example 12
Source File: TestISODateTimeFormatParsing.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
protected void setUp() throws Exception {
    originalDateTimeZone = DateTimeZone.getDefault();
    originalTimeZone = TimeZone.getDefault();
    originalLocale = Locale.getDefault();
    DateTimeZone.setDefault(DateTimeZone.forID("Europe/London"));
    TimeZone.setDefault(TimeZone.getTimeZone("Europe/London"));
    Locale.setDefault(Locale.UK);
}
 
Example 13
Source File: ValueConverterTest.java    From components with Apache License 2.0 5 votes vote down vote up
@Test
public void testXMLGregorianCalendarConverter() throws Exception {
    DateTimeZone tz1 = DateTimeZone.getDefault();

    MutableDateTime dateTime1 = new MutableDateTime(tz1);
    dateTime1.setDate(System.currentTimeMillis());
    Long controlValue1 = dateTime1.getMillis();

    XMLGregorianCalendar xmlCalendar1 = datatypeFactory.newXMLGregorianCalendar();
    xmlCalendar1.setYear(dateTime1.getYear());
    xmlCalendar1.setMonth(dateTime1.getMonthOfYear());
    xmlCalendar1.setDay(dateTime1.getDayOfMonth());
    xmlCalendar1.setHour(dateTime1.getHourOfDay());
    xmlCalendar1.setMinute(dateTime1.getMinuteOfHour());
    xmlCalendar1.setSecond(dateTime1.getSecondOfMinute());
    xmlCalendar1.setMillisecond(dateTime1.getMillisOfSecond());
    xmlCalendar1.setTimezone(tz1.toTimeZone().getOffset(dateTime1.getMillis()) / 60000);

    FieldDesc fieldInfo = typeDesc.getField("tranDate");

    NsObjectInputTransducer transducer = new NsObjectInputTransducer(clientService, schema, typeDesc.getTypeName());

    AvroConverter<XMLGregorianCalendar, Long> converter1 =
            (AvroConverter<XMLGregorianCalendar, Long>) transducer.getValueConverter(fieldInfo);
    assertEquals(AvroUtils._logicalTimestamp(), converter1.getSchema());
    assertEquals(XMLGregorianCalendar.class, converter1.getDatumClass());
    assertEquals(controlValue1,
            converter1.convertToAvro(xmlCalendar1));
    assertEquals(xmlCalendar1,
            converter1.convertToDatum(controlValue1));

    AvroConverter<XMLGregorianCalendar, Object> converter2 =
            (AvroConverter<XMLGregorianCalendar, Object>) transducer.getValueConverter(fieldInfo);
    assertEquals(xmlCalendar1,
            converter2.convertToDatum(new Date(controlValue1.longValue())));

    assertNull(converter1.convertToAvro(null));
}
 
Example 14
Source File: TestISOChronology.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
protected void setUp() throws Exception {
    DateTimeUtils.setCurrentMillisFixed(TEST_TIME_NOW);
    originalDateTimeZone = DateTimeZone.getDefault();
    originalTimeZone = TimeZone.getDefault();
    originalLocale = Locale.getDefault();
    DateTimeZone.setDefault(LONDON);
    TimeZone.setDefault(TimeZone.getTimeZone("Europe/London"));
    Locale.setDefault(Locale.UK);
}
 
Example 15
Source File: JulianChronology.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Gets an instance of the JulianChronology in the given time zone.
 * 
 * @param zone  the time zone to get the chronology in, null is default
 * @param minDaysInFirstWeek  minimum number of days in first week of the year; default is 4
 * @return a chronology in the specified time zone
 */
public static JulianChronology getInstance(DateTimeZone zone, int minDaysInFirstWeek) {
    if (zone == null) {
        zone = DateTimeZone.getDefault();
    }
    JulianChronology chrono;
    synchronized (cCache) {
        JulianChronology[] chronos = cCache.get(zone);
        if (chronos == null) {
            chronos = new JulianChronology[7];
            cCache.put(zone, chronos);
        }
        try {
            chrono = chronos[minDaysInFirstWeek - 1];
        } catch (ArrayIndexOutOfBoundsException e) {
            throw new IllegalArgumentException
                ("Invalid min days in first week: " + minDaysInFirstWeek);
        }
        if (chrono == null) {
            if (zone == DateTimeZone.UTC) {
                chrono = new JulianChronology(null, null, minDaysInFirstWeek);
            } else {
                chrono = getInstance(DateTimeZone.UTC, minDaysInFirstWeek);
                chrono = new JulianChronology
                    (ZonedChronology.getInstance(chrono, zone), null, minDaysInFirstWeek);
            }
            chronos[minDaysInFirstWeek - 1] = chrono;
        }
    }
    return chrono;
}
 
Example 16
Source File: CronExpressionTest.java    From cron with Apache License 2.0 4 votes vote down vote up
@Before
public void setUp() {
    original = DateTimeZone.getDefault();
    DateTimeZone.setDefault(DateTimeZone.forID("Europe/Oslo"));
}
 
Example 17
Source File: TestFixedDateTimeZone.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
protected void setUp() throws Exception {
    originalDateTimeZone = DateTimeZone.getDefault();
    DateTimeZone.setDefault(DateTimeZone.UTC);
}
 
Example 18
Source File: TimeCalculation.java    From mangosta-android with Apache License 2.0 4 votes vote down vote up
public static String getTimeStringAgoSinceDate(Context context, Date date) {
    DateTime postDate = new DateTime(date, DateTimeZone.getDefault());
    return getTimeStringAgoSinceDateTime(context, postDate);
}
 
Example 19
Source File: TimeCalculation.java    From mangosta-android with Apache License 2.0 4 votes vote down vote up
public static boolean wasMinutesAgoMax(Date date, int maxMinutes) {
    DateTime now = new DateTime(DateTimeZone.getDefault());
    DateTime dateTime = new DateTime(date, DateTimeZone.getDefault());
    return isMinutesDiffMax(dateTime, now, maxMinutes);
}
 
Example 20
Source File: TestTextFields.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
protected void setUp() throws Exception {
    originalDateTimeZone = DateTimeZone.getDefault();
    originalLocale = Locale.getDefault();
    DateTimeZone.setDefault(ZONES[0]);
    Locale.setDefault(Locale.ENGLISH);
}