【问题标题】:Getting the date as per the format in which month name is coming as first根据月份名称首先出现的格式获取日期
【发布时间】:2018-09-11 12:40:44
【问题描述】:

我得到如下所示格式的字符串

03-12-2018

我想根据 Java 8 标准将其转换为以下格式,请告知

December 03 , 2018  

我尝试过的如下所示,但我没有成功,请告知如何实现同样的效果

SimpleDateFormat month_date = new SimpleDateFormat("MMM yyyy", Locale.ENGLISH);
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

    String actualDate = "03-12-2018";

    Date date = sdf.parse(actualDate);

    String month_name = month_date.format(date);
    System.out.println("Month :" + month_name);  

【问题讨论】:

  • 请解释“不成功”。
  • 我的程序输出结果没有成功
  • @user1493927 我认为他的意思是你应该复制粘贴你得到的输出,这样我们也可以看到它。请注意,yyyy-MM-dd 的格式似乎与@987654326 中的格式不匹配@
  • 我建议你避免使用SimpleDateFormat 类。它不仅过时了,而且出了名的麻烦。今天我们在java.time, the modern Java date and time API 中做得更好。
  • @ScottHunter 的意思(我认为)是您应该准确地告诉我们您观察到的结果与预期的December 03 , 2018 有何不同。请粘贴您的输出,包括您可能看到的任何错误消息或堆栈跟踪。我们可以从那里更好地帮助您,所以这是为了您自己的利益(而且因为坦率地说,尝试回答我们必须猜测的问题并不好玩)。

标签: java date simpledateformat


【解决方案1】:

java.time

    DateTimeFormatter originalFormatter = DateTimeFormatter.ofPattern("dd-MM-uuuu");
    DateTimeFormatter monthFirst = DateTimeFormatter
            .ofLocalizedDate(FormatStyle.LONG)
            .withLocale(Locale.ENGLISH);

    String actualDate = "03-12-2018";
    LocalDate date = LocalDate.parse(actualDate, originalFormatter);
    String monthName = date.format(monthFirst);
    System.out.println("Month :" + monthName);

输出:

月份:2018 年 12 月 3 日

由于您使用的是 Java 8(即使您没有使用),请避免使用早已过时且臭名昭著的麻烦 SimpleDateFormat 类。尽可能使用内置格式,而不是自己滚动。

你的代码出了什么问题?

您解析了格式为yyyy-MM-dd 的03-12-2018 字符串。因此,这会解析为公元 3 年(2015 年前)第 12 个月的 2018 日。 12 月显然没有 2018 年的日子。所以期待一个例外是公平的。这只是SimpleDateFormat 麻烦的地方之一:在标准设置下,它只是在接下来的几个月和几年中一直计算天数,并在 9 年的 6 月 9 日结束,即 5 年半之后。接下来,您使用包含月份名称和年份的格式化程序格式化此日期,您似乎忘记了月份中的哪一天。无论如何,它打印为Jun 0009(您应该在问题中告诉我们,以便我们可以看到问题所在;这些信息对于尝试解决您的问题非常有帮助)。

链接

Oracle tutorial: Date Time 解释如何使用java.time。

【讨论】:

    【解决方案2】:

    只需选择正确的格式(并应用正确的Locale):

     DateTimeFormatter f = DateTimeFormatter.ofPattern("LLLL dd, yyyy");
     System.out.println(f.format(yourDate));
    

    顺便说一句,它在the documentation:

    ... 数字/文本:如果模式字母的数量为 3 或更多,请使用上面的文本规则。否则使用上面的数字规则。 ...

    【讨论】:

      【解决方案3】:

      使用以下代码:

      SimpleDateFormat month_date = new SimpleDateFormat("MMMM dd, yyyy", Locale.ENGLISH);
      SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy");
      
      String actualDate = "03-12-2018";
      
      Date date = sdf.parse(actualDate);
      
      String month_name = month_date.format(date);
      System.out.println("Month :" + month_name);
      

      【讨论】:

      • 您正在使用多年前被 java.time 类取代的糟糕类。
      • 我刚刚修改了提问者提供的代码来解决他的问题。
      • 您的答案和问题中使用的那些类本身就是问题的一部分。 (A) 问题试图表示仅日期值,但使用的是日期时间类:java.util.Date。所以这是数据类型的不匹配。 (B)添加时区的复杂问题,因为java.util.Date 类是UTC,但这一事实被您的代码和问题代码所掩盖。正确的最佳解决方案显示在 the Answer by Ole V.V. 中,显示了正确的用途:java.time.LocalDate(没有区域和时间)。
      猜你喜欢
      • 1970-01-01
      • 2020-12-27
      • 1970-01-01
      • 2010-12-11
      • 2021-08-21
      • 1970-01-01
      • 1970-01-01
      • 2017-05-26
      相关资源
      最近更新 更多