Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java: unparseable date exception

Tags:

java

date

format

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.

like image 426
Niko Gamulin Avatar asked Jan 05 '10 21:01

Niko Gamulin


People also ask

How do you handle Unparseable date exception in Java?

You can try the below format: SimpleDateFormat sdf = new SimpleDateFormat("EE MMM dd HH:mm:ss z yyyy", Locale. ENGLISH);

What is Unparseable date error?

parseexception: unparseable date” error message, when you try to parse the date string into another desired format. Don't worry it's a very common error message that users generally faced while parsing a date in java using SimpleDateFormat class.

How do I change date format in SimpleDateFormat?

You can just use: Date yourDate = new Date(); SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd"); String date = DATE_FORMAT. format(yourDate);

How do you format a date in Java?

Java SimpleDateFormat ExampleString pattern = "MM-dd-yyyy"; SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern); String date = simpleDateFormat. format(new Date()); System. out. println(date);


1 Answers

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.

like image 141
BalusC Avatar answered Sep 29 '22 00:09

BalusC