Changing one date to another date format using LocalDate

若如初见. 提交于 2021-02-10 12:11:20

问题


I have the following input as a

Map<String,String>

1) MM dd yyyy = 08 10 2019
2) dd MM yyyy = 10 05 2019
3) dd MM yyyy = 05 10 2008
4) yyyy dd MM =  2001 24 01

I want to convert all this dates to "yyyy-MM-dd" format

Currently, i am using

for (String eachFormat : formats) {
    SimpleDateFormat simpleDateFormat = new SimpleDateFormat(eachFormat);
    try {
        SimpleDateFormat targetFormat = new SimpleDateFormat("yyyy-MM-dd");
        Date inputDate = simpleDateFormat.parse(parsedDate.get(eachFormat));
        return targetFormat.format(inputDate);
    } catch (ParseException e) {
        LOGGER.error(e);
    }
}

but the "simpleDateFormat.parse()" will convert and give me date using timezone. I don't want timezone while converting. I want to straight up convert one date format to other. I am exploring LocalDate as java 8 feature. But it is failing if i try

DateTimeFormatter target = DateTimeFormatter.ofPattern(eachFormat);
LocalDate localDate = LocalDate.parse(parsedDate.get(eachFormat),target);

please help me with LocalDate and DateTimeFormatter.

Edit 1: ok, my bad for the typing Map example, here's the actually Map i am getting in program

1) MM dd yy = 8 12 2019
2) dd MM yy = 4 5 2007
3) yy dd MM = 2001 10 8

I guess the person who is identifying and giving me this map is using SimpleDate formatter, because i assume SimpleDateFormatter can identify the date "8 12 2019" as "MM dd yy" or "M dd yyyy" or "MM d yy" or "MM d yyyy"....

but "LocalDate" is very strict it is not parsing date

"8 12 2019" for "dd MM yy"

It strictly parse if and only if format of date

"8 12 2019" is "d MM yyyy"

...now what should i do?


回答1:


It’s correct, the old and troublesome SimpleDateFormat when parsing generally didn’t pay much attention to the number of pattern letters in the format pattern string. DateTimeFormatter does, which is generally an advantage since it allows for a much better validation of the string. MM requires two digits for month. yy requires a two-digit year (like 19 for 2019). Since you need to be able to parse one digit month and day of month and four digit year, I suggest that we modify the format pattern string to tell DateTimeFormatter exactly that. I am changing MM to M, dd to d and yy to y. This will cause DateTimeFormatter not to worry about the number of digits (one letter basically means at least one digit).

    Map<String, String> formattedDates = Map.of(
            "MM dd yy", "8 12 2019",
            "dd MM yy", "4 5 2007",
            "yy dd MM", "2001 10 8");

    for (Map.Entry<String, String> e : formattedDates.entrySet()) {
        String formatPattern = e.getKey();
        // Allow any number of digits for each of year, month and day of month
        formatPattern = formatPattern.replaceFirst("y+", "y")
                .replace("dd", "d")
                .replace("MM", "M");
        DateTimeFormatter sourceFormatter = DateTimeFormatter.ofPattern(formatPattern);
        LocalDate date = LocalDate.parse(e.getValue(), sourceFormatter);
        System.out.format("%-11s was parsed into %s%n", e.getValue(), date);
    }

Output from this snippet is:

8 12 2019   was parsed into 2019-08-12
4 5 2007    was parsed into 2007-05-04
2001 10 8   was parsed into 2001-08-10


来源:https://stackoverflow.com/questions/57791601/changing-one-date-to-another-date-format-using-localdate

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!