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

The following examples show how to use org.joda.time.DateTimeZone#forOffsetHours() . 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: ComposerJsonProcessorTest.java    From nexus-repository-composer with Eclipse Public License 1.0 6 votes vote down vote up
@Test
public void mergeProviderJson() throws Exception {
  DateTime time = new DateTime(1210869000000L, DateTimeZone.forOffsetHours(-4));

  String inputJson1 = readStreamToString(getClass().getResourceAsStream("mergeProviderJson.input1.json"));
  String inputJson2 = readStreamToString(getClass().getResourceAsStream("mergeProviderJson.input2.json"));
  String outputJson = readStreamToString(getClass().getResourceAsStream("mergeProviderJson.output.json"));

  when(repository.getUrl()).thenReturn("http://nexus.repo/base/repo");
  when(payload1.openInputStream()).thenReturn(new ByteArrayInputStream(inputJson1.getBytes(UTF_8)));
  when(payload2.openInputStream()).thenReturn(new ByteArrayInputStream(inputJson2.getBytes(UTF_8)));

  ComposerJsonProcessor underTest = new ComposerJsonProcessor(composerJsonExtractor);
  Payload output = underTest.mergeProviderJson(repository, Arrays.asList(payload1, payload2), time);

  assertEquals(outputJson, readStreamToString(output.openInputStream()), true);
}
 
Example 2
Source File: TestTruncateDateTime.java    From spork with Apache License 2.0 6 votes vote down vote up
@Test
public void testParseDateTime_defaultTimeZonePreserved() throws ExecException {

    // Remember pre-test default time zone.
    DateTimeZone previousDefaultTimeZone = DateTimeZone.getDefault();

    // Overwrite default time zone for this test.
    DateTimeZone testDefaultDateTimeZone = DateTimeZone.forOffsetHours(-8);
    DateTimeZone.setDefault(testDefaultDateTimeZone);

    Tuple t1 = TupleFactory.getInstance().newTuple(1);
    t1.set(0, "2010-04-15T08:11:33.020");

    assertEquals(new DateTime(2010, 4, 15, 8, 11, 33, 20, testDefaultDateTimeZone), ISOHelper.parseDateTime(t1));

    assertTrue(testDefaultDateTimeZone.equals(DateTimeZone.getDefault()));

    // Restore pre-test default time zone.
    DateTimeZone.setDefault(previousDefaultTimeZone);
}
 
Example 3
Source File: TestDateTimeFormatterBuilder.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
public void test_parseWrongOffset() {
    DateTimeFormatterBuilder bld = new DateTimeFormatterBuilder()
        .appendPattern("yyyy-MM-dd HH:mm ").appendTimeZoneOffset("Z", true, 2, 2);
    DateTimeFormatter f = bld.toFormatter();
    
    DateTime expected = new DateTime(2007, 3, 4, 12, 30, 0, DateTimeZone.forOffsetHours(7));
    // parses offset time then adjusts to requested zone
    assertEquals(expected.withZone(TOKYO), f.withZone(TOKYO).parseDateTime("2007-03-04 12:30 +07:00"));
    // parses offset time returning offset zone
    assertEquals(expected, f.withOffsetParsed().parseDateTime("2007-03-04 12:30 +07:00"));
    // parses offset time then converts to default zone
    assertEquals(expected.withZone(DateTimeZone.getDefault()), f.parseDateTime("2007-03-04 12:30 +07:00"));
}
 
