Solved

Date Parsing

Posted on 2004-10-18
11
224 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
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
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
 
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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
The viewer will learn how to implement Singleton Design Pattern in Java.

791 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