tags:

views:

1450

answers:

1

Hi,

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

#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.

+5  A: 

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.

BalusC
The problem is that the line Date date = formatter.parse(inputDate);throws an exception. I fixed the input format to yyyy-MM-dd HH:mm:ss z but it still throws an exception.
niko
I expected that, see my edit which was added before you commented on. If you want more assistance on finding the right pattern, you'll need to post an example of the **actual** `inputDate` value.
BalusC