java.time.format.FormatStyle Java Examples

The following examples show how to use java.time.format.FormatStyle. 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: TimeZones.java    From JavaSE8-Features with GNU General Public License v3.0 7 votes vote down vote up
public static void main(String[] args) {

        DateTimeFormatter dtf = DateTimeFormatter.ofLocalizedDateTime(FormatStyle.SHORT);
        LocalDateTime currentDT = LocalDateTime.now();
        System.out.println(dtf.format(currentDT));
        
        ZonedDateTime gmt = ZonedDateTime.now(ZoneId.of("GMT+0"));
        System.out.println(dtf.format(gmt));
        
        ZonedDateTime ny = ZonedDateTime.now(ZoneId.of("America/New_York"));
        System.out.println(dtf.format(ny));
        
        Set<String> zones = ZoneId.getAvailableZoneIds();
        Predicate<String> condition = str -> str.contains("London");
        zones.forEach(z -> {
            if(condition.test(z))
            System.out.println(z);
        });

    }
 
Example #2
Source File: PDTFormatter.java    From ph-commons with Apache License 2.0 6 votes vote down vote up
@Nonnull
public static FormatStyle toFormatStyle (final int nStyle)
{
  switch (nStyle)
  {
    case DateFormat.FULL:
      return FormatStyle.FULL;
    case DateFormat.LONG:
      return FormatStyle.LONG;
    case DateFormat.MEDIUM:
      return FormatStyle.MEDIUM;
    case DateFormat.SHORT:
      return FormatStyle.SHORT;
    default:
      throw new IllegalArgumentException ("Invalid style passed: " + nStyle);
  }
}
 
Example #3
Source File: LocalDate1.java    From javacore with Creative Commons Attribution Share Alike 4.0 International 6 votes vote down vote up
public static void main(String[] args) {
    LocalDate today = LocalDate.now();
    LocalDate tomorrow = today.plus(1, ChronoUnit.DAYS);
    LocalDate yesterday = tomorrow.minusDays(2);

    System.out.println(today);
    System.out.println(tomorrow);
    System.out.println(yesterday);

    LocalDate independenceDay = LocalDate.of(2014, Month.JULY, 4);
    DayOfWeek dayOfWeek = independenceDay.getDayOfWeek();
    System.out.println(dayOfWeek); // FRIDAY

    DateTimeFormatter germanFormatter = DateTimeFormatter.ofLocalizedDate(FormatStyle.MEDIUM)
        .withLocale(Locale.GERMAN);

    LocalDate xmas = LocalDate.parse("24.12.2014", germanFormatter);
    System.out.println(xmas); // 2014-12-24
}
 
Example #4
Source File: InstantDeserializer.java    From java-gitlab-api with Apache License 2.0 6 votes vote down vote up
private static Stream<DateTimeFormatter> getFormatters() {
    return Stream.of(
        // English (Standard) Formats
        DateTimeFormatter.ofLocalizedDateTime(FormatStyle.LONG, FormatStyle.LONG).withLocale(Locale.ENGLISH),
        DateTimeFormatter.ofLocalizedDateTime(FormatStyle.MEDIUM, FormatStyle.LONG).withLocale(Locale.ENGLISH),
        DateTimeFormatter.ofLocalizedDateTime(FormatStyle.SHORT, FormatStyle.LONG).withLocale(Locale.ENGLISH),
        DateTimeFormatter.ofLocalizedDateTime(FormatStyle.LONG, FormatStyle.MEDIUM).withLocale(Locale.ENGLISH),
        DateTimeFormatter.ofLocalizedDateTime(FormatStyle.MEDIUM, FormatStyle.MEDIUM).withLocale(Locale.ENGLISH),
        DateTimeFormatter.ofLocalizedDateTime(FormatStyle.SHORT, FormatStyle.MEDIUM).withLocale(Locale.ENGLISH),
        DateTimeFormatter.ofLocalizedDateTime(FormatStyle.LONG, FormatStyle.SHORT).withLocale(Locale.ENGLISH),
        DateTimeFormatter.ofLocalizedDateTime(FormatStyle.MEDIUM, FormatStyle.SHORT).withLocale(Locale.ENGLISH),
        DateTimeFormatter.ofLocalizedDateTime(FormatStyle.SHORT, FormatStyle.SHORT).withLocale(Locale.ENGLISH),

        // ISO Formats
        LOCAL_DATE_TIME_FORMATTER_WITH_SPACE_SEPARATOR,
        DateTimeFormatter.ISO_LOCAL_DATE_TIME,
        DateTimeFormatter.ISO_DATE_TIME
    );
}
 