Example 4
Source File: TestDateTimeFormatterBuilder.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
public void test_parseWrongOffsetAndZone() {
    DateTimeFormatterBuilder bld = new DateTimeFormatterBuilder()
        .appendPattern("yyyy-MM-dd HH:mm ").appendTimeZoneOffset("Z", true, 2, 2).appendLiteral(' ').appendTimeZoneId();
    DateTimeFormatter f = bld.toFormatter();
    
    DateTime expected = new DateTime(2007, 3, 4, 12, 30, 0, DateTimeZone.forOffsetHours(7));
    // parses offset time then adjusts to parsed zone
    assertEquals(expected.withZone(TOKYO), f.parseDateTime("2007-03-04 12:30 +07:00 Asia/Tokyo"));
    // parses offset time then adjusts to requested zone
    assertEquals(expected.withZone(TOKYO), f.withZone(TOKYO).parseDateTime("2007-03-04 12:30 +07:00 Asia/Tokyo"));
    // parses offset time returning offset zone (ignores zone)
    assertEquals(expected, f.withOffsetParsed().parseDateTime("2007-03-04 12:30 +07:00 Asia/Tokyo"));
}
 
Example 5
Source File: TestDateTimeFormatterBuilder.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
public void test_parseWrongOffset() {
    DateTimeFormatterBuilder bld = new DateTimeFormatterBuilder()
        .appendPattern("yyyy-MM-dd HH:mm ").appendTimeZoneOffset("Z", true, 2, 2);
    DateTimeFormatter f = bld.toFormatter();
    
    DateTime expected = new DateTime(2007, 3, 4, 12, 30, 0, DateTimeZone.forOffsetHours(7));
    // parses offset time then adjusts to requested zone
    assertEquals(expected.withZone(TOKYO), f.withZone(TOKYO).parseDateTime("2007-03-04 12:30 +07:00"));
    // parses offset time returning offset zone
    assertEquals(expected, f.withOffsetParsed().parseDateTime("2007-03-04 12:30 +07:00"));
    // parses offset time then converts to default zone
    assertEquals(expected.withZone(DateTimeZone.getDefault()), f.parseDateTime("2007-03-04 12:30 +07:00"));
}
 
Example 6
Source File: TestDateTimeFormatterBuilder.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
public void test_parseWrongOffsetAndZone() {
    DateTimeFormatterBuilder bld = new DateTimeFormatterBuilder()
        .appendPattern("yyyy-MM-dd HH:mm ").appendTimeZoneOffset("Z", true, 2, 2).appendLiteral(' ').appendTimeZoneId();
    DateTimeFormatter f = bld.toFormatter();
    
    DateTime expected = new DateTime(2007, 3, 4, 12, 30, 0, DateTimeZone.forOffsetHours(7));
    // parses offset time then adjusts to parsed zone
    assertEquals(expected.withZone(TOKYO), f.parseDateTime("2007-03-04 12:30 +07:00 Asia/Tokyo"));
    // parses offset time then adjusts to requested zone
    assertEquals(expected.withZone(TOKYO), f.withZone(TOKYO).parseDateTime("2007-03-04 12:30 +07:00 Asia/Tokyo"));
    // parses offset time returning offset zone (ignores zone)
    assertEquals(expected, f.withOffsetParsed().parseDateTime("2007-03-04 12:30 +07:00 Asia/Tokyo"));
}
 
Example 7
Source File: TestDateTimeZone.java    From joda-time-android with Apache License 2.0 5 votes vote down vote up
@Test
public void testForOffsetHours_int() {
    assertEquals(DateTimeZone.UTC, DateTimeZone.forOffsetHours(0));
    assertEquals(DateTimeZone.forID("+03:00"), DateTimeZone.forOffsetHours(3));
    assertEquals(DateTimeZone.forID("-02:00"), DateTimeZone.forOffsetHours(-2));
    try {
        DateTimeZone.forOffsetHours(999999);
        fail();
    } catch (IllegalArgumentException ex) {}
}
 
Example 8
Source File: DailyDumps.java    From NationStatesPlusPlus with MIT License 5 votes vote down vote up
/**
 * Checks to see if an update to the region daily dump, and if so, downloads the update.
 */
