public DateTimeZone getDateTimeZone() { return DateTimeZone.forTimeZone(TimeZone.getTimeZone(timeZone)); }
@SuppressForbidden(reason = "DateTimeZone#forID") public static DateTimeZone inferTzFromString(String tzId) { try { return DateTimeZone.forID(tzId); } catch (IllegalArgumentException e) { // also support Java timezone strings return DateTimeZone.forTimeZone(TimeZone.getTimeZone(tzId)); } }
@Override public void onReceive(Context context, Intent intent) { String tzId = intent.getStringExtra("time-zone"); try { DateTimeZone newDefault = DateTimeZone.forTimeZone(TimeZone.getDefault()); DateTimeZone.setDefault(newDefault); Log.d("joda-time-android", "TIMEZONE_CHANGED received, changed default timezone to \"" + tzId + "\""); } catch (IllegalArgumentException e) { Log.e("joda-time-android", "Could not recognize timezone id \"" + tzId + "\"", e); } }
private static ISOChronology fromTimezone(ZoneId timezone) { if (timezone == UTC) { return ISOChronology.getInstanceUTC(); } else { return ISOChronology.getInstance(DateTimeZone.forTimeZone(TimeZone.getTimeZone(timezone))); // <-- Jesus Christ, Java... } }
private static DateTimeZone getTimeZone(String id) { DateTimeZone zone; try { zone = DateTimeZone.forID(id); } catch (IllegalArgumentException e) { TimeZone timeZone = ZoneInfo.getTimeZone(id); //throw error if unrecognized zone if (timeZone == null) { throw new IllegalArgumentException("TimeZone " + id + " not recognized"); } zone = DateTimeZone.forTimeZone(timeZone); } return zone; }
public DateTimeFormatPatternSpec(String timeFormat, String sdfPatternWithTz) { _timeFormat = TimeFormat.valueOf(timeFormat); if (_timeFormat.equals(TimeFormat.SIMPLE_DATE_FORMAT)) { Matcher m = SDF_PATTERN_WITH_TIMEZONE.matcher(sdfPatternWithTz); _sdfPattern = sdfPatternWithTz; if (m.find()) { _sdfPattern = m.group(SDF_PATTERN_GROUP).trim(); String timezoneString = m.group(TIMEZONE_GROUP).trim(); _dateTimeZone = DateTimeZone.forTimeZone(TimeZone.getTimeZone(timezoneString)); } _dateTimeFormatter = DateTimeFormat.forPattern(_sdfPattern).withZone(_dateTimeZone); } }
dateTimeFormatter = dateTimeFormatter.withZone(DateTimeZone.forTimeZone(this.timeZone));
protected Calendar parseDate(String date) throws Exception { Calendar dateCalendar = null; try { dateCalendar = ISODateTimeFormat.dateTimeParser().withZone(DateTimeZone.forTimeZone( clockReader.getCurrentTimeZone())).parseDateTime(date).toCalendar(null); } catch (IllegalArgumentException e) { // try to parse a java.util.date to string back to a java.util.date dateCalendar = new GregorianCalendar(); DateFormat DATE_FORMAT = new SimpleDateFormat("EEE MMM dd kk:mm:ss z yyyy", Locale.ENGLISH); dateCalendar.setTime(DATE_FORMAT.parse(date)); } return dateCalendar; }
/** * Get the DateTimeFormatter with the this context's settings * applied to the base {@code formatter}. * @param formatter the base formatter that establishes default * formatting rules, generally context-independent * @return the contextual DateTimeFormatter */ public DateTimeFormatter getFormatter(DateTimeFormatter formatter) { if (this.chronology != null) { formatter = formatter.withChronology(this.chronology); } if (this.timeZone != null) { formatter = formatter.withZone(this.timeZone); } else { LocaleContext localeContext = LocaleContextHolder.getLocaleContext(); if (localeContext instanceof TimeZoneAwareLocaleContext) { TimeZone timeZone = ((TimeZoneAwareLocaleContext) localeContext).getTimeZone(); if (timeZone != null) { formatter = formatter.withZone(DateTimeZone.forTimeZone(timeZone)); } } } return formatter; }
@Override public Date resolveEndDate(String endDateString) { return ISODateTimeFormat.dateTimeParser().withZone(DateTimeZone.forTimeZone(clockReader.getCurrentTimeZone())).parseDateTime(endDateString).toCalendar(null).getTime(); }
/** * Get the DateTimeFormatter with the this context's settings * applied to the base {@code formatter}. * @param formatter the base formatter that establishes default * formatting rules, generally context-independent * @return the contextual DateTimeFormatter */ public DateTimeFormatter getFormatter(DateTimeFormatter formatter) { if (this.chronology != null) { formatter = formatter.withChronology(this.chronology); } if (this.timeZone != null) { formatter = formatter.withZone(this.timeZone); } else { LocaleContext localeContext = LocaleContextHolder.getLocaleContext(); if (localeContext instanceof TimeZoneAwareLocaleContext) { TimeZone timeZone = ((TimeZoneAwareLocaleContext) localeContext).getTimeZone(); if (timeZone != null) { formatter = formatter.withZone(DateTimeZone.forTimeZone(timeZone)); } } } return formatter; }
public Converter(Class<T> targetType, DynamoDBTypeConvertedTimestamp annotation) { this.formatter = DateTimeFormat.forPattern(annotation.pattern()).withZone( DateTimeZone.forTimeZone(StandardTypeConverters.Scalar.TIME_ZONE.<TimeZone>convert(annotation.timeZone())) ); this.converter = StandardTypeConverters.factory().getConverter(DateTime.class, targetType); }
@Test public void createDateTimeFormatterWithTimeZone() { factory.setPattern("yyyyMMddHHmmss Z"); factory.setTimeZone(TEST_TIMEZONE); DateTimeZone dateTimeZone = DateTimeZone.forTimeZone(TEST_TIMEZONE); DateTime dateTime = new DateTime(2009, 10, 21, 12, 10, 00, 00, dateTimeZone); String offset = (TEST_TIMEZONE.equals(NEW_YORK) ? "-0400" : "+0200"); assertThat(factory.createDateTimeFormatter().print(dateTime), is("20091021121000 " + offset)); }
@Test public void testSetTimeZoneId() throws Exception { TimeZoneKey defaultZoneKey = TimeZoneKey.getTimeZoneKey(TimeZone.getDefault().getID()); DateTimeZone defaultZone = DateTimeZone.forTimeZone(TimeZone.getDefault()); String sql = "SELECT current_timezone() zone, TIMESTAMP '2001-02-03 3:04:05' ts"; try (Connection connection = createConnection()) { try (Statement statement = connection.createStatement(); ResultSet rs = statement.executeQuery(sql)) { assertTrue(rs.next()); assertEquals(rs.getString("zone"), defaultZoneKey.getId()); assertEquals(rs.getTimestamp("ts"), new Timestamp(new DateTime(2001, 2, 3, 3, 4, 5, defaultZone).getMillis())); } connection.unwrap(PrestoConnection.class).setTimeZoneId("UTC"); try (Statement statement = connection.createStatement(); ResultSet rs = statement.executeQuery(sql)) { assertTrue(rs.next()); assertEquals(rs.getString("zone"), "UTC"); assertEquals(rs.getTimestamp("ts"), new Timestamp(new DateTime(2001, 2, 3, 3, 4, 5, DateTimeZone.UTC).getMillis())); } } }
@Test public void testTruncateDhaka() { final DateTimeZone tz = DateTimeZone.forTimeZone(TimeZone.getTimeZone("Asia/Dhaka")); final DateTime date = new DateTime("2011-03-15T21:42:23.898+06:00", tz); final PeriodGranularity year = new PeriodGranularity(new Period("P1Y"), null, tz); final PeriodGranularity hour = new PeriodGranularity(new Period("PT1H"), null, tz); final PeriodGranularity twoHour = new PeriodGranularity(new Period("PT2H"), null, tz); Assert.assertEquals( new DateTime("2011-01-01T00:00:00.000+06:00", tz), year.toDateTime(year.bucketStart(date).getMillis()) ); Assert.assertEquals( new DateTime("2011-03-15T21:00:00.000+06:00", tz), hour.toDateTime(hour.bucketStart(date).getMillis()) ); Assert.assertEquals( new DateTime("2011-03-15T20:00:00.000+06:00", tz), twoHour.toDateTime(twoHour.bucketStart(date).getMillis()) ); }
@Test public void testTruncateKathmandu() { final DateTimeZone tz = DateTimeZone.forTimeZone(TimeZone.getTimeZone("Asia/Kathmandu")); final DateTime date = new DateTime("2011-03-15T21:42:23.898+05:45", tz); final PeriodGranularity year = new PeriodGranularity(new Period("P1Y"), null, tz); final PeriodGranularity hour = new PeriodGranularity(new Period("PT1H"), null, tz); final PeriodGranularity twoHour = new PeriodGranularity(new Period("PT2H"), null, tz); Assert.assertEquals( new DateTime("2011-01-01T00:00:00.000+05:45", tz), year.toDateTime(year.bucketStart(date).getMillis()) ); Assert.assertEquals( new DateTime("2011-03-15T21:00:00.000+05:45", tz), hour.toDateTime(hour.bucketStart(date).getMillis()) ); Assert.assertEquals( new DateTime("2011-03-15T20:00:00.000+05:45", tz), twoHour.toDateTime(twoHour.bucketStart(date).getMillis()) ); }
@Test public void testTimeZoneConversion() { TimeZone jdkTimeZone = TimeZone.getTimeZone("GMT-10"); assertEquals("GMT-10:00", jdkTimeZone.getID()); DateTimeZone jodaTimeZone = DateTimeZone.forTimeZone(jdkTimeZone); assertEquals("-10:00", jodaTimeZone.getID()); assertEquals(jdkTimeZone.getRawOffset(), jodaTimeZone.getOffset(0L)); TimeZone convertedTimeZone = jodaTimeZone.toTimeZone(); assertEquals("GMT-10:00", jdkTimeZone.getID()); assertEquals(jdkTimeZone.getID(), convertedTimeZone.getID()); assertEquals(jdkTimeZone.getRawOffset(), convertedTimeZone.getRawOffset()); }
@Test public void testAuto() { final Function<Object, DateTime> parser = TimestampParser.createObjectTimestampParser("auto"); Assert.assertEquals(DateTimes.of("2009-02-13T23:31:30Z"), parser.apply("1234567890000")); Assert.assertEquals(DateTimes.of("2009-02-13T23:31:30Z"), parser.apply("2009-02-13T23:31:30Z")); Assert.assertEquals(DateTimes.of("2009-02-13T23:31:30-08:00"), parser.apply("2009-02-13T23:31:30-08:00")); Assert.assertEquals(DateTimes.of("2009-02-13T23:31:30Z"), parser.apply("2009-02-13 23:31:30Z")); Assert.assertEquals(DateTimes.of("2009-02-13T23:31:30-08:00"), parser.apply("2009-02-13 23:31:30-08:00")); Assert.assertEquals(DateTimes.of("2009-02-13T00:00:00Z"), parser.apply("2009-02-13")); Assert.assertEquals(DateTimes.of("2009-02-13T00:00:00Z"), parser.apply("\"2009-02-13\"")); Assert.assertEquals(DateTimes.of("2009-02-13T23:31:30Z"), parser.apply("2009-02-13 23:31:30")); Assert.assertEquals(DateTimes.of("2009-02-13T23:31:30Z"), parser.apply(1234567890000L)); Assert.assertEquals(DateTimes.of("2009-02-13T23:31:30Z"), parser.apply("2009-02-13 23:31:30 UTC")); Assert.assertEquals(new DateTime("2009-02-13T23:31:30Z", DateTimeZone.forTimeZone(TimeZone.getTimeZone("PST"))), parser.apply("2009-02-13 23:31:30 PST")); Assert.assertEquals(new DateTime("2009-02-13T23:31:30Z", DateTimeZone.forTimeZone(TimeZone.getTimeZone("PST"))), parser.apply("\"2009-02-13 23:31:30 PST\"")); }
out.writeLong(dateTime.toDateTime(DateTimeZone.forTimeZone(JSON.defaultTimeZone)).toInstant().getMillis());
@Test public void testExtractTimeZone() { Assert.assertEquals(DateTimeZone.UTC, ParserUtils.getDateTimeZone("UTC")); Assert.assertEquals(DateTimeZone.forTimeZone(TimeZone.getTimeZone("PST")), ParserUtils.getDateTimeZone("PST")); Assert.assertNull(ParserUtils.getDateTimeZone("Hello")); Assert.assertNull(ParserUtils.getDateTimeZone("AEST")); Assert.assertEquals(DateTimeZone.forTimeZone(TimeZone.getTimeZone("Australia/Hobart")), ParserUtils.getDateTimeZone("Australia/Hobart")); Assert.assertNull(ParserUtils.getDateTimeZone("")); Assert.assertNull(ParserUtils.getDateTimeZone(null)); }