Example #5
Source File: TCKLocalizedPrinterParser.java    From jdk8u-jdk with GNU General Public License v2.0 6 votes vote down vote up
@DataProvider(name="time")
    Object[][] data_time() {
        return new Object[][] {
                {LocalTime.of(11, 30), FormatStyle.SHORT, DateFormat.SHORT, Locale.UK},
                {LocalTime.of(11, 30), FormatStyle.SHORT, DateFormat.SHORT, Locale.US},
                {LocalTime.of(11, 30), FormatStyle.SHORT, DateFormat.SHORT, Locale.FRANCE},
                {LocalTime.of(11, 30), FormatStyle.SHORT, DateFormat.SHORT, Locale.JAPAN},

                {LocalTime.of(11, 30), FormatStyle.MEDIUM, DateFormat.MEDIUM, Locale.UK},
                {LocalTime.of(11, 30), FormatStyle.MEDIUM, DateFormat.MEDIUM, Locale.US},
                {LocalTime.of(11, 30), FormatStyle.MEDIUM, DateFormat.MEDIUM, Locale.FRANCE},
                {LocalTime.of(11, 30), FormatStyle.MEDIUM, DateFormat.MEDIUM, Locale.JAPAN},

                // these localized patterns include "z" which isn't available from LocalTime
//                {LocalTime.of(11, 30), FormatStyle.LONG, DateFormat.LONG, Locale.UK},
//                {LocalTime.of(11, 30), FormatStyle.LONG, DateFormat.LONG, Locale.US},
//                {LocalTime.of(11, 30), FormatStyle.LONG, DateFormat.LONG, Locale.FRANCE},
//                {LocalTime.of(11, 30), FormatStyle.LONG, DateFormat.LONG, Locale.JAPAN},
//
//                {LocalTime.of(11, 30), FormatStyle.FULL, DateFormat.FULL, Locale.UK},
//                {LocalTime.of(11, 30), FormatStyle.FULL, DateFormat.FULL, Locale.US},
//                {LocalTime.of(11, 30), FormatStyle.FULL, DateFormat.FULL, Locale.FRANCE},
//                {LocalTime.of(11, 30), FormatStyle.FULL, DateFormat.FULL, Locale.JAPAN},
        };
    }
 
Example #6
Source File: TaskDetailPart.java    From elexis-3-core with Eclipse Public License 1.0 6 votes vote down vote up
@PostConstruct
public void postConstruct(Composite parent, MPart part){
	parent.setLayout(new GridLayout(1, false));
	
	ITask task = (ITask) part.getTransientData().get("task");
	part.setIconURI(TaskResultLabelProvider.getInstance().getIconURI(task));
	
	String runAt;
	LocalDateTime _runAt = task.getRunAt();
	if(_runAt != null) {
		runAt = _runAt.format(DateTimeFormatter.ofLocalizedDate(FormatStyle.FULL));
	} else {
		runAt = "queued";
	}

	String partLabel = task.getTaskDescriptor().getReferenceId() + " - " + runAt;
	part.setLabel(partLabel);
	
	Map<String, Object> e4Services = new HashMap<String, Object>();
	e4Services.put(ECommandService.class.getName(), commandService);
	e4Services.put(EHandlerService.class.getName(), handlerService);
	
	taskResultDetailDialogContributions.createDetailCompositeForTask(parent, task, e4Services);
}
 
Example #7
Source File: TestNonIsoFormatter.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
@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);
}
 
Example #8
Source File: TestNonIsoFormatter.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
@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);
}
 
Example #9
Source File: TCKLocalizedPrinterParser.java    From openjdk-jdk8u 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: DateTimeFormatterFactory.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
private FormatStyle convertStyleCharacter(char c) {
	switch (c) {
		case 'S': return FormatStyle.SHORT;
		case 'M': return FormatStyle.MEDIUM;
		case 'L': return FormatStyle.LONG;
		case 'F': return FormatStyle.FULL;
		case '-': return null;
		default: throw new IllegalArgumentException("Invalid style character '" + c + "'");
	}
}
 
