• 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:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Date Parsing Issue...

 
Ranch Hand
Posts: 483
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Everyone,

I have a date which of the format "15-Dec-2009 06:13:14" enetered by the user as String. I want to convert it to Date format (java.util.Date). But i am getting the error as unable to parse in my code.



Error Stack:

 
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The first thing striking my eyes is that you're using MM for both months and minutes You need to read carefully the java.text.SimpleDateFormat API.
 
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
The codes you use to specify the date format when you create a SimpleDateFormat object are case-sensitive. So "DD-MM-YYYY HH:MM:SS" doesn't mean what you think it means. Lookup the documentation for SimpleDateFormat for information on how to specify the format.

Also, the "MM" you use for the months is not going to match three-letter month abbreviations like "Dec".
 
Somnath Mallick
Ranch Hand
Posts: 483
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I modified the code after going through the API.



Now I get the error as:

Also i checked the API and found that 14-Dec-2009 17:00:00 type of date is not mentioned anywhere!
 
Somnath Mallick
Ranch Hand
Posts: 483
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Got it after a little further reading and experiments. This worked for me! Thanks!

 
Jesper de Jong
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

Somnath Mallick wrote:Got it after a little further reading and experiments. This worked for me! Thanks!


Are you sure that this works correctly? Note that MM (upper-case) is the month number, not minutes, and SS (upper-case) is milliseconds, not seconds.
 
Somnath Mallick
Ranch Hand
Posts: 483
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
^^Well at least, Well do you mean to say that i should change it like this?


Also one more question, Lets for example take the code:


time is in GMT.
InputBean.getSearchStartTimeStamp() and InputBean.getSearchEndTimeStamp() are in IST.
Will they yield a proper result? I mean does JVM does the IST to GMT conversions internally?
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

I mean does JVM does the IST to GMT conversions internally?


No. The Date class has no notion of timezones; you'll need to use the Calendar class instead.
 
Somnath Mallick
Ranch Hand
Posts: 483
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I modified the code to this:

But still all the dates are printing in IST!
 
Ulf Dittmer
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As I said before, the Date class has no concept of timezones, therefore Date.toString is not likely to do the right thing. Use DateFormat.format instead, and make sure to set its timezone (which the code currently is not doing).
 
Jesper de Jong
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

Somnath Mallick wrote:^^Well at least, Well do you mean to say that i should change it like this?


Yes, but note that hh (lower-case) for the hours means the hour in AM/PM (1-12) - this will fail if hours is 17. Try this:


Somnath Mallick wrote:But still all the dates are printing in IST!


Yes, because Date.toString() uses the local timezone setting of your system - it displays the Date object in your local timezone. Instead of using Date.toString(), format the date object using a DateFormat object; set the timezone that you want your date displayed in on the DateFormat object. As Ulf said, class Date itself doesn't know about timezones.
 
Somnath Mallick
Ranch Hand
Posts: 483
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

I hope this is correct? But when the time is printing no time zone is getting printed so i cant really say the times are really in GMT or IST.
Also when i am trying to insert this value in an excel sheet using POI API, i had to convert all time values to string, so all the data entered in the Excel sheet is IST!
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic