【问题标题】:How can I format a String, which is a date, to a new date format in Java? [duplicate]如何在 Java 中将作为日期的字符串格式化为新的日期格式? [复制]
【发布时间】:2018-06-19 22:47:21
【问题描述】:

我会直截了当,我的目标如此改变

2018-05-24

进入

2018 年 5 月 24 日。

这是我当前的代码:

    String content = "2018-05-24";
    SimpleDateFormat old = new SimpleDateFormat("yyyy-mm-dd");
    Date oldDate = old.parse(content);
    SimpleDateFormat current = new SimpleDateFormat("MMMM dd, yyyy.");
    String newDate = current.format(oldDate);
    System.out.println(newDate);

但运行会产生:

2018 年 1 月 24 日。

我不知道为什么这会告诉我一月。我知道我错了,我在 StackOverFlow 上搜索过类似的问题,但也许我做的不对。有什么建议吗?

【问题讨论】:

    标签: java date simpledateformat


    【解决方案1】:

    这是因为你在一个月内使用mm,而它应该是MM

    SimpleDateFormat old = new SimpleDateFormat("yyyy-MM-dd");
    

    这个输出:

    May 24, 2018.
    

    【讨论】:

    • 谢谢。代表我犯了这么愚蠢的错误。
    猜你喜欢
    • 2022-01-09
    • 1970-01-01
    • 2013-09-14
    • 2018-06-01
    • 1970-01-01
    • 2012-06-18
    • 2022-07-04
    • 2016-05-17
    • 1970-01-01
    相关资源
    最近更新 更多