Example #11
Source File: DateTimeFormattingTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void testBindDateTimeWithSpecificStyle() throws Exception {
	DateTimeFormatterRegistrar registrar = new DateTimeFormatterRegistrar();
	registrar.setDateTimeStyle(FormatStyle.MEDIUM);
	setUp(registrar);
	MutablePropertyValues propertyValues = new MutablePropertyValues();
	propertyValues.add("localDateTime", LocalDateTime.of(2009, 10, 31, 12, 0));
	binder.bind(propertyValues);
	assertEquals(0, binder.getBindingResult().getErrorCount());
	String value = binder.getBindingResult().getFieldValue("localDateTime").toString();
	assertTrue(value.startsWith("Oct 31, 2009"));
	assertTrue(value.endsWith("12:00:00 PM"));
}
 
Example #12
Source File: DateTimeFormatterFactory.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Nullable
private FormatStyle convertStyleCharacter(char c) {
	switch (c) {
		case 'S': return FormatStyle.SHORT;
		case 'M': return FormatStyle.MEDIUM;
		case 'L': return FormatStyle.LONG;
		case 'F': return FormatStyle.FULL;
		case '-': return null;
		default: throw new IllegalArgumentException("Invalid style character '" + c + "'");
	}
}
 
Example #13
Source File: TestUmmAlQuraChronology.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
@Test(dataProvider="chronoDateTimes")
public void test_formatterOfLocalizedDate(int year, int month, int day, String expected) {
    HijrahDate hd = HijrahChronology.INSTANCE.date(year, month, day);
    ChronoLocalDateTime<HijrahDate> hdt = hd.atTime(LocalTime.NOON);
    hdt = hdt.plus(1, ChronoUnit.YEARS);
    hdt = hdt.plus(1, ChronoUnit.MONTHS);
    hdt = hdt.plus(1, ChronoUnit.DAYS);
    hdt = hdt.plus(1, ChronoUnit.HOURS);
    hdt = hdt.plus(1, ChronoUnit.MINUTES);
    hdt = hdt.plus(1, ChronoUnit.SECONDS);
    DateTimeFormatter df = DateTimeFormatter.ofLocalizedDate(FormatStyle.LONG).withChronology(Chronology.of("Hijrah-umalqura")).withLocale(Locale.US);
    assertEquals(df.format(hdt), expected);
}
 
Example #14
Source File: TCKLocalizedPrinterParser.java    From openjdk-8 with GNU General Public License v2.0 5 votes vote down vote up
@SuppressWarnings("deprecation")
@Test(dataProvider="time")
public void test_time_parse(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);
    TemporalAccessor parsed = f.parse(text, pos);
    assertEquals(pos.getIndex(), text.length());
    assertEquals(pos.getErrorIndex(), -1);
    assertEquals(LocalTime.from(parsed), time);
}
 
Example #15
Source File: TCKLocalizedPrinterParser.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
@SuppressWarnings("deprecation")
@Test(dataProvider="time")
public void test_time_parse(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);
    TemporalAccessor parsed = f.parse(text, pos);
    assertEquals(pos.getIndex(), text.length());
    assertEquals(pos.getErrorIndex(), -1);
    assertEquals(LocalTime.from(parsed), time);
}
 
