I am upgrading a Groovy application but have run i...
# groovy
t
I am upgrading a Groovy application but have run into an issue with date parsing, the following code parsing a danish date doesn't work after upgrading from Java 8 -> 11+
Copy code
Date.parse('yyyy-MMM', '2011-Okt')

java.text.ParseException: Unparseable date: "2011-Okt"
Java changed date parsing in Java 9, I have found answers suggesting setting
-Djava.locale.providers=COMPAT,SPI,CLDR
should bring back support for Java 8 formats - but it doesn't seam to work... Will I have to rewrite the code, or is there a JVM setting I am missing?
j
I actually get the same error in the Groovy console, even using Java 8.
Copy code
println System.getProperty("java.version")
Date.parse('yyyy-MMM', '2011-Okt')
puts out:
Copy code
1.8.0_412
Exception thrown

java.text.ParseException: Unparseable date: "2011-Okt"
	at ConsoleScript2.run(ConsoleScript2:2)
I reckon you'll be best off using LocalDate. Something like:
Copy code
import java.time.LocalDate
import java.time.format.DateTimeFormatter
import java.time.format.DateTimeFormatterBuilder
import java.time.format.ResolverStyle
import java.time.temporal.ChronoField

Locale da = Locale.forLanguageTag('da-DK')

DateTimeFormatter ymDa = new DateTimeFormatterBuilder()
        .parseCaseInsensitive()
        .appendPattern('uuuu')
        .optionalStart()
        .appendLiteral('-')
        .appendPattern('MMM')
        .optionalEnd()
        .parseDefaulting(ChronoField.MONTH_OF_YEAR, 1)
        .parseDefaulting(ChronoField.DAY_OF_MONTH, 1)
        .toFormatter(da)
        .withResolverStyle(ResolverStyle.STRICT)

LocalDate.parse('2011-okt.', ymDa)
Though I think (based on some searching and LLM bashing) that
2011-Okt
might actually be a German locale format, not a Danish one.
t
I ended op rewriting it using DateTimeFormatter and LocalDate
👍 1