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

The following examples show how to use java.time.format.DateTimeFormatter#format() . 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 openjdk-jdk8u-backup 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: TCKDateTimeTextPrinting.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
@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);
}
 
Example 3
Source File: AbstractTemporalDatatype.java    From cuba with Apache License 2.0 5 votes vote down vote up
@Override
public String format(@Nullable Object value, Locale locale) {
    if (value == null) {
        return "";
    }

    FormatStrings formatStrings = AppBeans.get(FormatStringsRegistry.class).getFormatStrings(locale);
    if (formatStrings == null) {
        return format(value);
    }

    DateTimeFormatter formatter = getDateTimeFormatter(formatStrings, locale);
    //noinspection unchecked
    return formatter.format((TemporalAccessor) value);
}
 
Example 4
Source File: TCKDateTimeTextPrinting.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
@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);
}
 
Example 5
Source File: DataTableModel.java    From MeteoInfo with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Override
public Object getValueAt(int rowIndex, int columnIndex) {
    if (_dataTable.getColumns().get(columnIndex).getDataType() == DataType.DATE){
        DateTimeFormatter format = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        return format.format((LocalDateTime)_dataTable.getValue(rowIndex, columnIndex));
    } else 
        return _dataTable.getValue(rowIndex, columnIndex);
}
 
Example 6
Source File: RpmMojo.java    From rpm-builder with Eclipse Public License 2.0 5 votes vote down vote up
private String makeSnapshotReleaseString ()
{
    if ( this.snapshotBuildId == null || this.snapshotBuildId.isEmpty () )
    {
        final DateTimeFormatter formatter = DateTimeFormatter.ofPattern ( "yyyyMMddHHmm", Locale.ROOT );
        return this.snapshotReleasePrefix + formatter.format ( Instant.now ().atOffset ( ZoneOffset.UTC ) );
    }
    else
    {
        return this.snapshotReleasePrefix + this.snapshotBuildId;
    }
}
 
Example 7
Source File: TCKLocalizedFieldPrinter.java    From openjdk-8 with GNU General Public License v2.0 5 votes vote down vote up
@Test(dataProvider = "LocalWeekBasedYearPatterns")
public void test_print_WeekBasedYear(String pattern, String expectedText, LocalDate date) {
    DateTimeFormatter dtf = DateTimeFormatter.ofPattern(pattern, locale);
    String result = dtf.format(date);
    WeekFields weekDef = WeekFields.of(locale);
    assertEquals(result, expectedText, "Incorrect formatting for " + pattern + ", weekDef: " + weekDef);
}
 
Example 8
Source File: TCKLocalizedFieldPrinter.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
@Test(dataProvider = "LocalWeekBasedYearPatterns")
public void test_print_WeekBasedYear(String pattern, String expectedText, LocalDate date) {
    DateTimeFormatter dtf = DateTimeFormatter.ofPattern(pattern, locale);
    String result = dtf.format(date);
    WeekFields weekDef = WeekFields.of(locale);
    assertEquals(result, expectedText, "Incorrect formatting for " + pattern + ", weekDef: " + weekDef);
}
 
Example 9
Source File: TCKLocalizedPrinterParser.java    From jdk8u_jdk with GNU General Public License v2.0 5 votes vote down vote up
@SuppressWarnings("deprecation")
@Test(dataProvider="date")
public void test_date_print(LocalDate date, FormatStyle dateStyle, int dateStyleOld, Locale locale) {
    DateFormat old = DateFormat.getDateInstance(dateStyleOld, locale);
    Date oldDate = new Date(date.getYear() - 1900, date.getMonthValue() - 1, date.getDayOfMonth());
    String text = old.format(oldDate);

    DateTimeFormatter f = builder.appendLocalized(dateStyle, null).toFormatter(locale);
    String formatted = f.format(date);
    assertEquals(formatted, text);
}
 
Example 10
Source File: TCKLocalizedPrinterParser.java    From jdk8u_jdk with GNU General Public License v2.0 5 votes vote down vote up
@SuppressWarnings("deprecation")
@Test(dataProvider="time")
public void test_time_print(LocalTime time, FormatStyle timeStyle, int timeStyleOld, Locale locale) {
    DateFormat old = DateFormat.getTimeInstance(timeStyleOld, locale);
    Date oldDate = new Date(1970, 0, 0, time.getHour(), time.getMinute(), time.getSecond());
    String text = old.format(oldDate);

    DateTimeFormatter f = builder.appendLocalized(null, timeStyle).toFormatter(locale);
    String formatted = f.format(time);
    assertEquals(formatted, text);
}
 
Example 11
Source File: JudgelsPlayUtils.java    From judgels with GNU General Public License v2.0 4 votes vote down vote up
public static String formatDetailedDateTime(long timestamp) {
    ZonedDateTime zonedDateTime = ZonedDateTime.ofInstant(Instant.ofEpochMilli(timestamp), ZoneOffset.systemDefault());
    DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd-MMM-yyyy HH:mm:ss XXX");
    return formatter.format(zonedDateTime);
}
 
