I am looking for a solution that lets me turn a string containing an iso basic date format, such as 20190330
into a formatted date such as 2019-03-30
(Eur format) or 30/03/2019
(UK format) or 03/30/2019
(US format). The format depends on the format that the user selects.
The libraries java.time.LocalDate
and java.time.format.DateTimeFormatter seem to offer possibilities of a solution but I find the documentation of these libraries incoherent and confusing and I’m not sure if a solution is possible from these sources.
I have tried the four solutions contained in the return statements shown in the code.
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
public class DateFormatter {
private static final String EU_PATTERN = "yyyy-MM-dd";
private static final String UK_PATTERN= "dd/MM/yyyy";
private static final String US_PATTERN= "MM/dd/yyyy";
private static final int EU = 1;
private static final int UK = 2;
private static final int US = 3;
private static String formattedDate(int fmt, String isobasic){
String pattern = null;
switch(fmt) {
case EU: {pattern = EU_PATTERN; break;}
case UK: {pattern = UK_PATTERN; break;}
case US: {pattern = US_PATTERN; break;}
}
DateTimeFormatter dateFormatter = DateTimeFormatter.ofPattern(pattern);
// return dateFormatter.format(isobasic); // String cannot be converted to Temporal Accessor
// return (String)dateFormatter.format(isobasic); // String cannot be converted to Temporal Accessor
// return LocalDate.parse(isobasic, dateFormatter); // LocalDate cannot be converted to String
return (String)LocalDate.parse(isobasic, dateFormatter); // LocalDate cannot be converted to String
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
System.out.println(formattedDate(EU, "20180927"));
System.out.println(formattedDate(UK, "20190131"));
System.out.println(formattedDate(US, "20171225"));
}
The expected result would be three formatted dates according tp the format selected: 2018-09-27 31/01/2019 12/25/2017 The actual results are the syntax errors shown after the return codes following the case statement.
My workaround is to rewrite the case statement and use clumsy statements like the following (for the EU format):
case EU: { return isobasic.substring(0,4) + "-" + isobasic.substring(4,6) + "-" + isobasic.substring(6,8);}
If that’s the only solution I will live with that but I want to check out first of the formatter/localdate libraries offer a more elegant approach.
Following my comment, you should try using your iso format to get a TemporalAccessor from your String and then format it to the desired pattern.
I just made minimal changes in your code to achieve the result, you can try it
public class DateFormatter {
private static final String ISO = "yyyyMMdd";
private static final String EU_PATTERN = "yyyy-MM-dd";
private static final String UK_PATTERN = "dd/MM/yyyy";
private static final String US_PATTERN = "MM/dd/yyyy";
private static final int EU = 1;
private static final int UK = 2;
private static final int US = 3;
private static String formattedDate(int fmt, String isobasic) {
String pattern = null;
switch (fmt) {
case EU: {
pattern = EU_PATTERN;
break;
}
case UK: {
pattern = UK_PATTERN;
break;
}
case US: {
pattern = US_PATTERN;
break;
}
}
DateTimeFormatter parser = DateTimeFormatter.ofPattern(ISO);
TemporalAccessor parsedDate = parser.parse(isobasic);
return DateTimeFormatter.ofPattern(pattern).format(parsedDate);
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
System.out.println(formattedDate(EU, "20180927"));
System.out.println(formattedDate(UK, "20190131"));
System.out.println(formattedDate(US, "20171225"));
}
}