本文整理汇总了Java中java.time.format.DateTimeFormatter.parse方法的典型用法代码示例。如果您正苦于以下问题:Java DateTimeFormatter.parse方法的具体用法?Java DateTimeFormatter.parse怎么用?Java DateTimeFormatter.parse使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类java.time.format.DateTimeFormatter
的用法示例。
在下文中一共展示了DateTimeFormatter.parse方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: test_resolveFourToDate
import java.time.format.DateTimeFormatter; //导入方法依赖的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: test_parse_fromField_InstantSeconds
import java.time.format.DateTimeFormatter; //导入方法依赖的package包/类
@Test
public void test_parse_fromField_InstantSeconds() {
DateTimeFormatter fmt = new DateTimeFormatterBuilder()
.appendValue(INSTANT_SECONDS).toFormatter();
TemporalAccessor acc = fmt.parse("86402");
Instant expected = Instant.ofEpochSecond(86402);
assertEquals(acc.isSupported(INSTANT_SECONDS), true);
assertEquals(acc.isSupported(NANO_OF_SECOND), true);
assertEquals(acc.isSupported(MICRO_OF_SECOND), true);
assertEquals(acc.isSupported(MILLI_OF_SECOND), true);
assertEquals(acc.getLong(INSTANT_SECONDS), 86402L);
assertEquals(acc.getLong(NANO_OF_SECOND), 0L);
assertEquals(acc.getLong(MICRO_OF_SECOND), 0L);
assertEquals(acc.getLong(MILLI_OF_SECOND), 0L);
assertEquals(Instant.from(acc), expected);
}
示例3: test_parsed_toString_resolvedDateTime
import java.time.format.DateTimeFormatter; //导入方法依赖的package包/类
@Test
public void test_parsed_toString_resolvedDateTime() {
DateTimeFormatter f = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
TemporalAccessor temporal = f.parse("2010-06-30 11:30:56");
String msg = temporal.toString();
assertTrue(msg.contains("2010-06-30"), msg);
assertTrue(msg.contains("11:30:56"), msg);
}
示例4: test_resolveOneToDate
import java.time.format.DateTimeFormatter; //导入方法依赖的package包/类
@Test(dataProvider="resolveOneToDate")
public void test_resolveOneToDate(TemporalField field1, long value1, LocalDate expectedDate) {
String str = Long.toString(value1);
DateTimeFormatter f = new DateTimeFormatterBuilder().appendValue(field1).toFormatter();
TemporalAccessor accessor = f.parse(str);
assertEquals(accessor.query(TemporalQueries.localDate()), expectedDate);
assertEquals(accessor.query(TemporalQueries.localTime()), null);
}
示例5: test_parseLocalizedText
import java.time.format.DateTimeFormatter; //导入方法依赖的package包/类
@Test(dataProvider="format_data")
public void test_parseLocalizedText(Chronology chrono, Locale formatLocale, Locale numberingLocale,
ChronoLocalDate expected, String text) {
DateTimeFormatter dtf = DateTimeFormatter.ofLocalizedDate(FormatStyle.FULL)
.withChronology(chrono).withLocale(formatLocale)
.withDecimalStyle(DecimalStyle.of(numberingLocale));
TemporalAccessor temporal = dtf.parse(text);
ChronoLocalDate date = chrono.date(temporal);
assertEquals(date, expected);
}
示例6: test_parse_CharSequence_ParsePosition
import java.time.format.DateTimeFormatter; //导入方法依赖的package包/类
@Test
public void test_parse_CharSequence_ParsePosition() {
DateTimeFormatter test = fmt.withLocale(Locale.ENGLISH).withDecimalStyle(DecimalStyle.STANDARD);
ParsePosition pos = new ParsePosition(3);
TemporalAccessor result = test.parse("XXXONE30XXX", pos);
assertEquals(pos.getIndex(), 8);
assertEquals(pos.getErrorIndex(), -1);
assertEquals(result.isSupported(DAY_OF_MONTH), true);
assertEquals(result.getLong(DAY_OF_MONTH), 30L);
assertEquals(result.isSupported(HOUR_OF_DAY), false);
}
示例7: test_parse_CharSequence_ParsePosition_parseError
import java.time.format.DateTimeFormatter; //导入方法依赖的package包/类
@Test(expectedExceptions=DateTimeParseException.class)
public void test_parse_CharSequence_ParsePosition_parseError() {
DateTimeFormatter test = DateTimeFormatter.ISO_DATE;
ParsePosition pos = new ParsePosition(3);
try {
test.parse("XXX2012XXX", pos);
fail();
} catch (DateTimeParseException ex) {
assertEquals(ex.getErrorIndex(), 7);
throw ex;
}
}
示例8: test_resolveTwoToDate
import java.time.format.DateTimeFormatter; //导入方法依赖的package包/类
@Test(dataProvider="resolveTwoToDate")
public void test_resolveTwoToDate(TemporalField field1, long value1,
TemporalField field2, long value2,
LocalDate expectedDate) {
String str = value1 + " " + value2;
DateTimeFormatter f = new DateTimeFormatterBuilder()
.appendValue(field1).appendLiteral(' ')
.appendValue(field2).toFormatter();
TemporalAccessor accessor = f.parse(str);
assertEquals(accessor.query(TemporalQueries.localDate()), expectedDate);
assertEquals(accessor.query(TemporalQueries.localTime()), null);
}
示例9: test_resolverFields_selectOneDateResolveYMD
import java.time.format.DateTimeFormatter; //导入方法依赖的package包/类
@Test
public void test_resolverFields_selectOneDateResolveYMD() throws Exception {
DateTimeFormatter base = new DateTimeFormatterBuilder()
.appendValue(YEAR).appendLiteral('-').appendValue(MONTH_OF_YEAR).appendLiteral('-')
.appendValue(DAY_OF_MONTH).appendLiteral('-').appendValue(DAY_OF_YEAR).toFormatter();
DateTimeFormatter f = base.withResolverFields(YEAR, MONTH_OF_YEAR, DAY_OF_MONTH);
try {
base.parse("2012-6-30-321", LocalDate::from); // wrong day-of-year
fail();
} catch (DateTimeException ex) {
// expected, fails as it produces two different dates
}
LocalDate parsed = f.parse("2012-6-30-321", LocalDate::from); // ignored day-of-year
assertEquals(parsed, LocalDate.of(2012, 6, 30));
}
示例10: test_fieldResolvesToChronoLocalDateTime_overrideChrono_matches
import java.time.format.DateTimeFormatter; //导入方法依赖的package包/类
@Test
public void test_fieldResolvesToChronoLocalDateTime_overrideChrono_matches() {
MinguoDate mdt = MinguoDate.of(100, 6, 30);
DateTimeFormatter f = new DateTimeFormatterBuilder().appendValue(new ResolvingField(mdt.atTime(LocalTime.NOON))).toFormatter();
f = f.withChronology(MinguoChronology.INSTANCE);
TemporalAccessor accessor = f.parse("1234567890");
assertEquals(accessor.query(TemporalQueries.localDate()), LocalDate.from(mdt));
assertEquals(accessor.query(TemporalQueries.localTime()), LocalTime.NOON);
assertEquals(accessor.query(TemporalQueries.chronology()), MinguoChronology.INSTANCE);
}
示例11: test_parse_instantZones_ZDT
import java.time.format.DateTimeFormatter; //导入方法依赖的package包/类
@Test(dataProvider = "instantZones")
public void test_parse_instantZones_ZDT(DateTimeFormatter formatter, String text, ZonedDateTime expected) {
TemporalAccessor actual = formatter.parse(text);
assertEquals(ZonedDateTime.from(actual), expected);
}
示例12: test_parse_instantNoZone_ZDT
import java.time.format.DateTimeFormatter; //导入方法依赖的package包/类
@Test(dataProvider = "instantNoZone", expectedExceptions = DateTimeException.class)
public void test_parse_instantNoZone_ZDT(DateTimeFormatter formatter, String text, Instant expected) {
TemporalAccessor actual = formatter.parse(text);
ZonedDateTime.from(actual);
}
示例13: test_parse_instantZones_LDT
import java.time.format.DateTimeFormatter; //导入方法依赖的package包/类
@Test(dataProvider = "instantZones")
public void test_parse_instantZones_LDT(DateTimeFormatter formatter, String text, ZonedDateTime expected) {
TemporalAccessor actual = formatter.parse(text);
assertEquals(LocalDateTime.from(actual), expected.toLocalDateTime());
}
示例14: parse
import java.time.format.DateTimeFormatter; //导入方法依赖的package包/类
/**
* Obtains an instance of {@code OffsetDateTime} from a text string using a specific formatter.
* <p>
* The text is parsed using the formatter, returning a date-time.
*
* @param text the text to parse, not null
* @param formatter the formatter to use, not null
* @return the parsed offset date-time, not null
* @throws DateTimeParseException if the text cannot be parsed
*/
public static OffsetDateTime parse(CharSequence text, DateTimeFormatter formatter) {
Objects.requireNonNull(formatter, "formatter");
return formatter.parse(text, OffsetDateTime::from);
}
示例15: parse
import java.time.format.DateTimeFormatter; //导入方法依赖的package包/类
/**
* Obtains an instance of {@code LocalTime} from a text string using a specific formatter.
* <p>
* The text is parsed using the formatter, returning a time.
*
* @param text the text to parse, not null
* @param formatter the formatter to use, not null
* @return the parsed local time, not null
* @throws DateTimeParseException if the text cannot be parsed
*/
public static LocalTime parse(CharSequence text, DateTimeFormatter formatter) {
Objects.requireNonNull(formatter, "formatter");
return formatter.parse(text, LocalTime::from);
}