本文整理匯總了Java中java.time.format.DateTimeFormatterBuilder類的典型用法代碼示例。如果您正苦於以下問題:Java DateTimeFormatterBuilder類的具體用法?Java DateTimeFormatterBuilder怎麽用?Java DateTimeFormatterBuilder使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
DateTimeFormatterBuilder類屬於java.time.format包,在下文中一共展示了DateTimeFormatterBuilder類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: test_resolveFourToDate
import java.time.format.DateTimeFormatterBuilder; //導入依賴的package包/類
@Test(dataProvider="resolveFourToDate")
public void test_resolveFourToDate(TemporalField field1, long value1,
TemporalField field2, long value2,
TemporalField field3, long value3,
TemporalField field4, long value4,
LocalDate expectedDate) {
String str = value1 + " " + value2 + " " + value3 + " " + value4;
DateTimeFormatter f = new DateTimeFormatterBuilder()
.appendValue(field1).appendLiteral(' ')
.appendValue(field2).appendLiteral(' ')
.appendValue(field3).appendLiteral(' ')
.appendValue(field4).toFormatter();
TemporalAccessor accessor = f.parse(str);
assertEquals(accessor.query(TemporalQueries.localDate()), expectedDate);
assertEquals(accessor.query(TemporalQueries.localTime()), null);
}
示例2: testCalculateDateTimeDateTime
import java.time.format.DateTimeFormatterBuilder; //導入依賴的package包/類
public void testCalculateDateTimeDateTime() {
WeightedDateSimilarity sim = new WeightedDateSimilarity(0, 0, 1);
sim.setYearRange(10);
DateTimeFormatter formatter = new DateTimeFormatterBuilder()
.appendPattern("yyyy-MM-dd")
.parseDefaulting(ChronoField.CLOCK_HOUR_OF_DAY, 0)
.parseDefaulting(ChronoField.MINUTE_OF_HOUR, 0)
.parseDefaulting(ChronoField.SECOND_OF_MINUTE, 0)
.toFormatter(Locale.ENGLISH);
LocalDateTime dt1 = LocalDateTime.parse("2015-01-01", formatter);
LocalDateTime dt2 = LocalDateTime.parse("2014-01-01", formatter);
LocalDateTime dt3 = LocalDateTime.parse("2010-01-01", formatter);
LocalDateTime dt4 = LocalDateTime.parse("2005-01-01", formatter);
LocalDateTime dt5 = LocalDateTime.parse("1905-01-01", formatter);
assertEquals(1.0, sim.calculate(dt1, dt1));
assertEquals(0.9, sim.calculate(dt1, dt2));
assertEquals(0.9, sim.calculate(dt2, dt1));
assertEquals(0.5, sim.calculate(dt1, dt3));
assertEquals(0.0, sim.calculate(dt1, dt4));
assertEquals(0.0, sim.calculate(dt1, dt5));
}
示例3: test_parse_resolve_localizedWomDow_lenient
import java.time.format.DateTimeFormatterBuilder; //導入依賴的package包/類
@Test(dataProvider="weekFields")
public void test_parse_resolve_localizedWomDow_lenient(DayOfWeek firstDayOfWeek, int minDays) {
LocalDate date = LocalDate.of(2012, 12, 15);
WeekFields week = WeekFields.of(firstDayOfWeek, minDays);
TemporalField dowField = week.dayOfWeek();
TemporalField womField = week.weekOfMonth();
for (int i = 1; i <= 60; i++) {
DateTimeFormatter f = new DateTimeFormatterBuilder()
.appendValue(YEAR).appendLiteral(':')
.appendValue(MONTH_OF_YEAR).appendLiteral(':')
.appendValue(womField).appendLiteral(':')
.appendValue(dowField).toFormatter().withResolverStyle(LENIENT);
int wom = date.get(womField);
int dow = date.get(dowField);
for (int j = wom - 10; j < wom + 10; j++) {
String str = date.getYear() + ":" + date.getMonthValue() + ":" + j + ":" + dow;
LocalDate parsed = LocalDate.parse(str, f);
assertEquals(parsed, date.plusWeeks(j - wom), " ::" + str + ": :" + i + "::" + j);
}
date = date.plusDays(1);
}
}
示例4: generateDS2
import java.time.format.DateTimeFormatterBuilder; //導入依賴的package包/類
private DataSet<Movie, Attribute> generateDS2() {
DataSet<Movie, Attribute> ds = new HashedDataSet<>();
DateTimeFormatter formatter = new DateTimeFormatterBuilder()
.appendPattern("yyyy-MM-dd")
.parseDefaulting(ChronoField.CLOCK_HOUR_OF_DAY, 0)
.parseDefaulting(ChronoField.MINUTE_OF_HOUR, 0)
.parseDefaulting(ChronoField.SECOND_OF_MINUTE, 0)
.toFormatter(Locale.ENGLISH);
Movie m1 = new Movie("4", "DS2");
m1.setDate(LocalDateTime.parse("1983-10-10", formatter));
ds.add(m1);
Movie m2 = new Movie("5", "DS2");
m2.setDate(LocalDateTime.parse("1984-10-10", formatter));
ds.add(m2);
Movie m3 = new Movie("6", "DS2");
m3.setDate(LocalDateTime.parse("1995-10-10", formatter));
ds.add(m3);
return ds;
}
示例5: test_parse_resolve_localizedWomDow
import java.time.format.DateTimeFormatterBuilder; //導入依賴的package包/類
@Test(dataProvider="weekFields")
public void test_parse_resolve_localizedWomDow(DayOfWeek firstDayOfWeek, int minDays) {
LocalDate date = LocalDate.of(2012, 12, 15);
WeekFields week = WeekFields.of(firstDayOfWeek, minDays);
TemporalField dowField = week.dayOfWeek();
TemporalField womField = week.weekOfMonth();
for (int i = 1; i <= 15; i++) {
DateTimeFormatter f = new DateTimeFormatterBuilder()
.appendValue(YEAR).appendLiteral(':')
.appendValue(MONTH_OF_YEAR).appendLiteral(':')
.appendValue(womField).appendLiteral(':')
.appendValue(dowField).toFormatter();
String str = date.getYear() + ":" + date.getMonthValue() + ":" +
date.get(womField) + ":" + date.get(dowField);
LocalDate parsed = LocalDate.parse(str, f);
assertEquals(parsed, date, " :: " + str + " " + i);
date = date.plusDays(1);
}
}
示例6: test_parse_parseLenientQuarter_SMART
import java.time.format.DateTimeFormatterBuilder; //導入依賴的package包/類
@Test(dataProvider = "parseLenientQuarter")
public void test_parse_parseLenientQuarter_SMART(String str, LocalDate expected, boolean smart) {
DateTimeFormatter f = new DateTimeFormatterBuilder()
.appendValue(YEAR).appendLiteral(':')
.appendValue(IsoFields.QUARTER_OF_YEAR).appendLiteral(':')
.appendValue(IsoFields.DAY_OF_QUARTER)
.toFormatter().withResolverStyle(ResolverStyle.SMART);
if (smart) {
LocalDate parsed = LocalDate.parse(str, f);
assertEquals(parsed, expected);
} else {
try {
LocalDate.parse(str, f);
fail("Should have failed");
} catch (DateTimeParseException ex) {
// expected
}
}
}
示例7: test_signStyle
import java.time.format.DateTimeFormatterBuilder; //導入依賴的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));
}
}
}
示例8: test_resolveClockHourOfDay
import java.time.format.DateTimeFormatterBuilder; //導入依賴的package包/類
@Test(dataProvider="resolveClockHourOfDay")
public void test_resolveClockHourOfDay(ResolverStyle style, long value, Integer expectedHour, int expectedDays) {
String str = Long.toString(value);
DateTimeFormatter f = new DateTimeFormatterBuilder().appendValue(CLOCK_HOUR_OF_DAY).toFormatter();
if (expectedHour != null) {
TemporalAccessor accessor = f.withResolverStyle(style).parse(str);
assertEquals(accessor.query(TemporalQueries.localDate()), null);
assertEquals(accessor.query(TemporalQueries.localTime()), LocalTime.of(expectedHour, 0));
assertEquals(accessor.query(DateTimeFormatter.parsedExcessDays()), Period.ofDays(expectedDays));
} else {
try {
f.withResolverStyle(style).parse(str);
fail();
} catch (DateTimeParseException ex) {
// expected
}
}
}
示例9: test_parse_resolve_localizedWom_lenient
import java.time.format.DateTimeFormatterBuilder; //導入依賴的package包/類
@Test(dataProvider="weekFields")
public void test_parse_resolve_localizedWom_lenient(DayOfWeek firstDayOfWeek, int minDays) {
LocalDate date = LocalDate.of(2012, 12, 15);
WeekFields week = WeekFields.of(firstDayOfWeek, minDays);
TemporalField womField = week.weekOfMonth();
for (int i = 1; i <= 60; i++) {
DateTimeFormatter f = new DateTimeFormatterBuilder()
.appendValue(YEAR).appendLiteral(':')
.appendValue(MONTH_OF_YEAR).appendLiteral(':')
.appendValue(womField).appendLiteral(':')
.appendValue(DAY_OF_WEEK).toFormatter().withResolverStyle(LENIENT);
int wom = date.get(womField);
int dow = date.get(DAY_OF_WEEK);
for (int j = wom - 10; j < wom + 10; j++) {
String str = date.getYear() + ":" + date.getMonthValue() + ":" + j + ":" + dow;
LocalDate parsed = LocalDate.parse(str, f);
assertEquals(parsed, date.plusWeeks(j - wom), " ::" + str + ": :" + i + "::" + j);
}
date = date.plusDays(1);
}
}
示例10: test_parse_resolve_localizedWoy
import java.time.format.DateTimeFormatterBuilder; //導入依賴的package包/類
@Test(dataProvider="weekFields")
public void test_parse_resolve_localizedWoy(DayOfWeek firstDayOfWeek, int minDays) {
LocalDate date = LocalDate.of(2012, 12, 15);
WeekFields week = WeekFields.of(firstDayOfWeek, minDays);
TemporalField woyField = week.weekOfYear();
for (int i = 1; i <= 60; i++) {
DateTimeFormatter f = new DateTimeFormatterBuilder()
.appendValue(YEAR).appendLiteral(':')
.appendValue(woyField).appendLiteral(':')
.appendValue(DAY_OF_WEEK).toFormatter();
String str = date.getYear() + ":" +
date.get(woyField) + ":" + date.get(DAY_OF_WEEK);
LocalDate parsed = LocalDate.parse(str, f);
assertEquals(parsed, date, " :: " + str + " " + i);
date = date.plusDays(1);
}
}
示例11: test_parse_resolve_localizedWoyDow_lenient
import java.time.format.DateTimeFormatterBuilder; //導入依賴的package包/類
@Test(dataProvider="weekFields")
public void test_parse_resolve_localizedWoyDow_lenient(DayOfWeek firstDayOfWeek, int minDays) {
LocalDate date = LocalDate.of(2012, 12, 15);
WeekFields week = WeekFields.of(firstDayOfWeek, minDays);
TemporalField dowField = week.dayOfWeek();
TemporalField woyField = week.weekOfYear();
for (int i = 1; i <= 60; i++) {
DateTimeFormatter f = new DateTimeFormatterBuilder()
.appendValue(YEAR).appendLiteral(':')
.appendValue(woyField).appendLiteral(':')
.appendValue(dowField).toFormatter().withResolverStyle(LENIENT);
int woy = date.get(woyField);
int dow = date.get(dowField);
for (int j = woy - 60; j < woy + 60; j++) {
String str = date.getYear() + ":" + j + ":" + dow;
LocalDate parsed = LocalDate.parse(str, f);
assertEquals(parsed, date.plusWeeks(j - woy), " ::" + str + ": :" + i + "::" + j);
}
date = date.plusDays(1);
}
}
示例12: test_reducedWithChronoYearOfEra
import java.time.format.DateTimeFormatterBuilder; //導入依賴的package包/類
@Test(dataProvider="ReducedWithChrono")
public void test_reducedWithChronoYearOfEra(ChronoLocalDate date) {
Chronology chrono = date.getChronology();
DateTimeFormatter df
= new DateTimeFormatterBuilder().appendValueReduced(YEAR_OF_ERA, 2, 2, LocalDate.of(2000, 1, 1))
.toFormatter()
.withChronology(chrono);
int expected = date.get(YEAR_OF_ERA);
String input = df.format(date);
ParsePosition pos = new ParsePosition(0);
TemporalAccessor parsed = df.parseUnresolved(input, pos);
int actual = parsed.get(YEAR_OF_ERA);
assertEquals(actual, expected,
String.format("Wrong date parsed, chrono: %s, input: %s",
chrono, input));
}
示例13: test_parse_resolve_localizedWoWBYDow
import java.time.format.DateTimeFormatterBuilder; //導入依賴的package包/類
@Test(dataProvider="weekFields")
public void test_parse_resolve_localizedWoWBYDow(DayOfWeek firstDayOfWeek, int minDays) {
LocalDate date = LocalDate.of(2012, 12, 31);
WeekFields week = WeekFields.of(firstDayOfWeek, minDays);
TemporalField dowField = week.dayOfWeek();
TemporalField wowbyField = week.weekOfWeekBasedYear();
TemporalField yowbyField = week.weekBasedYear();
for (int i = 1; i <= 60; i++) {
DateTimeFormatter f = new DateTimeFormatterBuilder()
.appendValue(yowbyField).appendLiteral(':')
.appendValue(wowbyField).appendLiteral(':')
.appendValue(dowField).toFormatter();
String str = date.get(yowbyField) + ":" + date.get(wowbyField) + ":" +
date.get(dowField);
LocalDate parsed = LocalDate.parse(str, f);
assertEquals(parsed, date, " :: " + str + " " + i);
date = date.plusDays(1);
}
}
示例14: test_parse_WeekBasedYear
import java.time.format.DateTimeFormatterBuilder; //導入依賴的package包/類
@Test(dataProvider="LocalWeekBasedYearPatterns")
public void test_parse_WeekBasedYear(String pattern, String text, int pos, int expectedPos, LocalDate expectedValue) {
ParsePosition ppos = new ParsePosition(pos);
DateTimeFormatterBuilder b = new DateTimeFormatterBuilder().appendPattern(pattern);
DateTimeFormatter dtf = b.toFormatter(locale);
TemporalAccessor parsed = dtf.parseUnresolved(text, ppos);
if (ppos.getErrorIndex() != -1) {
assertEquals(ppos.getErrorIndex(), expectedPos);
} else {
WeekFields weekDef = WeekFields.of(locale);
assertEquals(ppos.getIndex(), expectedPos, "Incorrect ending parse position");
assertEquals(parsed.isSupported(weekDef.dayOfWeek()), pattern.indexOf('e') >= 0);
assertEquals(parsed.isSupported(weekDef.weekOfWeekBasedYear()), pattern.indexOf('w') >= 0);
assertEquals(parsed.isSupported(weekDef.weekBasedYear()), pattern.indexOf('Y') >= 0);
// ensure combination resolves into a date
LocalDate result = LocalDate.parse(text, dtf);
assertEquals(result, expectedValue, "LocalDate incorrect for " + pattern + ", weekDef: " + weekDef);
}
}
示例15: test_resolveThreeNoChange
import java.time.format.DateTimeFormatterBuilder; //導入依賴的package包/類
@Test(dataProvider="resolveThreeNoChange")
public void test_resolveThreeNoChange(TemporalField field1, long value1, TemporalField field2, long value2, TemporalField field3, long value3) {
String str = value1 + " " + value2 + " " + value3;
DateTimeFormatter f = new DateTimeFormatterBuilder()
.appendValue(field1).appendLiteral(' ')
.appendValue(field2).appendLiteral(' ')
.appendValue(field3).toFormatter();
TemporalAccessor accessor = f.parse(str);
assertEquals(accessor.query(TemporalQueries.localDate()), null);
assertEquals(accessor.query(TemporalQueries.localTime()), null);
assertEquals(accessor.isSupported(field1), true);
assertEquals(accessor.isSupported(field2), true);
assertEquals(accessor.isSupported(field3), true);
assertEquals(accessor.getLong(field1), value1);
assertEquals(accessor.getLong(field2), value2);
assertEquals(accessor.getLong(field3), value3);
}