Solved

Help me!

Posted on 1998-11-22
1
161 Views
Last Modified: 2010-03-30
I need to compare two values: finish time and start time which gotten from text fields in my program.  I tried to use codes:
1).  if (txtfinishTime.getText() <= txtstartTime.getText())
            return true
      elase
           return false;

 
2).  SimpleDateFormat formatter = new SimpleDateFormat("hh:mm:ss");
  long ET = formatter.parse(txtfinishTime.getText()).getTime();
  long ST = formatter.parse(txtstartTime.getText()).getTime();
 if(ET <= ST)
     return true;
 else
   return false;

Both 1) and 2) are wrong. For 2). If I enter any time from 12:00:00 to 12:59:59 in the text field txtfinishTime, 9:00:00 in the text field txtstartTime, it returns true. But if I enter any time not from 12:00:00 to 12:59:59 and later than the txtstartTime 9:00:00, such as 9:30:00, it returns false.
Can anybody help me solve this problem? Thanks!!!!
0
Comment
Question by:leeann
1 Comment
 
LVL 8

Accepted Solution

by:
Christoffer Swanström earned 60 total points
ID: 1228027
Your second approach should work with the following modification. Change the line:

SimpleDateFormat formatter = new SimpleDateFormat("hh:mm:ss");

to:

SimpleDateFormat formatter = new SimpleDateFormat("HH:mm:ss");

The difference is that hh uses am/pm notation (hours from 1 to 12) and HH uses 24-hour notation (0 to 23). hh should also work, but for some reason it does not. You can verify this by printing out the values assigned to ET and ST: when the finish time is between 12:00:00 and 12:59:59 ET is negative, which just doesn't make sense (Date.getTime() returns the number of milliseconds elapsed since January 1st, 1970)

0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
topping2 challenge 13 83
Java asynchronous logging 4 50
Java passing arguments (type error) 2 48
eclipse apache tomcat admin console 52 73
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
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…
The viewer will learn how to implement Singleton Design Pattern in Java.
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

863 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

23 Experts available now in Live!

Get 1:1 Help Now