Win a copy of Terraform in Action this week in the Cloud forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

simple date format question

 
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Rancher
Posts: 600
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Marshal
Posts: 26914
82
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Johann,

You probably should change your pattern to something like this:


John.
 
Sheriff
Posts: 22510
122
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic