?
Solved

Help me!

Posted on 1998-11-22
1
Medium Priority
?
168 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
[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
1 Comment
 
LVL 8

Accepted Solution

by:
Christoffer Swanström earned 240 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

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…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
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
Course of the Month15 days, 15 hours left to enroll

743 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