當前位置: 首頁>>代碼示例>>Java>>正文


Java DateTimeFormatter.withZone方法代碼示例

本文整理匯總了Java中java.time.format.DateTimeFormatter.withZone方法的典型用法代碼示例。如果您正苦於以下問題:Java DateTimeFormatter.withZone方法的具體用法?Java DateTimeFormatter.withZone怎麽用?Java DateTimeFormatter.withZone使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在java.time.format.DateTimeFormatter的用法示例。


在下文中一共展示了DateTimeFormatter.withZone方法的7個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。

示例1: createDateTimeFormatter

import java.time.format.DateTimeFormatter; //導入方法依賴的package包/類
/**
 * 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 (can be {@code null}).
 * @return a new date time formatter
 */
public DateTimeFormatter createDateTimeFormatter(DateTimeFormatter fallbackFormatter) {
	DateTimeFormatter dateTimeFormatter = null;
	if (StringUtils.hasLength(this.pattern)) {
		dateTimeFormatter = DateTimeFormatter.ofPattern(this.pattern);
	}
	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;
			case NONE:
				/* no-op */
				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);
}
 
開發者ID:lamsfoundation,項目名稱:lams,代碼行數:47,代碼來源:DateTimeFormatterFactory.java

示例2: test_signStyle

import java.time.format.DateTimeFormatter; //導入方法依賴的package包/類
@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));
        }
    }
}
 
開發者ID:lambdalab-mirror,項目名稱:jdk8u-jdk,代碼行數:19,代碼來源:TCKSignStyle.java

示例3: test_withZone

import java.time.format.DateTimeFormatter; //導入方法依賴的package包/類
@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);
}
 
開發者ID:lambdalab-mirror,項目名稱:jdk8u-jdk,代碼行數:12,代碼來源:TCKDateTimeFormatter.java

示例4: test_withZone_override

import java.time.format.DateTimeFormatter; //導入方法依賴的package包/類
@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);
}
 
開發者ID:lambdalab-mirror,項目名稱:jdk8u-jdk,代碼行數:10,代碼來源:TCKDateTimeParseResolver.java

示例5: test_withZone_parsedZone_override

import java.time.format.DateTimeFormatter; //導入方法依賴的package包/類
@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);
}
 
開發者ID:lambdalab-mirror,項目名稱:jdk8u-jdk,代碼行數:10,代碼來源:TCKDateTimeParseResolver.java

示例6: test_fieldResolvesToChronoZonedDateTime_overrideZone_matches

import java.time.format.DateTimeFormatter; //導入方法依賴的package包/類
@Test
public void test_fieldResolvesToChronoZonedDateTime_overrideZone_matches() {
    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(EUROPE_PARIS);
    assertEquals(f.parse("1234567890", ZonedDateTime::from), zdt);
}
 
開發者ID:lambdalab-mirror,項目名稱:jdk8u-jdk,代碼行數:8,代碼來源:TCKDateTimeParseResolver.java

示例7: test_fieldResolvesToChronoZonedDateTime_overrideZone_wrongZone

import java.time.format.DateTimeFormatter; //導入方法依賴的package包/類
@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");
}
 
開發者ID:lambdalab-mirror,項目名稱:jdk8u-jdk,代碼行數:8,代碼來源:TCKDateTimeParseResolver.java


注:本文中的java.time.format.DateTimeFormatter.withZone方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。