Skip to content
Permalink
Browse files
8260468: Wrong behavior of LocalDateTimeStringConverter
Reviewed-by: kcr, pbansal
  • Loading branch information
nlisker committed Feb 23, 2021
1 parent 9e42eea commit e25d39b0dac302d304fa2d206cabbbd6609836cb
@@ -208,11 +208,11 @@ public LocalDateTimeStringConverter(FormatStyle dateStyle, FormatStyle timeStyle
TemporalAccessor temporal = parser.parse(text);

if (type == LocalDate.class) {
return (T)LocalDate.from(chronology.date(temporal));
return (T) LocalDate.from(temporal);
} else if (type == LocalTime.class) {
return (T)LocalTime.from(temporal);
return (T) LocalTime.from(temporal);
} else {
return (T)LocalDateTime.from(chronology.localDateTime(temporal));
return (T) LocalDateTime.from(temporal);
}
}

@@ -228,29 +228,7 @@ public LocalDateTimeStringConverter(FormatStyle dateStyle, FormatStyle timeStyle
formatter = getDefaultFormatter();
}

if (value instanceof LocalDate) {
ChronoLocalDate cDate;
try {
cDate = chronology.date(value);
} catch (DateTimeException ex) {
Logging.getLogger().warning("Converting LocalDate " + value + " to " + chronology + " failed, falling back to IsoChronology.", ex);
chronology = IsoChronology.INSTANCE;
cDate = (LocalDate)value;
}
return formatter.format(cDate);
} else if (value instanceof LocalDateTime) {
ChronoLocalDateTime<? extends ChronoLocalDate> cDateTime;
try {
cDateTime = chronology.localDateTime(value);
} catch (DateTimeException ex) {
Logging.getLogger().warning("Converting LocalDateTime " + value + " to " + chronology + " failed, falling back to IsoChronology.", ex);
chronology = IsoChronology.INSTANCE;
cDateTime = (LocalDateTime)value;
}
return formatter.format(cDateTime);
} else {
return formatter.format(value);
}
return formatter.format(value);
}


@@ -25,9 +25,10 @@

package test.javafx.util.converter;

import java.time.DateTimeException;
import java.time.LocalDateTime;
import java.time.chrono.Chronology;
import java.time.chrono.IsoChronology;
import java.time.chrono.JapaneseChronology;
import java.time.format.DateTimeFormatter;
import java.time.format.FormatStyle;
import java.util.Arrays;
@@ -36,20 +37,20 @@
import static org.junit.Assert.*;

import javafx.util.StringConverter;
import javafx.util.converter.LocalTimeStringConverterShim;
import javafx.util.converter.LocalDateTimeStringConverter;
import javafx.util.converter.LocalDateTimeStringConverterShim;

import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.Ignore;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;

/**
*/
@RunWith(Parameterized.class)
public class LocalDateTimeStringConverterTest {

private static final String JAPANESE_DATE_STRING = "Saturday, January 12, 60 Shōwa, 12:34:56 PM";
private static final LocalDateTime VALID_LDT_WITH_SECONDS = LocalDateTime.of(1985, 1, 12, 12, 34, 56);
private static final LocalDateTime VALID_LDT_WITHOUT_SECONDS = LocalDateTime.of(1985, 1, 12, 12, 34, 0);

@@ -155,4 +156,15 @@ public void fromString_testInvalidInput() {
assertEquals("12 January 1985, 12:34:56", converter.toString(VALID_LDT_WITH_SECONDS));
assertEquals(VALID_LDT_WITH_SECONDS, converter.fromString("12 January 1985, 12:34:56"));
}

@Test
public void testChronologyConsistency() {
var converter = new LocalDateTimeStringConverter(FormatStyle.FULL, FormatStyle.MEDIUM, null, JapaneseChronology.INSTANCE);
assertEquals(JAPANESE_DATE_STRING, converter.toString(VALID_LDT_WITH_SECONDS));
// force a chronology change with an invalid Japanese date
try {
converter.toString(LocalDateTime.of(1, 1, 1, 1, 1, 1));
} catch (DateTimeException e) {}
assertEquals(VALID_LDT_WITH_SECONDS, converter.fromString(JAPANESE_DATE_STRING));
}
}

1 comment on commit e25d39b

@openjdk-notifier

This comment has been minimized.

Copy link

@openjdk-notifier openjdk-notifier bot commented on e25d39b Feb 23, 2021

Please sign in to comment.