Java: unparseable date exception

2019-01-05 04:15发布

While trying to transform the date format I get an exception:unparseable date and don't know how to fix this problem.

I am receiving a string which represents an event date and would like to display this date in different format in GUI.

What I was trying to do is the following:

private String modifyDateLayout(String inputDate){
        try {
            //inputDate = "2010-01-04 01:32:27 UTC";
            Date date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss z").parse(inputDate);
            return new SimpleDateFormat("dd.MM.yyyy HH:mm:ss").format(date);
        } catch (ParseException e) {
            e.printStackTrace();
            return "15.01.2010";
        }
    }

Anyway the line

String modifiedDateString = originalDate.toString();

is dummy. I would like to get a date string in the following format:

dd.MM.yyyy HH:mm:ss

and the input String example is the following:

2010-01-04 01:32:27 UTC

Does anyone know how to convert the example date (String) above into a String format dd.MM.yyyy HH:mm:ss?

Thank you!

Edit: I fixed the wrong input date format but still it doesn't work. Above is the pasted method and below is the screen image from debugging session.

alt text http://img683.imageshack.us/img683/193/dateproblem.png

#Update I ran

String[] timezones = TimeZone.getAvailableIDs();

and there is UTC String in the array. It's a strange problem.

I did a dirty hack that works:

private String modifyDateLayout(String inputDate){
    try {
        inputDate = inputDate.replace(" UTC", "");
        Date date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(inputDate);
        return new SimpleDateFormat("dd.MM.yyyy HH:mm:ss").format(date);
    } catch (ParseException e) {
        e.printStackTrace();
        return "15.01.2010";
    }
}

But still I would prefer to transform the original input without cutting timezone away.

This code is written for Android phone using JDK 1.6.

标签: java date format
1条回答
神经病院院长
2楼-- · 2019-01-05 04:45

What you're basically doing here is relying on Date#toString() which already has a fixed pattern. To convert a Java Date object into another human readable String pattern, you need SimpleDateFormat#format().

private String modifyDateLayout(String inputDate) throws ParseException{
    Date date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss z").parse(inputDate);
    return new SimpleDateFormat("dd.MM.yyyy HH:mm:ss").format(date);
}

By the way, the "unparseable date" exception can here only be thrown by SimpleDateFormat#parse(). This means that the inputDate isn't in the expected pattern "yyyy-MM-dd HH:mm:ss z". You'll probably need to modify the pattern to match the inputDate's actual pattern.

Update: Okay, I did a test:

public static void main(String[] args) throws Exception {
    String inputDate = "2010-01-04 01:32:27 UTC";
    String newDate = new Test().modifyDateLayout(inputDate);
    System.out.println(newDate);
}

This correctly prints:

03.01.2010 21:32:27

(I'm on GMT-4)

Update 2: as per your edit, you really got a ParseException on that. The most suspicious part would then be the timezone of UTC. Is this actually known at your Java environment? What Java version and what OS version are you using? Check TimeZone.getAvailableIDs(). There must be a UTC in between.

查看更多
登录 后发表回答