【问题标题】:Parse a Date of MMMM YYYY format解析 MMMM YYYY 格式的日期
【发布时间】:2015-10-22 11:27:20
【问题描述】:

我必须将日期字符串(例如“2015 年 10 月”)解析为日期。 所以问题是:如何解析MMMM yyyy 格式的日期?如果新的 Date 对象是给定月份的第一个月,则可以。

我试过了:

DateTimeFormatter formatter = new DateTimeFormatterBuilder().appendPattern("MMMM yyyy").toFormatter();
TemporalAccessor ta = formatter.parse(node.textValue());
Instant instant = LocalDate.from(ta).atStartOfDay().atZone(ZoneId.systemDefault()).toInstant();
Date d = Date.from(instant);

但由于缺少这一天,它不起作用。

【问题讨论】:

  • @GaëlJ 请不要教年轻人使用早已过时且臭名昭著的麻烦 SimpleDateFormat 类。尤其是当他们已经在使用 java.time(现代 Java 日期和时间 API)时,就像在这个问题中一样。好多了。

标签: java date format


【解决方案1】:

你所拥有的是YearMonth,而不是LocalDate,因为这一天不见了。

以下作品:

DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MMMM yyyy", Locale.ENGLISH);
YearMonth yearMonth = formatter.parse("October 2015", YearMonth::from);
LocalDate date = yearMonth.atDay(1);

System.out.println(yearMonth); // prints "2015-10"
System.out.println(date);      // prints "2015-10-01"

如果您想将其作为java.util.Date,您需要指定您所指的时区,可能是UTC 或系统默认值?

// ZoneId zone = ZoneOffset.UTC;
ZoneId zone = ZoneId.systemDefault();
Date javaUtilDate = Date.from(date.atStartOfDay(zone).toInstant());

System.out.println(javaUtilDate); // prints "Thu Oct 01 00:00:00 CEST 2015"
                                  // because i'm in Europe/Stockholm.

【讨论】:

    【解决方案2】:

    这个怎么样

    DateFormat format = new SimpleDateFormat("MMMM yyyy", Locale.ENGLISH);
    Date date = format.parse("October 2015");
    System.out.println(date); // Prints Thu Oct 01 00:00:00 BST 2015
    

    【讨论】:

    • 请注意,这使用系统默认时区,在这种情况下恰好是“BST”。
    • 请不要教年轻人使用早已过时且臭名昭著的SimpleDateFormat类。尤其是当他们已经在使用 java.time(现代 Java 日期和时间 API)时,就像在这个问题中一样。好多了。
    【解决方案3】:

    对于 java 8

        DateTimeFormatter formatter = new DateTimeFormatterBuilder()
            .appendPattern("MMMM yyyy")
            .toFormatter(Locale.US);
        TemporalAccessor ta = formatter.parse("October 2015");
        YearMonth ym = YearMonth.from(ta);
        LocalDateTime dt = LocalDateTime.of(ym.getYear(), ym.getMonthValue(),
            1, 0, 0, 0);
        Instant instant = Instant.from(dt.atZone(ZoneId.systemDefault()));
    
        Date d = Date.from(instant);
    

    【讨论】:

      【解决方案4】:

      您可以为此使用SimpleDateFormatparse 方法

      private static final SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("MMMMM yyyy"); //MMMM yyyy example: October 2015
      
      public Date getDateFromString(String input) {
          return DATE_FORMAT.parse(input);
      }
      

      欲了解更多信息,请参阅:http://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html#parse(java.lang.String,%20java.text.ParsePosition)

      格式说明: MMMM 表示您正在解析全名月份,例如“October” yyyy 表示您有 4 位数的长度年份。例如,如果您想解析 10 月 15 日,您的格式将如下所示:“MMMM yy”

      【讨论】:

      • 与 Java 捆绑在一起的旧日期时间类(例如 SimpleTextFormat)是出了名的麻烦,在设计和实现上都存在缺陷。是时候继续使用 Java 8 及更高版本中内置的新 java.time 框架了。 Sun、Oracle 和本问题的作者都有。
      【解决方案5】:

      为什么不使用 SimpleDateFormatter? 这对我来说很好:

      SimpleDateFormat formatter = new SimpleDateFormat("MMMM yyyy");
      Date d = formatter.parse("Oktober 2015");
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-01-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-05-20
        • 1970-01-01
        相关资源
        最近更新 更多