[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Date Parsing

Posted on 2004-10-18
11
Medium Priority
?
246 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 200 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
Industry Leaders: 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

Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Suggested Courses

649 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