Example 12
Source File: TestTextPrinter.java    From jdk8u-dev-jdk with GNU General Public License v2.0 4 votes vote down vote up
@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);
}
 
Example 13
Source File: TestDateTimeFormatterBuilder.java    From jdk8u-dev-jdk with GNU General Public License v2.0 4 votes vote down vote up
@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);
}
 
Example 14
Source File: ScanUploadSchedulingServiceTest.java    From emodb with Apache License 2.0 4 votes vote down vote up
@Test(dataProvider = "every10minutes")
public void testStartScheduledScan(Instant now)
        throws Exception {
    DateTimeFormatter format = DateTimeFormatter.ofPattern("HH:mmX");
    String timeOfDay = format.format(now.atZone(ZoneOffset.UTC));

    ScanDestination destination = ScanDestination.to(URI.create("s3://bucket/path/to/root"));

    ScheduledDailyScanUpload scanUpload =
            new ScheduledDailyScanUpload("daily", timeOfDay, DateTimeFormatter.ofPattern("'test'-yyyyMMddHHmmss").withZone(ZoneOffset.UTC),
                    destination, DateTimeFormatter.ofPattern("yyyyMMddHHmmss").withZone(ZoneOffset.UTC),
                    ImmutableList.of("placement1"), 1, true, false, 1000000, Duration.ofMinutes(10));

    ScanUploader.ScanAndUploadBuilder builder = mock(ScanUploader.ScanAndUploadBuilder.class);
    ScanUploader scanUploader = mock(ScanUploader.class);
    when(scanUploader.scanAndUpload(anyString(), any(ScanOptions.class))).thenReturn(builder);
    StashRequestManager stashRequestManager = mock(StashRequestManager.class);
    ScanCountListener scanCountListener = mock(ScanCountListener.class);

    Clock clock = Clock.fixed(Instant.ofEpochMilli(now.toEpochMilli()), ZoneId.systemDefault());

    ScanUploadSchedulingService.DelegateSchedulingService service =
            new ScanUploadSchedulingService.DelegateSchedulingService(scanUploader, stashRequestManager, ImmutableList.<ScheduledDailyScanUpload>of(), scanCountListener, clock);

    service.startScheduledScan(scanUpload, now);

    String expectedScanId = DateTimeFormatter.ofPattern("'test'-yyyyMMddHHmmss").withZone(ZoneOffset.UTC).format(now);
    ScanDestination expectedDestination = ScanDestination.to(
            URI.create("s3://bucket/path/to/root/" + DateTimeFormatter.ofPattern("yyyyMMddHHmmss").withZone(ZoneOffset.UTC).format(now)));

    // Called once to to verify the scan does not exist
    verify(scanUploader).getStatus(expectedScanId);

    verify(scanUploader).scanAndUpload(expectedScanId,
            new ScanOptions("placement1")
                    .addDestination(expectedDestination)
                    .setMaxConcurrentSubRangeScans(1)
                    .setScanByAZ(true));

    verify(builder).start();
    
    verifyNoMoreInteractions(scanUploader);
}
 
Example 15
Source File: YearMonth.java    From desugar_jdk_libs with GNU General Public License v2.0 2 votes vote down vote up
/**
 * Formats this year-month using the specified formatter.
 * <p>
 * This year-month will be passed to the formatter to produce a string.
 *
 * @param formatter  the formatter to use, not null
 * @return the formatted year-month string, not null
 * @throws DateTimeException if an error occurs during printing
 */
public String format(DateTimeFormatter formatter) {
    Objects.requireNonNull(formatter, "formatter");
    return formatter.format(this);
}
 
Example 16
Source File: ChronoZonedDateTime.java    From dragonwell8_jdk with GNU General Public License v2.0 2 votes vote down vote up
/**
 * Formats this date-time using the specified formatter.
 * <p>
 * This date-time 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-time string, not null
 * @throws DateTimeException if an error occurs during printing
 */
default String format(DateTimeFormatter formatter) {
    Objects.requireNonNull(formatter, "formatter");
    return formatter.format(this);
}
 
Example 17
Source File: OffsetDateTime.java    From hottub with GNU General Public License v2.0 2 votes vote down vote up
/**
 * 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
 */
public String format(DateTimeFormatter formatter) {
    Objects.requireNonNull(formatter, "formatter");
    return formatter.format(this);
}
 
Example 18
Source File: OffsetTime.java    From openjdk-jdk8u with GNU General Public License v2.0 2 votes vote down vote up
/**
 * 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);
}
 
Example 19
Source File: ChronoLocalDate.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 2 votes vote down vote up
/**
 * 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);
}
 
Example 20
Source File: MonthDay.java    From Bytecoder with Apache License 2.0 2 votes vote down vote up
/**
 * Formats this month-day using the specified formatter.
 * <p>
 * This month-day will be passed to the formatter to produce a string.
 *
 * @param formatter  the formatter to use, not null
 * @return the formatted month-day string, not null
 * @throws DateTimeException if an error occurs during printing
 */
public String format(DateTimeFormatter formatter) {
    Objects.requireNonNull(formatter, "formatter");
    return formatter.format(this);
}