Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Agile forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

simple date format question

 
Johann Dobbins
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 600
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Johann:

You're missing a time zone.

John.
 
Johann Dobbins
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Pie
Posts: 20750
30
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 600
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Johann,

You probably should change your pattern to something like this:


John.
 
Rob Spoor
Sheriff
Pie
Posts: 20495
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:
 
Johann Dobbins
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Saloon Keeper
Posts: 15207
36
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Johann Dobbins
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic