Solved

Help me!

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
array6 challenfge 6 64
strCount chalenge 3 50
Systems talking to each other 5 110
Impossible to extract MSI from new JAVA releases 2 41
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
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 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:
The viewer will learn how to implement Singleton Design Pattern in Java.

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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now