有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

Java SimpleDataFormat异常:不可解析的日期

我试图让Java读入如下格式的日期: 2012年3月8日星期四13:33:25 但是有一个不可原谅的例外。以下是代码:

SimpleDateFormat formatter2 = new SimpleDateFormat("EEE MMM d HH:mm:ss yyyy", Locale.ENGLISH);
String currentDate = "Thu Mar 8 13:33:25 2012";
Date date2 = formatter.parse(currentDate);

它抛出以下异常:Exception in thread "main" java.text.ParseException: Unparseable date: "Thu Mar 8 13:33:25 2012"

谁能帮帮我吗?我试着把“d”改成“dd”,但还是不起作用


共 (2) 个答案

  1. # 1 楼答案

    您没有使用示例中指定的formatter2变量

    您的代码在使用formatter2时有效

    SimpleDateFormat formatter2 = new SimpleDateFormat("EEE MMM d HH:mm:ss yyyy", Locale.ENGLISH);
    String currentDate = "Thu Mar 8 13:33:25 2012";
    Date date2 = formatter2.parse(currentDate);
    System.out.println(date2);
    

    结果:

    Thu Mar 08 13:33:25 CAT 2012

  2. # 2 楼答案

    String pattern = "EEEEE MMMMM yyyy HH:mm:ss.SSSZ";
    SimpleDateFormat simpleDateFormat =
            new SimpleDateFormat(pattern, new Locale("en", "IN"));
    
    String date = simpleDateFormat.format(new Date());
    System.out.println(date);