Solved

Date Parsing

Posted on 2004-10-18
11
201 Views
Last Modified: 2010-03-31
Hi,

I think java's date parsing is a little messed up...
Can someone explain why I get a true result in the following code snippet.
Thanks.
------------------------------------------------------------------------------------------

import java.util.*;
import java.text.*;

public class Uptime
{
  public static void main(String []args) throws Exception
  {
   
          String started =       "28 Feb 2004 1:23:45 pm";
          String now =       "28 Feb 2004 1:23:45 am";
   
      SimpleDateFormat df=new SimpleDateFormat("dd MMM yyyy HH:mm:ss aa");

          Date s=df.parse(started,new ParsePosition(0));
          Date n=df.parse(now,new ParsePosition(0));
      System.out.println(s.equals(n)); // prints true...
  }

}
0
Comment
Question by:BabyFace
  • 6
  • 5
11 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 12341615
Probably because you're telling Java you're using double digits for hours yet you're not in reality. If you move the String format to single figures, it will probably then take into account am/pm and print false
0
 
LVL 14

Accepted Solution

by:
Tommy Braas earned 50 total points
ID: 12341767
Hi BabyFace,

You're specifying to read the time as 24 hour clock, but have added the am/pm field, plus the actual time is in am/pm.

Try the following format:
"dd MMM yyyy h:mm:ss a")


\t
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 12341792
o can you tell me how that answer (posted almost twenty minutes later) is any different from what i've said?
0
 
LVL 14

Expert Comment

by:Tommy Braas
ID: 12341893
CEHJ,
> o can you tell me how that answer (posted almost twenty minutes later)
> is any different from what i've said?
Certainly, you didn't mention that the actual parse string was in 24 hour format. You're not mentioning the conflict between the 24 hour clock and the am/pm field.

\t
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 12341938
>>you didn't mention that the actual parse string was in 24 hour format

>>
you're using double digits for hours yet you're not in reality. If you move the String format to single figures, it will probably then take into account am/pm and print false
>>
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 86

Expert Comment

by:CEHJ
ID: 12342217
!?
0
 
LVL 14

Expert Comment

by:Tommy Braas
ID: 12342402
CEHJ,

So are you saying the 10, 11, 12 are only legal values for the hour field in 24 hour notation?

\t

(being facetious...)
0
 
LVL 14

Expert Comment

by:Tommy Braas
ID: 12342403
BabyFace,

Thank you much! =-)
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 12342451
>>So are you saying the ..

I don't understand your comment, but i do understand that you've simply paraphrased the answer i gave:

>>You're specifying to read the time as 24 hour clock
==
>>you're using double digits for hours

and
>>
Try the following format:
"dd MMM yyyy h:mm:ss a")
>>
==
>> If you move the String format to single figures
0
 
LVL 14

Expert Comment

by:Tommy Braas
ID: 12342542
CEHJ,
> >>You're specifying to read the time as 24 hour clock
> ==
> >>you're using double digits for hours

NOT the same thing. "HH" parses as 24 hour clock (which is what the asker was using), "hh" or "h" denotes 12 hour clock. Whether you use one or two 'h' is of NO consequence, same thing goes for the 'a'

\t
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 12342589
>>NOT the same thing.

That's certainly what i meant by it
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Suggested Solutions

For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

759 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now