private void updateRegionsDump() {
	InputStream stream = null;
	try {
		HttpURLConnection conn = (HttpURLConnection)(new URL(REGIONS_URL)).openConnection();
		conn.setRequestProperty("User-Agent", userAgent);
		conn.connect();
		final long contentLength = conn.getContentLengthLong();
		final long time = conn.getHeaderFieldDate("Last-Modified", -1);
		final DateTime serverModified = new DateTime(time, DateTimeZone.forOffsetHours(0)); //set to UTC time
		Logger.info("Checking region dump for {}, length: {}, lastModified: {}", serverModified, contentLength, serverModified);
		
		File regionsDump = new File(regionsDir, serverModified.toString(FILE_DATE) + "-regions.xml.gz");
		latestRegionDump.set(regionsDump);
		
		stream = conn.getInputStream();
		if (!regionsDump.exists() || regionsDump.length() != contentLength) {
			Logger.info("Saving regions dump to " + regionsDump.getAbsolutePath());
			try (FileOutputStream fos = new FileOutputStream(regionsDump)) {
				IOUtils.copy(stream, fos);
				Logger.info("Saved regions dump, size: {}", regionsDump.length());
			}
		} else {
			Logger.debug("Regions dump is up to date");
		}
	} catch (IOException e) {
		Logger.error("Unable to process regions dump", e);
	} finally {
		IOUtils.closeQuietly(stream);
	}
}
 
Example 9
Source File: DailyDumps.java    From NationStatesPlusPlus with MIT License 5 votes vote down vote up
/**
 * Checks to see if an update to the nation daily dump, and if so, downloads the update.
 * 
 * Will create a DumpUpdateTask thread after the update is downloaded.
 */
private void updateNationsDump() {
	InputStream stream = null;
	try {
		HttpURLConnection conn = (HttpURLConnection)(new URL(NATIONS_URL)).openConnection();
		conn.setRequestProperty("User-Agent", userAgent);
		conn.connect();
		final long contentLength = conn.getContentLengthLong();
		final long time = conn.getHeaderFieldDate("Last-Modified", -1);
		final DateTime serverModified = new DateTime(time, DateTimeZone.forOffsetHours(0)); //set to UTC
		
		Logger.info("Checking nations dump, length: {}, lastModified: {}", contentLength, serverModified);
		File nationsDump = new File(nationsDir, serverModified.toString(FILE_DATE) + "-nations.xml.gz");
		latestNationDump.set(nationsDump);
		
		stream = conn.getInputStream();
		if (!nationsDump.exists() || nationsDump.length() != contentLength) {
			Logger.info("Saving nations dump to " + nationsDump.getAbsolutePath());
			try (FileOutputStream fos = new FileOutputStream(nationsDump)) {
				IOUtils.copy(stream, fos);
				Logger.info("Saved nations dump successfully, size: {}", nationsDump.length());
			}
			(new Thread(new DumpUpdateTask(access, getMostRecentRegionDump(), nationsDump), "Daily Dump Update Thread")).start();
		} else {
			Logger.debug("Nations dump is up to date");
		}
	} catch (IOException e) {
		Logger.error("Unable to process nations dump", e);
	} finally {
		IOUtils.closeQuietly(stream);
	}
}
 
Example 10
Source File: ISO8601DateUtilTest.java    From scheduling with GNU Affero General Public License v3.0 4 votes vote down vote up
@Test
public void testParseToISO8601() throws ParseException {
    DateTime expectedDateTime = new DateTime(2015, 12, 2, 14, 50, 0, 0, DateTimeZone.forOffsetHours(6));
    assertThat(ISO8601DateUtil.toDate("2015-12-02T14:50:00+06:00").getTime(),
               is(expectedDateTime.toDate().getTime()));
}
 
Example 11
Source File: ISO8601DateUtilTest.java    From scheduling with GNU Affero General Public License v3.0 4 votes vote down vote up
@Test
public void testISO8601ComplaintStringPlus2Hours() throws ParseException {
    DateTime expectedDateTime = new DateTime(2015, 12, 2, 14, 50, 0, 0, DateTimeZone.forOffsetHours(2));
    assertThat(ISO8601DateUtil.toDate("2015-12-02T14:50:00+02:00").getTime(),
               is(expectedDateTime.toDate().getTime()));
}
 
Example 12
Source File: ISO8601DateUtilTest.java    From scheduling with GNU Affero General Public License v3.0 4 votes vote down vote up
@Test
public void testISO8601ComplaintStringMinus2Hours() throws ParseException {
    DateTime expectedDateTime = new DateTime(2015, 12, 2, 14, 50, 0, 0, DateTimeZone.forOffsetHours(-2));
    assertThat(ISO8601DateUtil.toDate("2015-12-02T14:50:00-02:00").getTime(),
               is(expectedDateTime.toDate().getTime()));
}
 
Example 13
Source File: SettableDataSetterFactoryTest.java    From SimpleFlatMapper with MIT License 4 votes vote down vote up
private DateTimeZone getNonDefaultDateTimeZone(Date date) {
    return DateTimeZone.forOffsetHours((int) (TimeUnit.MILLISECONDS.toHours(DateTimeZone.getDefault().getOffset(date.getTime())) - 2));
}
 
Example 14
Source File: TestISOChronology.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
public void testDurationFields() {
    final ISOChronology iso = ISOChronology.getInstance();
    assertEquals("eras", iso.eras().getName());
    assertEquals("centuries", iso.centuries().getName());
    assertEquals("years", iso.years().getName());
    assertEquals("weekyears", iso.weekyears().getName());
    assertEquals("months", iso.months().getName());
    assertEquals("weeks", iso.weeks().getName());
    assertEquals("days", iso.days().getName());
    assertEquals("halfdays", iso.halfdays().getName());
    assertEquals("hours", iso.hours().getName());
    assertEquals("minutes", iso.minutes().getName());
    assertEquals("seconds", iso.seconds().getName());
    assertEquals("millis", iso.millis().getName());
    
    assertEquals(false, iso.eras().isSupported());
    assertEquals(true, iso.centuries().isSupported());
    assertEquals(true, iso.years().isSupported());
    assertEquals(true, iso.weekyears().isSupported());
    assertEquals(true, iso.months().isSupported());
    assertEquals(true, iso.weeks().isSupported());
    assertEquals(true, iso.days().isSupported());
    assertEquals(true, iso.halfdays().isSupported());
    assertEquals(true, iso.hours().isSupported());
    assertEquals(true, iso.minutes().isSupported());
    assertEquals(true, iso.seconds().isSupported());
    assertEquals(true, iso.millis().isSupported());
    
    assertEquals(false, iso.centuries().isPrecise());
    assertEquals(false, iso.years().isPrecise());
    assertEquals(false, iso.weekyears().isPrecise());
    assertEquals(false, iso.months().isPrecise());
    assertEquals(false, iso.weeks().isPrecise());
    assertEquals(false, iso.days().isPrecise());
    assertEquals(false, iso.halfdays().isPrecise());
    assertEquals(true, iso.hours().isPrecise());
    assertEquals(true, iso.minutes().isPrecise());
    assertEquals(true, iso.seconds().isPrecise());
    assertEquals(true, iso.millis().isPrecise());
    
    final ISOChronology isoUTC = ISOChronology.getInstanceUTC();
    assertEquals(false, isoUTC.centuries().isPrecise());
    assertEquals(false, isoUTC.years().isPrecise());
    assertEquals(false, isoUTC.weekyears().isPrecise());
    assertEquals(false, isoUTC.months().isPrecise());
    assertEquals(true, isoUTC.weeks().isPrecise());
    assertEquals(true, isoUTC.days().isPrecise());
    assertEquals(true, isoUTC.halfdays().isPrecise());
    assertEquals(true, isoUTC.hours().isPrecise());
    assertEquals(true, isoUTC.minutes().isPrecise());
    assertEquals(true, isoUTC.seconds().isPrecise());
    assertEquals(true, isoUTC.millis().isPrecise());
    
    final DateTimeZone gmt = DateTimeZone.forID("Etc/GMT");
    final ISOChronology isoGMT = ISOChronology.getInstance(gmt);
    assertEquals(false, isoGMT.centuries().isPrecise());
    assertEquals(false, isoGMT.years().isPrecise());
    assertEquals(false, isoGMT.weekyears().isPrecise());
    assertEquals(false, isoGMT.months().isPrecise());
    assertEquals(true, isoGMT.weeks().isPrecise());
    assertEquals(true, isoGMT.days().isPrecise());
    assertEquals(true, isoGMT.halfdays().isPrecise());
    assertEquals(true, isoGMT.hours().isPrecise());
    assertEquals(true, isoGMT.minutes().isPrecise());
    assertEquals(true, isoGMT.seconds().isPrecise());
    assertEquals(true, isoGMT.millis().isPrecise());
    
    final DateTimeZone offset = DateTimeZone.forOffsetHours(1);
    final ISOChronology isoOffset1 = ISOChronology.getInstance(offset);
    assertEquals(false, isoOffset1.centuries().isPrecise());
    assertEquals(false, isoOffset1.years().isPrecise());
    assertEquals(false, isoOffset1.weekyears().isPrecise());
    assertEquals(false, isoOffset1.months().isPrecise());
    assertEquals(true, isoOffset1.weeks().isPrecise());
    assertEquals(true, isoOffset1.days().isPrecise());
    assertEquals(true, isoOffset1.halfdays().isPrecise());
    assertEquals(true, isoOffset1.hours().isPrecise());
    assertEquals(true, isoOffset1.minutes().isPrecise());
    assertEquals(true, isoOffset1.seconds().isPrecise());
    assertEquals(true, isoOffset1.millis().isPrecise());
}
 
