【问题标题】:Parsing String into date using java [duplicate]使用java将字符串解析为日期[重复]
【发布时间】:2017-03-09 10:53:13
【问题描述】:

我正在尝试使用以下代码将字符串解析为日期:

public static Date dateFormatter(String s)
{
    SimpleDateFormat ft = new SimpleDateFormat ("MMddYYYY"); 
    Date excelDate=null;
    try
    {
        excelDate = ft.parse(s);    
        Date formatString = ft.format(excelDate);
        System.out.println("Date to be printed in Excel is :" +formatString);
        return excelDate;
    }
    catch(Exception ae)
    {
        System.out.println("No date");
    }
    return excelDate;
}

我正在传递参数"04202017"

这个功能对我不起作用。我无法弄清楚我做错了什么。有人可以帮帮我吗?

【问题讨论】:

  • 试试"MMddyyyy"
  • format 不是parse
  • 为什么要格式化解析结果? (输入错误的类型)

标签: java


【解决方案1】:

您必须使用ft.parse(s); 而不是format(excelDate)。格式是另一种方式(Date -> String

DateFormat.parse(String)

而且您不必将Date 解析回String

更正的代码:

public static Date dateFormatter(String s) {
    SimpleDateFormat ft = new SimpleDateFormat ("MMddYYYY"); 
    Date excelDate = null;
    try {
        excelDate = ft.parse(s);    
        System.out.println("Date to be printed in Excel is :" +excelDate);
        return excelDate;
    } catch(Exception ae) {
        System.out.println("No date");
    }
    return excelDate;
}

【讨论】:

  • YYYY 不是我认为的有效年份。它应该是 MMddyyyy 或类似的东西。
  • OP 已经在这样做了,但他正试图将excelDate 重新格式化为日期(而不是字符串)。所以这个答案不正确(还;)) PS:你必须使用ft.parse(excelDate); ,你的意思是parse(s)?因为您不能将日期传递给parse
  • @AxelH 我更正了我的答案
  • 是的,我正在尝试将字符串 04202017 重新格式化为 2017-04-20.. 我正在尝试编写一个函数,该函数将字符串作为参数并以 YYYY-MM- 格式返回日期DD
【解决方案2】:

您已经使用所需的日期格式将 String 解析为 excelDate。所以我认为只打印excelDate就足够了。

System.out.println("Date to be printed in Excel is :" +excelDate);

这样。

并将MMddYYYY 更改为MMddyyyy

【讨论】:

    【解决方案3】:

    尝试parse 方法而不是format

    对于迄今为止的字符串,使用:

    SimpleDateFormat.parse(String);
    

    对于日期到字符串,使用:

    SimpleDateFormat.format(date);
    

    但是,在您的代码中,您已经解析了字符串并在这一行分配给excelDate

    excelDate = ft.parse(s);
    

    【讨论】:

      【解决方案4】:

      试试这个:

          String string = "march 9, 2017";
          DateFormat format = new SimpleDateFormat("MMMM d, yyyy", Locale.ENGLISH);
          Date date = format.parse(string);
          System.out.println(date); 
      

      【讨论】:

      • 为什么要更改格式?但是正确
      • 我只是举例说明如何将字符串格式转换为日期,感谢您的反馈@AxelH
      • 我同意,但由于他的模式有错误,他可能难以理解您的答案,因为它使用了更复杂的模式(MMMM 部分)。
      • 我想要格式为 YYYY-MM-DD @Anantham .. 感谢您的指导 :)
      【解决方案5】:

      最好使用 Java 1.8 的新时间类(在 java.time.* 包中)。

      public static void main(String[] args)
      {        
          LocalDateTime dateTime = LocalDateTime.now();
          DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
      
          // To String
          String dateString = dateTime.format(formatter);
          System.out.println(dateString);
      
          // To LocalDateTime
          LocalDateTime parsedLocalDateTime = LocalDateTime.parse(dateString, formatter);
      }
      

      【讨论】:

      • 这只是格式化当前日期,您应该在答案中包含将字符串解析为LocalDateTime 的方法才能正确。因为这是相反的;)
      • 哦,是的,你是对的。我已经编辑过了。
      猜你喜欢
      • 1970-01-01
      • 2013-01-21
      • 1970-01-01
      • 2012-06-10
      • 2013-06-05
      • 1970-01-01
      • 1970-01-01
      • 2012-10-21
      • 1970-01-01
      相关资源
      最近更新 更多