• Post Reply Bookmark Topic Watch Topic
  • New Topic

convert a String to a Date  RSS feed

 
Yingie Pitts
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • 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?
 
Jeff Langr
author
Ranch Hand
Posts: 799
5
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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?
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • 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
  • 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?
 
Kim Kantola
Ranch Hand
Posts: 276
  • Mark post as helpful
  • send pies
  • 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
  • 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
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • 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:
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!