Win a copy of Rust Web Development this week in the Other Languages 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
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

convert a String to a Date

 
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I got two Date object using this way:

Date date1 = DateFormate.getDateInstance().parse("Jan 23, 2004 03:01:38pm");
Date date2 = DateFormate.getDateInstance().parse("Jan 23, 2004 03:10:38pm");

However, date1.getTime() always equals to date2.getTime(), which is 1074834000000. Can anyone tell my what's wrong?
 
author
Posts: 799
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It's only parsing the day; I believe that's the default DateFormat parse behavior. If you really need to parse a string like that, take a look at SimpleDateFormat.
What are you really trying to do?
-Jeff-
 
Yingie Pitts
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If I use SimpleDateFormat, I would get this:
java.text.ParseException: Unparseable date: "Jan 23, 2004 03:39:38pm".
What I am trying to do is to get a difference of two dates, which are supposed to be formed from two strings.
 
Yingie Pitts
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry about that ParseException thing. That was because I did not use ParsePosition parameter. However,
Date date1 = dateParser.parse("Jan 23, 2004 03:01:38pm", new ParsePosition(0)
returns null. Do you know why?
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry about that ParseException thing. That was because I did not use ParsePosition parameter.
You should be able to use the parse(String) inherited from DateFormat; it works fine. If you're having trouble I suspect it's in how you create the SimpleDateFormat. Did you read the documentation for the class? How are you creating the SimpleDateFormat? You can tell the SDF exactly what format to expect.
 
Yingie Pitts
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Jim, you were right. I could've used parse(String) directly, but that caused the ParseException problem (unable to parse date). Here is how I create the SimpleDateFormat object:
SimpleDateFormat dateParser = new SimpleDateFormat();
Date date1 = dateParser.parse("Jan 23, 2004 03:01:38pm");
Is there anything wrong?
 
Ranch Hand
Posts: 276
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I believe that your problem mignt be with the way you are constructing the SimpleDateFormat, you need to tell it how to expect the String that it will be parsing is formatted. For example:
 
Kim Kantola
Ranch Hand
Posts: 276
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Or, your problem may be with the string you passed, did you use this exact String? "Jan 23, 2004 03:01:38pm" ? If so, I believe it would have trouble with the pm immediately following the time, there should be a space there,
"03:01:38 pm".
 
Jim Yingst
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You want to tell the SimpleDateFormat what sort of format to expect; it's not psychic. To do this, you provide a pattern string as described in the documentation. In this case, try:
 
reply
    Bookmark Topic Watch Topic
  • New Topic