Java Code Examples for java.time.format.DateTimeFormatter#withZone()

The following examples show how to use java.time.format.DateTimeFormatter#withZone() . 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: TCKSignStyle.java    From hottub with GNU General Public License v2.0 6 votes vote down vote up
@Test(dataProvider = "signStyle")
public void test_signStyle(LocalDate localDate, SignStyle style, Class<?> expectedEx, String expectedStr) {
    DateTimeFormatterBuilder builder = new DateTimeFormatterBuilder();
    DateTimeFormatter formatter = builder.appendValue(ChronoField.YEAR, 2, 4, style)
                                         .toFormatter();
    formatter = formatter.withZone(ZoneOffset.UTC);
    if (expectedEx == null) {
        String output = formatter.format(localDate);
        assertEquals(output, expectedStr);
    } else {
        try {
            formatter.format(localDate);
            fail();
        } catch (Exception ex) {
            assertTrue(expectedEx.isInstance(ex));
        }
    }
}
 
Example 2
Source File: TimestampFormatTest.java    From diirt with MIT License 6 votes vote down vote up
@Test
public void spacedFormat() {
    DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.NNNNNN");
    formatter.withZone(TimeZone.getTimeZone("GMT").toZoneId());
    Instant time = Instant.ofEpochSecond(0, 1);
    assertThat(LocalDateTime.ofInstant(time, TimeZone.getTimeZone("GMT").toZoneId()).format(formatter), equalTo("1970-01-01T00:00:00.000001"));
    time = Instant.ofEpochSecond(0, 12);
    assertThat(LocalDateTime.ofInstant(time, TimeZone.getTimeZone("GMT").toZoneId()).format(formatter), equalTo("1970-01-01T00:00:00.000012"));
    time = Instant.ofEpochSecond(0, 123);
    assertThat(LocalDateTime.ofInstant(time, TimeZone.getTimeZone("GMT").toZoneId()).format(formatter), equalTo("1970-01-01T00:00:00.000123"));
    time = Instant.ofEpochSecond(0, 1234);
    assertThat(LocalDateTime.ofInstant(time, TimeZone.getTimeZone("GMT").toZoneId()).format(formatter), equalTo("1970-01-01T00:00:00.001234"));
    time = Instant.ofEpochSecond(0, 12345);
    assertThat(LocalDateTime.ofInstant(time, TimeZone.getTimeZone("GMT").toZoneId()).format(formatter), equalTo("1970-01-01T00:00:00.012345"));
    time = Instant.ofEpochSecond(0, 123456);
    assertThat(LocalDateTime.ofInstant(time, TimeZone.getTimeZone("GMT").toZoneId()).format(formatter), equalTo("1970-01-01T00:00:00.123456"));
    // time = Instant.ofEpochSecond(0, 1234567);
    // assertThat(LocalDateTime.ofInstant(time, TimeZone.getTimeZone("GMT").toZoneId()).format(formatter),
    // equalTo("1970-01-01T00:00:00.1234567"));
    // time = Instant.ofEpochSecond(0, 12345678);
    // assertThat(LocalDateTime.ofInstant(time, TimeZone.getTimeZone("GMT").toZoneId()).format(formatter),
    // equalTo("1970-01-01T00:00:00.12345678"));
}
 
Example 3
Source File: TCKSignStyle.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
@Test(dataProvider = "signStyle")
public void test_signStyle(LocalDate localDate, SignStyle style, Class<?> expectedEx, String expectedStr) {
    DateTimeFormatterBuilder builder = new DateTimeFormatterBuilder();
    DateTimeFormatter formatter = builder.appendValue(ChronoField.YEAR, 2, 4, style)
                                         .toFormatter();
    formatter = formatter.withZone(ZoneOffset.UTC);
    if (expectedEx == null) {
        String output = formatter.format(localDate);
        assertEquals(output, expectedStr);
    } else {
        try {
            formatter.format(localDate);
            fail();
        } catch (Exception ex) {
            assertTrue(expectedEx.isInstance(ex));
        }
    }
}
 
Example 4
Source File: TCKSignStyle.java    From j2objc with Apache License 2.0 6 votes vote down vote up
@Test
@UseDataProvider("data_signStyle")
public void test_signStyle(LocalDate localDate, SignStyle style, Class<?> expectedEx, String expectedStr) {
    DateTimeFormatterBuilder builder = new DateTimeFormatterBuilder();
    DateTimeFormatter formatter = builder.appendValue(ChronoField.YEAR, 2, 4, style)
                                         .toFormatter();
    formatter = formatter.withZone(ZoneOffset.UTC);
    if (expectedEx == null) {
        String output = formatter.format(localDate);
        assertEquals(output, expectedStr);
    } else {
        try {
            formatter.format(localDate);
            fail();
        } catch (Exception ex) {
            assertTrue(expectedEx.isInstance(ex));
        }
    }
}
 
Example 5
Source File: TCKSignStyle.java    From jdk8u_jdk with GNU General Public License v2.0 6 votes vote down vote up
@Test(dataProvider = "signStyle")
public void test_signStyle(LocalDate localDate, SignStyle style, Class<?> expectedEx, String expectedStr) {
    DateTimeFormatterBuilder builder = new DateTimeFormatterBuilder();
    DateTimeFormatter formatter = builder.appendValue(ChronoField.YEAR, 2, 4, style)
                                         .toFormatter();
    formatter = formatter.withZone(ZoneOffset.UTC);
    if (expectedEx == null) {
        String output = formatter.format(localDate);
        assertEquals(output, expectedStr);
    } else {
        try {
            formatter.format(localDate);
            fail();
        } catch (Exception ex) {
            assertTrue(expectedEx.isInstance(ex));
        }
    }
}
 
Example 6
Source File: TCKDateTimeFormatter.java    From openjdk-8-source with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void test_withZone() {
    DateTimeFormatter test = fmt;
    assertEquals(test.getZone(), null);
    test = test.withZone(ZoneId.of("Europe/Paris"));
    assertEquals(test.getZone(), ZoneId.of("Europe/Paris"));
    test = test.withZone(ZoneOffset.UTC);
    assertEquals(test.getZone(), ZoneOffset.UTC);
    test = test.withZone(null);
    assertEquals(test.getZone(), null);
}
 
Example 7
Source File: TCKDateTimeParseResolver.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void test_withZone_override() {
    DateTimeFormatter f = new DateTimeFormatterBuilder().parseDefaulting(EPOCH_DAY, 2).toFormatter();
    f = f.withZone(EUROPE_ATHENS);
    TemporalAccessor accessor = f.parse("");
    assertEquals(accessor.query(TemporalQueries.localDate()), LocalDate.of(1970, 1, 3));
    assertEquals(accessor.query(TemporalQueries.localTime()), null);
    assertEquals(accessor.query(TemporalQueries.zoneId()), EUROPE_ATHENS);
}
 
Example 8
Source File: TCKDateTimeParseResolver.java    From j2objc with Apache License 2.0 5 votes vote down vote up
@Test
public void test_withZone_parsedZone_override() {
    DateTimeFormatter f = new DateTimeFormatterBuilder().parseDefaulting(EPOCH_DAY, 2).appendZoneId().toFormatter();
    f = f.withZone(EUROPE_ATHENS);
    TemporalAccessor accessor = f.parse("Europe/Paris");
    assertEquals(accessor.query(TemporalQueries.localDate()), LocalDate.of(1970, 1, 3));
    assertEquals(accessor.query(TemporalQueries.localTime()), null);
    assertEquals(accessor.query(TemporalQueries.zoneId()), EUROPE_PARIS);
}
 
Example 9
Source File: DateTimeFormatterFactory.java    From java-technology-stack with MIT License 5 votes vote down vote up
/**
 * Create a new {@code DateTimeFormatter} using this factory.
 * <p>If no specific pattern or style has been defined,
 * the supplied {@code fallbackFormatter} will be used.
 * @param fallbackFormatter the fall-back formatter to use
 * when no specific factory properties have been set
 * @return a new date time formatter
 */
public DateTimeFormatter createDateTimeFormatter(DateTimeFormatter fallbackFormatter) {
	DateTimeFormatter dateTimeFormatter = null;
	if (StringUtils.hasLength(this.pattern)) {
		// Using strict parsing to align with Joda-Time and standard DateFormat behavior:
		// otherwise, an overflow like e.g. Feb 29 for a non-leap-year wouldn't get rejected.
		// However, with strict parsing, a year digit needs to be specified as 'u'...
		String patternToUse = StringUtils.replace(this.pattern, "yy", "uu");
		dateTimeFormatter = DateTimeFormatter.ofPattern(patternToUse).withResolverStyle(ResolverStyle.STRICT);
	}
	else if (this.iso != null && this.iso != ISO.NONE) {
		switch (this.iso) {
			case DATE:
				dateTimeFormatter = DateTimeFormatter.ISO_DATE;
				break;
			case TIME:
				dateTimeFormatter = DateTimeFormatter.ISO_TIME;
				break;
			case DATE_TIME:
				dateTimeFormatter = DateTimeFormatter.ISO_DATE_TIME;
				break;
			default:
				throw new IllegalStateException("Unsupported ISO format: " + this.iso);
		}
	}
	else if (this.dateStyle != null && this.timeStyle != null) {
		dateTimeFormatter = DateTimeFormatter.ofLocalizedDateTime(this.dateStyle, this.timeStyle);
	}
	else if (this.dateStyle != null) {
		dateTimeFormatter = DateTimeFormatter.ofLocalizedDate(this.dateStyle);
	}
	else if (this.timeStyle != null) {
		dateTimeFormatter = DateTimeFormatter.ofLocalizedTime(this.timeStyle);
	}

	if (dateTimeFormatter != null && this.timeZone != null) {
		dateTimeFormatter = dateTimeFormatter.withZone(this.timeZone.toZoneId());
	}
	return (dateTimeFormatter != null ? dateTimeFormatter : fallbackFormatter);
}
 
Example 10
Source File: TCKDateTimeParseResolver.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
@Test(expectedExceptions = DateTimeParseException.class)
public void test_fieldResolvesToChronoZonedDateTime_overrideZone_wrongZone() {
    ZonedDateTime zdt = ZonedDateTime.of(2010, 6, 30, 12, 30, 0, 0, EUROPE_PARIS);
    DateTimeFormatter f = new DateTimeFormatterBuilder().appendValue(new ResolvingField(zdt)).toFormatter();
    f = f.withZone(ZoneId.of("Europe/London"));
    f.parse("1234567890");
}
 
Example 11
Source File: TCKDateTimeFormatter.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void test_withZone() {
    DateTimeFormatter test = fmt;
    assertEquals(test.getZone(), null);
    test = test.withZone(ZoneId.of("Europe/Paris"));
    assertEquals(test.getZone(), ZoneId.of("Europe/Paris"));
    test = test.withZone(ZoneOffset.UTC);
    assertEquals(test.getZone(), ZoneOffset.UTC);
    test = test.withZone(null);
    assertEquals(test.getZone(), null);
}
 
Example 12
Source File: TCKDateTimeParseResolver.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
@Test(expectedExceptions = DateTimeParseException.class)
public void test_fieldResolvesToChronoZonedDateTime_overrideZone_wrongZone() {
    ZonedDateTime zdt = ZonedDateTime.of(2010, 6, 30, 12, 30, 0, 0, EUROPE_PARIS);
    DateTimeFormatter f = new DateTimeFormatterBuilder().appendValue(new ResolvingField(zdt)).toFormatter();
    f = f.withZone(ZoneId.of("Europe/London"));
    f.parse("1234567890");
}
 
Example 13
Source File: TCKDateTimeFormatter.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void test_withZone() {
    DateTimeFormatter test = fmt;
    assertEquals(test.getZone(), null);
    test = test.withZone(ZoneId.of("Europe/Paris"));
    assertEquals(test.getZone(), ZoneId.of("Europe/Paris"));
    test = test.withZone(ZoneOffset.UTC);
    assertEquals(test.getZone(), ZoneOffset.UTC);
    test = test.withZone(null);
    assertEquals(test.getZone(), null);
}
 
Example 14
Source File: TCKDateTimeParseResolver.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void test_withZone_parsedZone_override() {
    DateTimeFormatter f = new DateTimeFormatterBuilder().parseDefaulting(EPOCH_DAY, 2).appendZoneId().toFormatter();
    f = f.withZone(EUROPE_ATHENS);
    TemporalAccessor accessor = f.parse("Europe/Paris");
    assertEquals(accessor.query(TemporalQueries.localDate()), LocalDate.of(1970, 1, 3));
    assertEquals(accessor.query(TemporalQueries.localTime()), null);
    assertEquals(accessor.query(TemporalQueries.zoneId()), EUROPE_PARIS);
}
 
Example 15
Source File: TCKDateTimeParseResolver.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void test_withZone_parsedZone_override() {
    DateTimeFormatter f = new DateTimeFormatterBuilder().parseDefaulting(EPOCH_DAY, 2).appendZoneId().toFormatter();
    f = f.withZone(EUROPE_ATHENS);
    TemporalAccessor accessor = f.parse("Europe/Paris");
    assertEquals(accessor.query(TemporalQueries.localDate()), LocalDate.of(1970, 1, 3));
    assertEquals(accessor.query(TemporalQueries.localTime()), null);
    assertEquals(accessor.query(TemporalQueries.zoneId()), EUROPE_PARIS);
}
 
Example 16
Source File: TCKDateTimeParseResolver.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void test_withZone_override() {
    DateTimeFormatter f = new DateTimeFormatterBuilder().parseDefaulting(EPOCH_DAY, 2).toFormatter();
    f = f.withZone(EUROPE_ATHENS);
    TemporalAccessor accessor = f.parse("");
    assertEquals(accessor.query(TemporalQueries.localDate()), LocalDate.of(1970, 1, 3));
    assertEquals(accessor.query(TemporalQueries.localTime()), null);
    assertEquals(accessor.query(TemporalQueries.zoneId()), EUROPE_ATHENS);
}
 
