Solved

Date Parsing

Posted on 2004-10-18
11
230 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
The viewer will learn how to implement Singleton Design Pattern in Java.

726 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