繁体   English   中英

java.text.parseException:无法解析的日期

[英]java.text.parseException:unparsable date

我在尝试将字符串转换为日期时遇到此错误。 不可参数的数据下面是我的代码:

String str = "hello"                

在解析String str缺少第二个。 因此,要解析它,您不应在SimpleDateFormat pattern包含第二种格式。 还要更正日期和月份格式。 看一下df的声明

SimpleDateFormat df = new SimpleDateFormat("dd-MMM-yyyy hh:mm a");//Remove :ss

要了解模式的详细信息,请阅读此文档

编辑

String date2 = sdformatter.format(date1);// format method return String.
                                         //Should declare with String

完整代码

    String str = "25-Nov-2013 06:00 AM";
    SimpleDateFormat df = new SimpleDateFormat("dd-MMM-yyyy hh:mm a");//Remove :ss
    SimpleDateFormat sdformatter = new SimpleDateFormat("MM-dd-yyyy hh:mm:ss a");
    Date date1=null;
    try {
        date1 = df.parse(str);
    } catch (ParseException ex) {
        ex.printStackTrace();
    }
    String date2 = sdformatter.format(date1);
    System.out.println(date2);

根据str格式,您应该编写SimpleDateFormat,

(25-Nov-2013 06:00 AM ---> dd-MMM-yyyy hh:mm a) and for 
(25-Nov-2013 06:00:30 AM-----> dd-MMM-yyyy hh:mm:ss a)  

将工作

尝试这个

long newerdate = new Date().parse("25-Nov-2013 06:30 AM");
java.text.SimpleDateFormat df = new java.text.SimpleDateFormat("dd-MM-yyyy hh:mm a");
String data = df.format(newerdate);
System.out.println(data);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM