使用LocalDate将一个日期更改为另一种日期格式 [英] Changing one date to another date format using LocalDate

查看:173
本文介绍了使用LocalDate将一个日期更改为另一种日期格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将以下输入作为

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

我想将所有这些日期转换为"yyyy-MM-dd"格式

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

当前,我正在使用

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);
    }
}

,但"simpleDateFormat.parse()"将使用时区进行转换并给我日期.转换时我不希望时区.我想直接将一种日期格式转换为其他日期格式.我正在探索LocalDate作为Java 8功能.但是如果我尝试的话就会失败

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);

请帮助我使用LocalDate和DateTimeFormatter.

please help me with LocalDate and DateTimeFormatter.

好吧,我对键入Map示例不利,这是我实际使用的Map进入程序

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

我猜正在识别并给我这张地图的人正在使用SimpleDate格式化程序,因为我假设SimpleDateFormatter可以将日期"8 12 2019"标识为"MM dd yy"或"M dd yyyy"或"MM d yy"或"MM d yyyy"....

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"....

但是"LocalDate"非常严格,它不是解析日期

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?

推荐答案

是正确的,解析时旧且麻烦的 SimpleDateFormat 并没有特别注意格式中模式字母的数量模式字符串. DateTimeFormatter 确实如此,这通常是一个优势,因为它可以更好地验证字符串. MM 的月份需要两位数字. yy 需要两位数的年份(例如19代表2019).由于您需要能够解析一位数字的月份,月份的某天和一年的四位数的年份,因此我建议我们修改格式模式字符串以准确地告诉 DateTimeFormatter .我正在将 MM 更改为 M ,将 dd 更改为 d ,将 yy 更改为 y .这将使 DateTimeFormatter 不必担心位数(一个字母基本上表示至少 个位数).

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);
    }

此代码段的输出为:

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

这篇关于使用LocalDate将一个日期更改为另一种日期格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