Solved

Date Parsing

Posted on 2004-10-18
11
238 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
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 
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

The Ultimate Checklist to Optimize Your Website

Websites are getting bigger and complicated by the day. Video, images, custom fonts are all great for showcasing your product/service. But the price to pay in terms of reduced page load times and ultimately, decreased sales, can lead to some difficult decisions about what to cut.

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…
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…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

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