Example 17
Source File: ComAdminImpl.java    From openemm with GNU Affero General Public License v3.0 5 votes vote down vote up
@Override
public DateTimeFormatter getDateTimeFormatter() {
	String dateTimeFormatPattern = DateTimeFormatterBuilder.getLocalizedDateTimePattern(FormatStyle.SHORT, FormatStyle.SHORT, IsoChronology.INSTANCE, getLocale());
	dateTimeFormatPattern = dateTimeFormatPattern.replaceFirst("y+", "yyyy").replaceFirst(", ", " ");
	DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern(dateTimeFormatPattern, getLocale());
	dateTimeFormatter.withZone(TimeZone.getTimeZone(getAdminTimezone()).toZoneId());
	dateTimeFormatter.withResolverStyle(ResolverStyle.STRICT);
	return dateTimeFormatter;
}
 
Example 18
Source File: ComAdminImpl.java    From openemm with GNU Affero General Public License v3.0 5 votes vote down vote up
@Override
public DateTimeFormatter getDateTimeFormatterWithSeconds() {
	String dateFormatPattern = DateTimeFormatterBuilder.getLocalizedDateTimePattern(FormatStyle.SHORT, FormatStyle.MEDIUM, IsoChronology.INSTANCE, getLocale());
	dateFormatPattern = dateFormatPattern.replaceFirst("y+", "yyyy").replaceFirst(", ", " ");
	DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern(dateFormatPattern, getLocale());
	dateTimeFormatter.withZone(TimeZone.getTimeZone(getAdminTimezone()).toZoneId());
	dateTimeFormatter.withResolverStyle(ResolverStyle.STRICT);
	return dateTimeFormatter;
}
 
Example 19
Source File: DateTimeFormatterFactory.java    From spring-analysis-note with MIT License 5 votes vote down vote up
/**
 * Create a new {@code DateTimeFormatter} using this factory.
 * <p>If no specific pattern or style has been defined,
 * the supplied {@code fallbackFormatter} will be used.
 * @param fallbackFormatter the fall-back formatter to use
 * when no specific factory properties have been set
 * @return a new date time formatter
 */
public DateTimeFormatter createDateTimeFormatter(DateTimeFormatter fallbackFormatter) {
	DateTimeFormatter dateTimeFormatter = null;
	if (StringUtils.hasLength(this.pattern)) {
		// Using strict parsing to align with Joda-Time and standard DateFormat behavior:
		// otherwise, an overflow like e.g. Feb 29 for a non-leap-year wouldn't get rejected.
		// However, with strict parsing, a year digit needs to be specified as 'u'...
		String patternToUse = StringUtils.replace(this.pattern, "yy", "uu");
		dateTimeFormatter = DateTimeFormatter.ofPattern(patternToUse).withResolverStyle(ResolverStyle.STRICT);
	}
	else if (this.iso != null && this.iso != ISO.NONE) {
		switch (this.iso) {
			case DATE:
				dateTimeFormatter = DateTimeFormatter.ISO_DATE;
				break;
			case TIME:
				dateTimeFormatter = DateTimeFormatter.ISO_TIME;
				break;
			case DATE_TIME:
				dateTimeFormatter = DateTimeFormatter.ISO_DATE_TIME;
				break;
			default:
				throw new IllegalStateException("Unsupported ISO format: " + this.iso);
		}
	}
	else if (this.dateStyle != null && this.timeStyle != null) {
		dateTimeFormatter = DateTimeFormatter.ofLocalizedDateTime(this.dateStyle, this.timeStyle);
	}
	else if (this.dateStyle != null) {
		dateTimeFormatter = DateTimeFormatter.ofLocalizedDate(this.dateStyle);
	}
	else if (this.timeStyle != null) {
		dateTimeFormatter = DateTimeFormatter.ofLocalizedTime(this.timeStyle);
	}

	if (dateTimeFormatter != null && this.timeZone != null) {
		dateTimeFormatter = dateTimeFormatter.withZone(this.timeZone.toZoneId());
	}
	return (dateTimeFormatter != null ? dateTimeFormatter : fallbackFormatter);
}
 
Example 20
Source File: TimestampFormatTest.java    From diirt with MIT License 4 votes vote down vote up
@Test(expected = DateTimeParseException.class)
public void parse2() throws Exception {
    DateTimeFormatter format = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss");
    format.withZone(TimeZone.getTimeZone("GMT").toZoneId());
    Instant time = LocalDateTime.parse("1976-NN-01T00:00:00", format).toInstant(ZoneOffset.of("Z"));
}