本文整理汇总了Java中java.time.format.DateTimeFormatter.format方法的典型用法代码示例。如果您正苦于以下问题:Java DateTimeFormatter.format方法的具体用法?Java DateTimeFormatter.format怎么用?Java DateTimeFormatter.format使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类java.time.format.DateTimeFormatter
的用法示例。
在下文中一共展示了DateTimeFormatter.format方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: test_format_withZone_withChronology
import java.time.format.DateTimeFormatter; //导入方法依赖的package包/类
@Test(dataProvider="formatWithZoneWithChronology")
public void test_format_withZone_withChronology(Chronology overrideChrono, ZoneId overrideZone, TemporalAccessor temporal, String expected) {
DateTimeFormatter test = new DateTimeFormatterBuilder()
.optionalStart().appendValue(YEAR, 4).optionalEnd()
.appendLiteral(':').optionalStart().appendValue(HOUR_OF_DAY, 2).optionalEnd()
.appendLiteral(':').optionalStart().appendOffsetId().optionalEnd()
.appendLiteral(':').optionalStart().appendZoneId().optionalEnd()
.appendLiteral(':').optionalStart().appendChronologyId().optionalEnd()
.toFormatter(Locale.ENGLISH)
.withChronology(overrideChrono).withZone(overrideZone);
if (expected != null) {
String result = test.format(temporal);
assertEquals(result, expected);
} else {
try {
test.format(temporal);
fail("Formatting should have failed");
} catch (DateTimeException ex) {
// expected
}
}
}
示例2: test_formatLocalizedDate
import java.time.format.DateTimeFormatter; //导入方法依赖的package包/类
@Test(dataProvider="format_data")
public void test_formatLocalizedDate(Chronology chrono, Locale formatLocale, Locale numberingLocale,
ChronoLocalDate date, String expected) {
DateTimeFormatter dtf = DateTimeFormatter.ofLocalizedDate(FormatStyle.FULL)
.withChronology(chrono).withLocale(formatLocale)
.withDecimalStyle(DecimalStyle.of(numberingLocale));
String text = dtf.format(date);
assertEquals(text, expected);
}
示例3: generateBillInfoText
import java.time.format.DateTimeFormatter; //导入方法依赖的package包/类
private void generateBillInfoText(PDPage firstPage, PDPageContentStream contentStream)
throws IOException {
Guest guest = booking.getGuest();
String guestName = guest.getLastName() + ", " + guest.getFirstName();
MailingAddress address = guest.getMailingAddress();
String country = address.getCountry();
String city = address.getCity();
UsState state = address.getState();
String postalCode = address.getPostalCode();
String addressLine1 = address.getAddressLine1();
String addressLine2 = address.getAddressLine2();
contentStream.beginText();
// Print customer name line
contentStream.newLineAtOffset(80f, 710f);
contentStream.setLeading(10);
contentStream.setFont(BOLD, NORMAL_FONT_SIZE);
contentStream.showText("Customer name: ");
contentStream.setFont(NORMAL_FONT, NORMAL_FONT_SIZE);
contentStream.showText(guestName);
// Print customer mailing address lines
contentStream.newLine();
contentStream.setFont(BOLD, NORMAL_FONT_SIZE);
contentStream.showText("Billing Address: ");
contentStream.setFont(NORMAL_FONT, NORMAL_FONT_SIZE);
contentStream.newLine();
contentStream.showText(addressLine1);
if(!addressLine2.isEmpty()) {
contentStream.showText(",");
contentStream.showText(addressLine2);
}
contentStream.newLine();
contentStream.showText(city);
if(state != null) {
contentStream.showText(", " + state.getAbbreviation());
}
contentStream.showText(" " + postalCode);
contentStream.showText(" - ");
contentStream.showText(country);
// Print booking number line
contentStream.newLine();
contentStream.setFont(BOLD, NORMAL_FONT_SIZE);
contentStream.showText("Booking Number: ");
contentStream.setFont(NORMAL_FONT, NORMAL_FONT_SIZE);
contentStream.showText(booking.getId().toString());
// Print bill printed info line
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("EEE, MMMM d, u '-' H:m:s zz");
String formattedDate = formatter.format(ZonedDateTime.now(ZoneId.systemDefault()));
contentStream.newLine();
contentStream.setFont(BOLD, NORMAL_FONT_SIZE);
contentStream.showText("Bill Printed On: ");
contentStream.setFont(NORMAL_FONT, NORMAL_FONT_SIZE);
contentStream.showText(formattedDate);
contentStream.endText();
}
示例4: test_appendZoneText_parseNonGenricTimeZonePatterns_1
import java.time.format.DateTimeFormatter; //导入方法依赖的package包/类
@Test(dataProvider = "formatNonGenericTimeZonePatterns_1")
public void test_appendZoneText_parseNonGenricTimeZonePatterns_1(String pattern, LocalDateTime ldt, String expected) {
ZoneId zId = ZoneId.of("America/Los_Angeles");
DateTimeFormatter df = new DateTimeFormatterBuilder().appendPattern(pattern).toFormatter(Locale.US);
ZonedDateTime zdt = ZonedDateTime.of(ldt, zId);
String actual = df.format(zdt);
assertEquals(actual, expected);
}
示例5: test_appendText1arg_format
import java.time.format.DateTimeFormatter; //导入方法依赖的package包/类
@Test(dataProvider="printText")
public void test_appendText1arg_format(TemporalField field, TextStyle style, int value, String expected) throws Exception {
if (style == TextStyle.FULL) {
DateTimeFormatter f = builder.appendText(field).toFormatter(Locale.ENGLISH);
LocalDateTime dt = LocalDateTime.of(2010, 1, 1, 0, 0);
dt = dt.with(field, value);
String text = f.format(dt);
assertEquals(text, expected);
}
}
示例6: test_localized_offset_parse
import java.time.format.DateTimeFormatter; //导入方法依赖的package包/类
@Test
public void test_localized_offset_parse() {
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.S O")
.withLocale(Locale.ENGLISH);
String date = formatter.format(ZonedDateTime.now(ZoneOffset.UTC));
formatter.parse(date) ;
}
示例7: main
import java.time.format.DateTimeFormatter; //导入方法依赖的package包/类
public static void main(String [] args) {
LocalDate d = LocalDate.of(1980, Month.JANUARY, 1);
// en-GB inherits from en-001 where its short tz name for
// America/Los_Angeles is "non-inheritance marker". Thus the
// resulting formatted text should be a custom ID.
DateTimeFormatterBuilder dtfb = new DateTimeFormatterBuilder();
dtfb.appendZoneText(TextStyle.SHORT);
DateTimeFormatter dtf = dtfb.toFormatter(Locale.UK)
.withZone(ZoneId.of("America/Los_Angeles"));
String result = dtf.format(d);
System.out.println(result);
if (!"GMT-08:00".equals(result)) {
throw new RuntimeException("short time zone name for America/Los_Angeles in en_GB is incorrect. Got: " + result + ", expected: GMT-08:00");
}
// Islamic Um-Alqura calendar is an alias to Islamic calendar.
// In Islamic calendar data, only month names are localized, also
// date/time format for FULL style should be inherited from "generic"
// calendar, where it includes ERA field.
Locale locale = Locale.forLanguageTag("en-US-u-ca-islamic-umalqura");
Chronology chrono = Chronology.ofLocale(locale);
dtf = DateTimeFormatter
.ofLocalizedDate(FormatStyle.FULL)
.withLocale(locale)
.withChronology(chrono);
result = dtf.format(d);
System.out.println(dtf.format(d));
if (!"Tuesday, Safar 12, 1400 AH".equals(result)) {
throw new RuntimeException("FULL date format of Islamic Um-Alqura calendar in en_US is incorrect. Got: " + result + ", expected: Tuesday, Safar 12, 1400 AH");
}
}
示例8: test_appendText2arg_format
import java.time.format.DateTimeFormatter; //导入方法依赖的package包/类
@Test(dataProvider="printText")
public void test_appendText2arg_format(TemporalField field, TextStyle style, int value, String expected) throws Exception {
DateTimeFormatter f = builder.appendText(field, style).toFormatter(Locale.ENGLISH);
LocalDateTime dt = LocalDateTime.of(2010, 1, 1, 0, 0);
dt = dt.with(field, value);
String text = f.format(dt);
assertEquals(text, expected);
}
示例9: test_formatDayOfWeek
import java.time.format.DateTimeFormatter; //导入方法依赖的package包/类
@Test(dataProvider="print_DayOfWeekData")
public void test_formatDayOfWeek(Locale locale, String pattern, String expected, DayOfWeek dayOfWeek) {
DateTimeFormatter formatter = getPatternFormatter(pattern).withLocale(locale);
String text = formatter.format(dayOfWeek);
assertEquals(text, expected);
}
示例10: updateDateText
import java.time.format.DateTimeFormatter; //导入方法依赖的package包/类
private void updateDateText() {
DateTimeFormatter formatter = getSkinnable().getDateTimeFormatter();
String text = formatter.format(getSkinnable().getDate());
dateText.setText(text);
}
示例11: test_appendPattern_patternPrint
import java.time.format.DateTimeFormatter; //导入方法依赖的package包/类
@Test(dataProvider="patternPrint")
public void test_appendPattern_patternPrint(String input, Temporal temporal, String expected) throws Exception {
DateTimeFormatter f = builder.appendPattern(input).toFormatter(Locale.UK);
String test = f.format(temporal);
assertEquals(test, expected);
}
示例12: format
import java.time.format.DateTimeFormatter; //导入方法依赖的package包/类
/**
* Formats this date using the specified formatter.
* <p>
* This date will be passed to the formatter to produce a string.
* <p>
* The default implementation must behave as follows:
* <pre>
* return formatter.format(this);
* </pre>
*
* @param formatter the formatter to use, not null
* @return the formatted date string, not null
* @throws DateTimeException if an error occurs during printing
*/
default String format(DateTimeFormatter formatter) {
Objects.requireNonNull(formatter, "formatter");
return formatter.format(this);
}
示例13: format
import java.time.format.DateTimeFormatter; //导入方法依赖的package包/类
/**
* Formats this date-time using the specified formatter.
* <p>
* This date-time will be passed to the formatter to produce a string.
*
* @param formatter the formatter to use, not null
* @return the formatted date-time string, not null
* @throws DateTimeException if an error occurs during printing
*/
@Override // override for Javadoc and performance
public String format(DateTimeFormatter formatter) {
Objects.requireNonNull(formatter, "formatter");
return formatter.format(this);
}
示例14: format
import java.time.format.DateTimeFormatter; //导入方法依赖的package包/类
/**
* Formats this time using the specified formatter.
* <p>
* This time will be passed to the formatter to produce a string.
*
* @param formatter the formatter to use, not null
* @return the formatted time string, not null
* @throws DateTimeException if an error occurs during printing
*/
public String format(DateTimeFormatter formatter) {
Objects.requireNonNull(formatter, "formatter");
return formatter.format(this);
}
示例15: format
import java.time.format.DateTimeFormatter; //导入方法依赖的package包/类
/**
* Formats this date using the specified formatter.
* <p>
* This date will be passed to the formatter to produce a string.
*
* @param formatter the formatter to use, not null
* @return the formatted date string, not null
* @throws DateTimeException if an error occurs during printing
*/
@Override // override for Javadoc and performance
public String format(DateTimeFormatter formatter) {
Objects.requireNonNull(formatter, "formatter");
return formatter.format(this);
}