Solved

Help me!

Posted on 1998-11-22
1
165 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

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!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
even odd program using while loop 3 72
Which non-HTML GUI front end to use with Java? 3 52
Problem to Alipay 10 67
Crystal Reports Licensing Questions 4 59
INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
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…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:

762 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