| Author |
simple date format question
|
Johann Dobbins
Ranch Hand
Joined: Oct 16, 2008
Posts: 62
|
|
I need to parse a date string into the following format: 20 Jun 1982 12:00:00 GMT.
The below code gives exception: java.text.ParseException: Unparseable date: "2010-04-28 07:48:19"
Help please?
|
 |
John de Michele
Rancher
Joined: Mar 09, 2009
Posts: 600
|
|
Johann:
You're missing a time zone.
John.
|
 |
Johann Dobbins
Ranch Hand
Joined: Oct 16, 2008
Posts: 62
|
|
John de Michele wrote:Johann:
You're missing a time zone.
John.
Sorry? I'm missing a time zone in the string I'm trying to parse?
That string is actually dynamic, I just wrote it has hardcoded for clarity. In the dynamic string, no time zone is given. I thought if no time zone was present, the date parser would use the local time zone?
|
 |
Paul Clapham
Bartender
Joined: Oct 14, 2005
Posts: 16487
|
|
The "z" at the end of the format string means there's going to be a timezone at the end of the date string.
And what made you think that it would default to the local timezone? Can you point to where it says that in the API documentation? Because if it doesn't say that, then that isn't the case.
|
 |
John de Michele
Rancher
Joined: Mar 09, 2009
Posts: 600
|
|
Johann,
You probably should change your pattern to something like this:
John.
|
 |
Rob Spoor
Sheriff
Joined: Oct 27, 2005
Posts: 19230
|
|
Johann Dobbins wrote:I need to parse a date string into the following format: 20 Jun 1982 12:00:00 GMT.
Dates have no formatting of their own, so you would need two DateFormats for this: one for parsing, one for formatting. Although if you know the Date to format you can also use java.util.Calendar to create the Date:
|
SCJP 1.4 - SCJP 6 - SCWCD 5
How To Ask Questions How To Answer Questions
|
 |
Johann Dobbins
Ranch Hand
Joined: Oct 16, 2008
Posts: 62
|
|
Rob Prime wrote:
Johann Dobbins wrote:I need to parse a date string into the following format: 20 Jun 1982 12:00:00 GMT.
Dates have no formatting of their own, so you would need two DateFormats for this: one for parsing, one for formatting. Although if you know the Date to format you can also use java.util.Calendar to create the Date:
excellent, thank you
|
 |
Jesper de Jong
Java Cowboy
Bartender
Joined: Aug 16, 2005
Posts: 12950
|
|
Johann Dobbins wrote:I need to parse a date string into the following format: 20 Jun 1982 12:00:00 GMT.
Parsing means converting a String to a Date. The opposite, converting a Date to a String, is formatting.
So you can't "parse a date string into format X" - you parse a String with a format X into a Date object, or you format a Date object into a String with format X.
|
Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
|
 |
Johann Dobbins
Ranch Hand
Joined: Oct 16, 2008
Posts: 62
|
|
Jesper Young wrote:
Johann Dobbins wrote:I need to parse a date string into the following format: 20 Jun 1982 12:00:00 GMT.
Parsing means converting a String to a Date. The opposite, converting a Date to a String, is formatting.
So you can't "parse a date string into format X" - you parse a String with a format X into a Date object, or you format a Date object into a String with format X.
Thank you for your help.
|
 |
 |
|
|
subject: simple date format question
|
|
|