Example #16
Source File: TCKFormatStyle.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
@DataProvider(name="formatStyle")
Object[][] data_formatStyle() {
    return new Object[][] {
            {ZonedDateTime.of(LocalDateTime.of(2001, 10, 2, 1, 2, 3), ZONEID_PARIS), FormatStyle.FULL, "Tuesday, October 2, 2001 1:02:03 AM CEST Europe/Paris"},
            {ZonedDateTime.of(LocalDateTime.of(2001, 10, 2, 1, 2, 3), ZONEID_PARIS), FormatStyle.LONG, "October 2, 2001 1:02:03 AM CEST Europe/Paris"},
            {ZonedDateTime.of(LocalDateTime.of(2001, 10, 2, 1, 2, 3), ZONEID_PARIS), FormatStyle.MEDIUM, "Oct 2, 2001 1:02:03 AM Europe/Paris"},
            {ZonedDateTime.of(LocalDateTime.of(2001, 10, 2, 1, 2, 3), ZONEID_PARIS), FormatStyle.SHORT, "10/2/01 1:02 AM Europe/Paris"},

            {ZonedDateTime.of(LocalDateTime.of(2001, 10, 2, 1, 2, 3), OFFSET_PTWO), FormatStyle.FULL, "Tuesday, October 2, 2001 1:02:03 AM +02:00 +02:00"},
            {ZonedDateTime.of(LocalDateTime.of(2001, 10, 2, 1, 2, 3), OFFSET_PTWO), FormatStyle.LONG, "October 2, 2001 1:02:03 AM +02:00 +02:00"},
            {ZonedDateTime.of(LocalDateTime.of(2001, 10, 2, 1, 2, 3), OFFSET_PTWO), FormatStyle.MEDIUM, "Oct 2, 2001 1:02:03 AM +02:00"},
            {ZonedDateTime.of(LocalDateTime.of(2001, 10, 2, 1, 2, 3), OFFSET_PTWO), FormatStyle.SHORT, "10/2/01 1:02 AM +02:00"},
    };
}
 
Example #17
Source File: TestNonIsoFormatter.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
@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);
}
 
Example #18
Source File: DateTimeFormatterUnitTest.java    From tutorials with MIT License 5 votes vote down vote up
@Test
public void shouldPrintStyledDateTime() {
    LocalDateTime anotherSummerDay = LocalDateTime.of(2016, 8, 23, 13, 12, 45);
    Assert.assertEquals("Tuesday, August 23, 2016 1:12:45 PM EET", DateTimeFormatter.ofLocalizedDateTime(FormatStyle.FULL).withZone(ZoneId.of("Europe/Helsinki")).format(anotherSummerDay));
    Assert.assertEquals("August 23, 2016 1:12:45 PM EET", DateTimeFormatter.ofLocalizedDateTime(FormatStyle.LONG).withZone(ZoneId.of("Europe/Helsinki")).format(anotherSummerDay));
    Assert.assertEquals("Aug 23, 2016 1:12:45 PM", DateTimeFormatter.ofLocalizedDateTime(FormatStyle.MEDIUM).withZone(ZoneId.of("Europe/Helsinki")).format(anotherSummerDay));
    Assert.assertEquals("8/23/16 1:12 PM", DateTimeFormatter.ofLocalizedDateTime(FormatStyle.SHORT).withZone(ZoneId.of("Europe/Helsinki")).format(anotherSummerDay));
}
 
Example #19
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_parse(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);
    TemporalAccessor parsed = f.parse(text, pos);
    assertEquals(pos.getIndex(), text.length());
    assertEquals(pos.getErrorIndex(), -1);
    assertEquals(LocalDate.from(parsed), date);
}
 
Example #20
Source File: EpochService.java    From dev-tools with Apache License 2.0 5 votes vote down vote up
public static String toHumanEpoch(LocalDateTime dt) {
    String formatted = DateTimeFormatter.ofLocalizedDateTime(FormatStyle.FULL)
            .format(dt.atZone(ZoneId.systemDefault()));
    String formattedUTC = DateTimeFormatter.ofLocalizedDateTime(FormatStyle.FULL)
            .format(dt.atZone(ZoneId.systemDefault()).withZoneSameInstant(ZoneOffset.UTC));
    return "Local Time:\n" + formatted + "\nGMT:\n" + formattedUTC;
}
 
Example #21
Source File: JFXTimePickerContent.java    From JFoenix with Apache License 2.0 5 votes vote down vote up
void updateValue() {
    if (is24HourView) {
        LocalTimeStringConverter localTimeStringConverter =
            new LocalTimeStringConverter(FormatStyle.SHORT, Locale.GERMAN);
        timePicker.setValue(localTimeStringConverter.fromString(selectedHourLabel.getText()
                                                                + ":" + selectedMinLabel.getText()));
    } else {
        timePicker.setValue(LocalTime.parse(selectedHourLabel.getText() + ":" + selectedMinLabel.getText() + " " + period.get(), DateTimeFormatter.ofPattern("h:mm a").withLocale(Locale.ENGLISH)));
    }
}
 
