aspose file tools*
The moose likes Java in General and the fly likes simple date format question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "simple date format question" Watch "simple date format question" New topic
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: 18909
    
    8

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: 19762
    
  20

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 - OCEEJBD 6
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
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14350
    
  22

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 8 API documentation
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