【问题标题】:converting date from one format to another does not work将日期从一种格式转换为另一种格式不起作用
【发布时间】:2015-01-06 19:24:34
【问题描述】:

我有一个使用这种格式的日期:Sun Oct 19 01:00:00 ADT 2014

我需要将其转换为以下格式但我不能,

这是我的代码:

    // The test string
    String str = "Sun Oct 19 01:00:00 ADT 2014";

    // Formatter for the input date
    final DateTimeFormatter inputFormat = 
    DateTimeFormatter.ofPattern("EEE MMM dd HH:mm:ss zzz yyyy");

   // The parsed date
   final LocalDateTime parsed = LocalDateTime.parse(str, inputFormat);

       // The output format(s). Specify the one you need
    final DateTimeFormatter outputFormat1 = DateTimeFormatter.ofPattern("yyyy/MM/dd");
     final DateTimeFormatter outputFormat2 = DateTimeFormatter.ofPattern("yyyy-MM-dd");

  // Print
    System.out.println(outputFormat1.format(parsed)); // -> 2014/10/21
      System.out.println(outputFormat2.format(parsed)); // -> 2014-10-21

但它根本不起作用,我从早上开始就被这个问题困扰着,真的不知道如何解决它,有人知道吗?

【问题讨论】:

  • 究竟是什么不工作?你得到什么错误?你得到什么输出,你期望什么输出?
  • 你的输入字符串是一个完全指定的时间点,包括时区,那你为什么要用LocalDateTime来解析呢?
  • 您似乎正在测试 2014 年 10 月 19 日是否转换为 2014-10-21。每月 19 日不是每月 21 日。此外,正如@forgivenson 指出的那样,您可能应该使用ZonedDateTime 而不是LocalDateTime.
  • 这是我在线程“main”java.time.DateTimeException 中得到异常的错误:无法提取值:java.time.format.DateTimePrintContext.getValue(DateTimePrintContext) 的类 java.time.LocalDateTime .java:282) 在 java.time.format.DateTimeFormatterBuilder$ZoneTextPrinterParser.format(DateTimeFormatterBuilder.java:3685) 在 java.time.format.DateTimeFormatterBuilder$CompositePrinterParser.format(DateTimeFormatterBuilder.java:2182) 在 java.time.format。 DateTimeFormatter.formatTo(DateTimeFormatter.java:1744) at java.time.format.DateTimeFormatter.format(DateTimeFormatter.java:1718)

标签: java


【解决方案1】:

使用此代码有什么问题。它适用于 19-th

    // The test string
    String str = "Sun Oct 19 01:00:00 ADT 2014";

    // Formatter for the input date
    final DateFormat inputFormat = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy");
    // The parsed date
    final Date parsed = inputFormat.parse(str);

    // The output format(s). Specify the one you need
    final DateFormat outputFormat1 = new SimpleDateFormat("yyyy/MM/dd");
    final DateFormat outputFormat2 = new SimpleDateFormat("yyyy-MM-dd");

    // Print
    System.out.println(outputFormat1.format(parsed)); // -> 2014/10/21
    System.out.println(outputFormat2.format(parsed)); // -> 2014-10-21

【讨论】:

    【解决方案2】:

    我希望您使用的是 JDK8,并且您的导入如下:

    import java.time.LocalDateTime;
    import java.time.format.DateTimeFormatter;
    

    然后是下面的代码:

    import java.time.LocalDateTime;
    import java.time.format.DateTimeFormatter;
    
    public class Main {
    
        public static void main(String[] args) {
            // The test string
            String str = "Sun Oct 19 01:00:00 ADT 2014";
    
            // Formatter for the input date
            final DateTimeFormatter inputFormat =
                    DateTimeFormatter.ofPattern("EEE MMM dd HH:mm:ss zzz yyyy");
    
            // The parsed date
            final LocalDateTime parsed = LocalDateTime.parse(str, inputFormat);
    
            // The output format(s). Specify the one you need
            final DateTimeFormatter outputFormat1 = DateTimeFormatter.ofPattern("yyyy/MM/dd");
            final DateTimeFormatter outputFormat2 = DateTimeFormatter.ofPattern("yyyy-MM-dd");
    
            // Print
            System.out.println(outputFormat1.format(parsed)); // -> 2014/10/21
            System.out.println(outputFormat2.format(parsed)); // -> 2014-10-21
        }
    }
    

    按预期打印:

    2014/10/19
    2014-10-19
    

    如果您想打印2014/10/21,只需将str 更改为:

    String str = "Tue Oct 21 01:00:00 ADT 2014";
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-04-23
      • 1970-01-01
      • 2011-11-23
      • 1970-01-01
      • 1970-01-01
      • 2017-08-27
      相关资源
      最近更新 更多