Example #22
Source File: ODateLabel.java    From Orienteer with Apache License 2.0 5 votes vote down vote up
private DateTimeFormatter getFormatter(ZoneId zoneId) {
    DateTimeFormatter formatter;
    if (showTime) {
        formatter = DateTimeFormatter.ofLocalizedDateTime(FormatStyle.MEDIUM);
    } else formatter = DateTimeFormatter.ofLocalizedDate(FormatStyle.MEDIUM);

    return formatter.withLocale(getLocale())
    				.withZone(convertTimeZone&&zoneId!=null
    									?zoneId
    									:OrienteerWebSession.get().getDatabase().getStorage()
    												.getConfiguration().getTimeZone().toZoneId());
}
 
Example #23
Source File: TCKFormatStyle.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
@DataProvider(name="formatStyle")
Object[][] data_formatStyle() {
    return new Object[][] {
            {ZonedDateTime.of(LocalDateTime.of(2001, 10, 2, 1, 2, 3), ZONEID_PARIS), FormatStyle.FULL, "Tuesday, October 2, 2001 1:02:03 AM CEST Europe/Paris"},
            {ZonedDateTime.of(LocalDateTime.of(2001, 10, 2, 1, 2, 3), ZONEID_PARIS), FormatStyle.LONG, "October 2, 2001 1:02:03 AM CEST Europe/Paris"},
            {ZonedDateTime.of(LocalDateTime.of(2001, 10, 2, 1, 2, 3), ZONEID_PARIS), FormatStyle.MEDIUM, "Oct 2, 2001 1:02:03 AM Europe/Paris"},
            {ZonedDateTime.of(LocalDateTime.of(2001, 10, 2, 1, 2, 3), ZONEID_PARIS), FormatStyle.SHORT, "10/2/01 1:02 AM Europe/Paris"},

            {ZonedDateTime.of(LocalDateTime.of(2001, 10, 2, 1, 2, 3), OFFSET_PTWO), FormatStyle.FULL, "Tuesday, October 2, 2001 1:02:03 AM +02:00 +02:00"},
            {ZonedDateTime.of(LocalDateTime.of(2001, 10, 2, 1, 2, 3), OFFSET_PTWO), FormatStyle.LONG, "October 2, 2001 1:02:03 AM +02:00 +02:00"},
            {ZonedDateTime.of(LocalDateTime.of(2001, 10, 2, 1, 2, 3), OFFSET_PTWO), FormatStyle.MEDIUM, "Oct 2, 2001 1:02:03 AM +02:00"},
            {ZonedDateTime.of(LocalDateTime.of(2001, 10, 2, 1, 2, 3), OFFSET_PTWO), FormatStyle.SHORT, "10/2/01 1:02 AM +02:00"},
    };
}
 
Example #24
Source File: TCKFormatStyle.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
@DataProvider(name="formatStyle")
Object[][] data_formatStyle() {
    return new Object[][] {
            {ZonedDateTime.of(LocalDateTime.of(2001, 10, 2, 1, 2, 3), ZONEID_PARIS), FormatStyle.FULL, "Tuesday, October 2, 2001 1:02:03 AM CEST Europe/Paris"},
            {ZonedDateTime.of(LocalDateTime.of(2001, 10, 2, 1, 2, 3), ZONEID_PARIS), FormatStyle.LONG, "October 2, 2001 1:02:03 AM CEST Europe/Paris"},
            {ZonedDateTime.of(LocalDateTime.of(2001, 10, 2, 1, 2, 3), ZONEID_PARIS), FormatStyle.MEDIUM, "Oct 2, 2001 1:02:03 AM Europe/Paris"},
            {ZonedDateTime.of(LocalDateTime.of(2001, 10, 2, 1, 2, 3), ZONEID_PARIS), FormatStyle.SHORT, "10/2/01 1:02 AM Europe/Paris"},

            {ZonedDateTime.of(LocalDateTime.of(2001, 10, 2, 1, 2, 3), OFFSET_PTWO), FormatStyle.FULL, "Tuesday, October 2, 2001 1:02:03 AM +02:00 +02:00"},
            {ZonedDateTime.of(LocalDateTime.of(2001, 10, 2, 1, 2, 3), OFFSET_PTWO), FormatStyle.LONG, "October 2, 2001 1:02:03 AM +02:00 +02:00"},
            {ZonedDateTime.of(LocalDateTime.of(2001, 10, 2, 1, 2, 3), OFFSET_PTWO), FormatStyle.MEDIUM, "Oct 2, 2001 1:02:03 AM +02:00"},
            {ZonedDateTime.of(LocalDateTime.of(2001, 10, 2, 1, 2, 3), OFFSET_PTWO), FormatStyle.SHORT, "10/2/01 1:02 AM +02:00"},
    };
}
 