Example 15
Source File: TestISOChronology.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
public void testDurationFields() {
    assertEquals("eras", ISOChronology.getInstance().eras().getName());
    assertEquals("centuries", ISOChronology.getInstance().centuries().getName());
    assertEquals("years", ISOChronology.getInstance().years().getName());
    assertEquals("weekyears", ISOChronology.getInstance().weekyears().getName());
    assertEquals("months", ISOChronology.getInstance().months().getName());
    assertEquals("weeks", ISOChronology.getInstance().weeks().getName());
    assertEquals("days", ISOChronology.getInstance().days().getName());
    assertEquals("halfdays", ISOChronology.getInstance().halfdays().getName());
    assertEquals("hours", ISOChronology.getInstance().hours().getName());
    assertEquals("minutes", ISOChronology.getInstance().minutes().getName());
    assertEquals("seconds", ISOChronology.getInstance().seconds().getName());
    assertEquals("millis", ISOChronology.getInstance().millis().getName());
    
    assertEquals(false, ISOChronology.getInstance().eras().isSupported());
    assertEquals(true, ISOChronology.getInstance().centuries().isSupported());
    assertEquals(true, ISOChronology.getInstance().years().isSupported());
    assertEquals(true, ISOChronology.getInstance().weekyears().isSupported());
    assertEquals(true, ISOChronology.getInstance().months().isSupported());
    assertEquals(true, ISOChronology.getInstance().weeks().isSupported());
    assertEquals(true, ISOChronology.getInstance().days().isSupported());
    assertEquals(true, ISOChronology.getInstance().halfdays().isSupported());
    assertEquals(true, ISOChronology.getInstance().hours().isSupported());
    assertEquals(true, ISOChronology.getInstance().minutes().isSupported());
    assertEquals(true, ISOChronology.getInstance().seconds().isSupported());
    assertEquals(true, ISOChronology.getInstance().millis().isSupported());
    
    assertEquals(false, ISOChronology.getInstance().centuries().isPrecise());
    assertEquals(false, ISOChronology.getInstance().years().isPrecise());
    assertEquals(false, ISOChronology.getInstance().weekyears().isPrecise());
    assertEquals(false, ISOChronology.getInstance().months().isPrecise());
    assertEquals(false, ISOChronology.getInstance().weeks().isPrecise());
    assertEquals(false, ISOChronology.getInstance().days().isPrecise());
    assertEquals(false, ISOChronology.getInstance().halfdays().isPrecise());
    assertEquals(true, ISOChronology.getInstance().hours().isPrecise());
    assertEquals(true, ISOChronology.getInstance().minutes().isPrecise());
    assertEquals(true, ISOChronology.getInstance().seconds().isPrecise());
    assertEquals(true, ISOChronology.getInstance().millis().isPrecise());
    
    assertEquals(false, ISOChronology.getInstanceUTC().centuries().isPrecise());
    assertEquals(false, ISOChronology.getInstanceUTC().years().isPrecise());
    assertEquals(false, ISOChronology.getInstanceUTC().weekyears().isPrecise());
    assertEquals(false, ISOChronology.getInstanceUTC().months().isPrecise());
    assertEquals(true, ISOChronology.getInstanceUTC().weeks().isPrecise());
    assertEquals(true, ISOChronology.getInstanceUTC().days().isPrecise());
    assertEquals(true, ISOChronology.getInstanceUTC().halfdays().isPrecise());
    assertEquals(true, ISOChronology.getInstanceUTC().hours().isPrecise());
    assertEquals(true, ISOChronology.getInstanceUTC().minutes().isPrecise());
    assertEquals(true, ISOChronology.getInstanceUTC().seconds().isPrecise());
    assertEquals(true, ISOChronology.getInstanceUTC().millis().isPrecise());
    
    DateTimeZone gmt = DateTimeZone.forID("Etc/GMT");
    assertEquals(false, ISOChronology.getInstance(gmt).centuries().isPrecise());
    assertEquals(false, ISOChronology.getInstance(gmt).years().isPrecise());
    assertEquals(false, ISOChronology.getInstance(gmt).weekyears().isPrecise());
    assertEquals(false, ISOChronology.getInstance(gmt).months().isPrecise());
    assertEquals(true, ISOChronology.getInstance(gmt).weeks().isPrecise());
    assertEquals(true, ISOChronology.getInstance(gmt).days().isPrecise());
    assertEquals(true, ISOChronology.getInstance(gmt).halfdays().isPrecise());
    assertEquals(true, ISOChronology.getInstance(gmt).hours().isPrecise());
    assertEquals(true, ISOChronology.getInstance(gmt).minutes().isPrecise());
    assertEquals(true, ISOChronology.getInstance(gmt).seconds().isPrecise());
    assertEquals(true, ISOChronology.getInstance(gmt).millis().isPrecise());
    
    DateTimeZone offset = DateTimeZone.forOffsetHours(1);
    assertEquals(false, ISOChronology.getInstance(offset).centuries().isPrecise());
    assertEquals(false, ISOChronology.getInstance(offset).years().isPrecise());
    assertEquals(false, ISOChronology.getInstance(offset).weekyears().isPrecise());
    assertEquals(false, ISOChronology.getInstance(offset).months().isPrecise());
    assertEquals(true, ISOChronology.getInstance(offset).weeks().isPrecise());
    assertEquals(true, ISOChronology.getInstance(offset).days().isPrecise());
    assertEquals(true, ISOChronology.getInstance(offset).halfdays().isPrecise());
    assertEquals(true, ISOChronology.getInstance(offset).hours().isPrecise());
    assertEquals(true, ISOChronology.getInstance(offset).minutes().isPrecise());
    assertEquals(true, ISOChronology.getInstance(offset).seconds().isPrecise());
    assertEquals(true, ISOChronology.getInstance(offset).millis().isPrecise());
}
 
Example 16
Source File: SnapshotTest.java    From btrbck with GNU General Public License v3.0 4 votes vote down vote up
@Before
public void setup() {
    date = new DateTime(456497456L, DateTimeZone.forOffsetHours(5));

}