/** * Gets a time zone instance for the specified offset to UTC in hours. * This method assumes standard length hours. * <p> * This factory is a convenient way of constructing zones with a fixed offset. * * @param hoursOffset the offset in hours from UTC, from -23 to +23 * @return the DateTimeZone object for the offset * @throws IllegalArgumentException if the offset is too large or too small */ public static DateTimeZone forOffsetHours(int hoursOffset) throws IllegalArgumentException { return forOffsetHoursMinutes(hoursOffset, 0); }
/** * Gets a time zone instance for the specified offset to UTC in hours. * This method assumes standard length hours. * <p> * This factory is a convenient way of constructing zones with a fixed offset. * * @param hoursOffset the offset in hours from UTC, from -23 to +23 * @return the DateTimeZone object for the offset * @throws IllegalArgumentException if the offset is too large or too small */ public static DateTimeZone forOffsetHours(int hoursOffset) throws IllegalArgumentException { return forOffsetHoursMinutes(hoursOffset, 0); }
@Test public void testCommonDateTimePattern() { DateTime dt1 = DateTimes.nowUtc(); DateTime dt2 = new DateTime(System.currentTimeMillis(), DateTimes.inferTzFromString("IST")); DateTime dt3 = new DateTime(System.currentTimeMillis(), DateTimeZone.forOffsetHoursMinutes(1, 30)); for (DateTime dt : new DateTime[] {dt1, dt2, dt3}) { Assert.assertTrue(DateTimes.COMMON_DATE_TIME_PATTERN.matcher(dt.toString()).matches()); } } }
int h = offset / 3600; int m = offset % 3600; return org.joda.time.DateTimeZone.forOffsetHoursMinutes(h, m); } catch (IllegalArgumentException ex) {
assertEquals(rs.getObject("a"), new Time(new DateTime(1970, 1, 1, 3, 4, 5).getMillis())); assertEquals(rs.getTime(2), new Time(new DateTime(1970, 1, 1, 6, 7, 8, DateTimeZone.forOffsetHoursMinutes(6, 17)).getMillis())); assertEquals(rs.getTime(2, ASIA_ORAL_CALENDAR), new Time(new DateTime(1970, 1, 1, 6, 7, 8, DateTimeZone.forOffsetHoursMinutes(6, 17)).getMillis())); assertEquals(rs.getObject(2), new Time(new DateTime(1970, 1, 1, 6, 7, 8, DateTimeZone.forOffsetHoursMinutes(6, 17)).getMillis())); assertEquals(rs.getTime("b"), new Time(new DateTime(1970, 1, 1, 6, 7, 8, DateTimeZone.forOffsetHoursMinutes(6, 17)).getMillis())); assertEquals(rs.getTime("b", ASIA_ORAL_CALENDAR), new Time(new DateTime(1970, 1, 1, 6, 7, 8, DateTimeZone.forOffsetHoursMinutes(6, 17)).getMillis())); assertEquals(rs.getObject("b"), new Time(new DateTime(1970, 1, 1, 6, 7, 8, DateTimeZone.forOffsetHoursMinutes(6, 17)).getMillis())); assertEquals(rs.getObject("d"), new Timestamp(new DateTime(2001, 2, 3, 3, 4, 5).getMillis())); assertEquals(rs.getTimestamp(5), new Timestamp(new DateTime(2004, 5, 6, 6, 7, 8, DateTimeZone.forOffsetHoursMinutes(6, 17)).getMillis())); assertEquals(rs.getTimestamp(5, ASIA_ORAL_CALENDAR), new Timestamp(new DateTime(2004, 5, 6, 6, 7, 8, DateTimeZone.forOffsetHoursMinutes(6, 17)).getMillis())); assertEquals(rs.getObject(5), new Timestamp(new DateTime(2004, 5, 6, 6, 7, 8, DateTimeZone.forOffsetHoursMinutes(6, 17)).getMillis())); assertEquals(rs.getTimestamp("e"), new Timestamp(new DateTime(2004, 5, 6, 6, 7, 8, DateTimeZone.forOffsetHoursMinutes(6, 17)).getMillis())); assertEquals(rs.getTimestamp("e", ASIA_ORAL_CALENDAR), new Timestamp(new DateTime(2004, 5, 6, 6, 7, 8, DateTimeZone.forOffsetHoursMinutes(6, 17)).getMillis())); assertEquals(rs.getObject("e"), new Timestamp(new DateTime(2004, 5, 6, 6, 7, 8, DateTimeZone.forOffsetHoursMinutes(6, 17)).getMillis()));
@Test public void testForOffsetHoursMinutes_int_int() { assertEquals(DateTimeZone.UTC, DateTimeZone.forOffsetHoursMinutes(0, 0)); assertEquals(DateTimeZone.forID("+23:59"), DateTimeZone.forOffsetHoursMinutes(23, 59)); assertEquals(DateTimeZone.forID("+02:15"), DateTimeZone.forOffsetHoursMinutes(2, 15)); assertEquals(DateTimeZone.forID("+02:00"), DateTimeZone.forOffsetHoursMinutes(2, 0)); try { DateTimeZone.forOffsetHoursMinutes(2, -15); fail(); } catch (IllegalArgumentException ex) {} assertEquals(DateTimeZone.forID("+00:15"), DateTimeZone.forOffsetHoursMinutes(0, 15)); assertEquals(DateTimeZone.forID("+00:00"), DateTimeZone.forOffsetHoursMinutes(0, 0)); assertEquals(DateTimeZone.forID("-00:15"), DateTimeZone.forOffsetHoursMinutes(0, -15)); assertEquals(DateTimeZone.forID("-02:00"), DateTimeZone.forOffsetHoursMinutes(-2, 0)); assertEquals(DateTimeZone.forID("-02:15"), DateTimeZone.forOffsetHoursMinutes(-2, -15)); assertEquals(DateTimeZone.forID("-02:15"), DateTimeZone.forOffsetHoursMinutes(-2, 15)); assertEquals(DateTimeZone.forID("-23:59"), DateTimeZone.forOffsetHoursMinutes(-23, 59)); try { DateTimeZone.forOffsetHoursMinutes(2, 60); fail(); } catch (IllegalArgumentException ex) {} try { DateTimeZone.forOffsetHoursMinutes(-2, 60); fail(); } catch (IllegalArgumentException ex) {} try {
DateTimeZone dateTimeZone = DateTimeZone.forOffsetHoursMinutes(offsetHours, offsetMinutes); assertTimeZone(dateTimeZone.getID(), dateTimeZone);
/** * Gets a time zone instance for the specified offset to UTC in hours. * This method assumes standard length hours. * <p> * This factory is a convenient way of constructing zones with a fixed offset. * * @param hoursOffset the offset in hours from UTC * @return the DateTimeZone object for the offset * @throws IllegalArgumentException if the offset is too large or too small */ public static DateTimeZone forOffsetHours(int hoursOffset) throws IllegalArgumentException { return forOffsetHoursMinutes(hoursOffset, 0); }
/** * @see org.joda.time.DateTimeZone#forOffsetHoursMinutes(int, int) */ public DateTimeZone getTimezone(int offsetHours, int offsetMinutes) { return DateTimeZone.forOffsetHoursMinutes(offsetHours, offsetMinutes); }
/** * Gets a time zone instance for the specified offset to UTC in hours. * This method assumes standard length hours. * <p> * This factory is a convenient way of constructing zones with a fixed offset. * * @param hoursOffset the offset in hours from UTC, from -23 to +23 * @return the DateTimeZone object for the offset * @throws IllegalArgumentException if the offset is too large or too small */ public static DateTimeZone forOffsetHours(int hoursOffset) throws IllegalArgumentException { return forOffsetHoursMinutes(hoursOffset, 0); }
/** * Gets a time zone instance for the specified offset to UTC in hours. * This method assumes standard length hours. * <p> * This factory is a convenient way of constructing zones with a fixed offset. * * @param hoursOffset the offset in hours from UTC, from -23 to +23 * @return the DateTimeZone object for the offset * @throws IllegalArgumentException if the offset is too large or too small */ public static DateTimeZone forOffsetHours(int hoursOffset) throws IllegalArgumentException { return forOffsetHoursMinutes(hoursOffset, 0); }
/** * Gets a time zone instance for the specified offset to UTC in hours. * This method assumes standard length hours. * <p> * This factory is a convenient way of constructing zones with a fixed offset. * * @param hoursOffset the offset in hours from UTC, from -23 to +23 * @return the DateTimeZone object for the offset * @throws IllegalArgumentException if the offset is too large or too small */ public static DateTimeZone forOffsetHours(int hoursOffset) throws IllegalArgumentException { return forOffsetHoursMinutes(hoursOffset, 0); }
/** * Gets a time zone instance for the specified offset to UTC in hours. * This method assumes standard length hours. * <p> * This factory is a convenient way of constructing zones with a fixed offset. * * @param hoursOffset the offset in hours from UTC * @return the DateTimeZone object for the offset * @throws IllegalArgumentException if the offset is too large or too small */ public static DateTimeZone forOffsetHours(int hoursOffset) throws IllegalArgumentException { return forOffsetHoursMinutes(hoursOffset, 0); }
/** * Gets a time zone instance for the specified offset to UTC in hours. * This method assumes standard length hours. * <p> * This factory is a convenient way of constructing zones with a fixed offset. * * @param hoursOffset the offset in hours from UTC * @return the DateTimeZone object for the offset * @throws IllegalArgumentException if the offset is too large or too small */ public static DateTimeZone forOffsetHours(int hoursOffset) throws IllegalArgumentException { return forOffsetHoursMinutes(hoursOffset, 0); }
/** * Gets a time zone instance for the specified offset to UTC in hours. * This method assumes standard length hours. * <p> * This factory is a convenient way of constructing zones with a fixed offset. * * @param hoursOffset the offset in hours from UTC, from -23 to +23 * @return the DateTimeZone object for the offset * @throws IllegalArgumentException if the offset is too large or too small */ public static DateTimeZone forOffsetHours(int hoursOffset) throws IllegalArgumentException { return forOffsetHoursMinutes(hoursOffset, 0); }
/** * Gets a time zone instance for the specified offset to UTC in hours. * This method assumes standard length hours. * <p> * This factory is a convenient way of constructing zones with a fixed offset. * * @param hoursOffset the offset in hours from UTC, from -23 to +23 * @return the DateTimeZone object for the offset * @throws IllegalArgumentException if the offset is too large or too small */ public static DateTimeZone forOffsetHours(int hoursOffset) throws IllegalArgumentException { return forOffsetHoursMinutes(hoursOffset, 0); }
/** * Gets a time zone instance for the specified offset to UTC in hours. * This method assumes standard length hours. * <p> * This factory is a convenient way of constructing zones with a fixed offset. * * @param hoursOffset the offset in hours from UTC, from -23 to +23 * @return the DateTimeZone object for the offset * @throws IllegalArgumentException if the offset is too large or too small */ public static DateTimeZone forOffsetHours(int hoursOffset) throws IllegalArgumentException { return forOffsetHoursMinutes(hoursOffset, 0); }
public static DateTimeZone resolveDateTimeZone(BigDecimal offset) { if (offset == null) { return DateTimeZone.forOffsetMillis(TimeZone.getDefault().getRawOffset()); } else { int minuteOffset = new BigDecimal("60").multiply(offset.remainder(BigDecimal.ONE)).intValue(); return DateTimeZone.forOffsetHoursMinutes(offset.intValue(), minuteOffset); } }
@Test public void testFromHL7() { PatientInfo patientInfo = new PatientInfo(); patientInfo.getHl7FieldIterator("PID-7").add("19800102030405-0100^sdf"); DateTime expected = new DateTime(1980, 1, 2, 3, 4, 5, DateTimeZone.forOffsetHoursMinutes(-1, 0)); assertEquals( new Timestamp(expected, Timestamp.Precision.SECOND), patientInfo.getDateOfBirth()); }
@Test public void shouldTransformAllDatesIntoUtc() { assertThat(transformer.from(date(new DateTime(2014, 5, 1, 12, 34, 56, 123).withZoneRetainFields(DateTimeZone.forOffsetHours(10)))), is("2014-05-01T02:34:56.123Z")); assertThat(transformer.from(date(new DateTime(2014, 6, 1, 12, 34, 56, 123).withZoneRetainFields(DateTimeZone.forOffsetHours(-10)))), is("2014-06-01T22:34:56.123Z")); assertThat(transformer.from(date(new DateTime(2014, 6, 1, 12, 34, 56, 123).withZoneRetainFields(DateTimeZone.forOffsetHoursMinutes(10, 30)))), is("2014-06-01T02:04:56.123Z")); assertThat(transformer.from(date(new DateTime(2014, 6, 1, 12, 34, 56, 123).withZoneRetainFields(DateTimeZone.forOffsetHours(0)))), is("2014-06-01T12:34:56.123Z")); assertThat(transformer.from(date(new DateTime(2014, 6, 1, 12, 34, 56).withZoneRetainFields(DateTimeZone.forOffsetHours(8)))), is("2014-06-01T04:34:56.000Z")); assertThat(transformer.from(date(new DateTime(2014, 6, 1, 12, 34, 0).withZoneRetainFields(DateTimeZone.forOffsetHours(8)))), is("2014-06-01T04:34:00.000Z")); assertThat(transformer.from(date(new DateTime(2014, 6, 1, 12, 0, 0).withZoneRetainFields(DateTimeZone.forOffsetHours(8)))), is("2014-06-01T04:00:00.000Z")); }