Example #25
Source File: TestNonIsoFormatter.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
@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);
}
 
Example #26
Source File: TCKLocalizedPrinterParser.java    From dragonwell8_jdk with GNU General Public License v2.0 5 votes vote down vote up
@SuppressWarnings("deprecation")
@Test(dataProvider="time")
public void test_time_parse(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);
    TemporalAccessor parsed = f.parse(text, pos);
    assertEquals(pos.getIndex(), text.length());
    assertEquals(pos.getErrorIndex(), -1);
    assertEquals(LocalTime.from(parsed), time);
}
 
Example #27
Source File: TCKLocalizedPrinterParser.java    From openjdk-8 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 #28
Source File: TCKLocalizedPrinterParser.java    From dragonwell8_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 #29
Source File: TCKFormatStyle.java    From j2objc with Apache License 2.0 5 votes vote down vote up
@DataProvider
public static Object[][] data_formatStyle() {
    return new Object[][] {
            // Android-changed: date/time patterns changed in new CLDR; adapt to UK locale.
            {ZonedDateTime.of(LocalDateTime.of(2001, 10, 2, 1, 2, 3), ZONEID_PARIS), FormatStyle.FULL, "Tuesday, 2 October 2001 at 01:02:03 Central European Summer Time Europe/Paris"},
            {ZonedDateTime.of(LocalDateTime.of(2001, 10, 2, 1, 2, 3), ZONEID_PARIS), FormatStyle.LONG, "2 October 2001 at 01:02:03 CEST Europe/Paris"},
            {ZonedDateTime.of(LocalDateTime.of(2001, 10, 2, 1, 2, 3), ZONEID_PARIS), FormatStyle.MEDIUM, "2 Oct 2001, 01:02:03 Europe/Paris"},
            {ZonedDateTime.of(LocalDateTime.of(2001, 10, 2, 1, 2, 3), ZONEID_PARIS), FormatStyle.SHORT, "02/10/2001, 01:02 Europe/Paris"},

            {ZonedDateTime.of(LocalDateTime.of(2001, 10, 2, 1, 2, 3), OFFSET_PTWO), FormatStyle.FULL, "Tuesday, 2 October 2001 at 01:02:03 +02:00 +02:00"},
            {ZonedDateTime.of(LocalDateTime.of(2001, 10, 2, 1, 2, 3), OFFSET_PTWO), FormatStyle.LONG, "2 October 2001 at 01:02:03 +02:00 +02:00"},
            {ZonedDateTime.of(LocalDateTime.of(2001, 10, 2, 1, 2, 3), OFFSET_PTWO), FormatStyle.MEDIUM, "2 Oct 2001, 01:02:03 +02:00"},
            {ZonedDateTime.of(LocalDateTime.of(2001, 10, 2, 1, 2, 3), OFFSET_PTWO), FormatStyle.SHORT, "02/10/2001, 01:02 +02:00"},
    };
}
 
Example #30
Source File: TCKFormatStyle.java    From hottub with GNU General Public License v2.0 5 votes vote down vote up
@Test(dataProvider = "formatStyle")
public void test_formatStyle(Temporal temporal, FormatStyle style, String formattedStr) {
    DateTimeFormatterBuilder builder = new DateTimeFormatterBuilder();
    DateTimeFormatter formatter = builder.appendLocalized(style, style).appendLiteral(" ").appendZoneOrOffsetId().toFormatter();
    formatter = formatter.withLocale(Locale.US);
    assertEquals(formatter.format(temporal), formattedStr);
}