Solved

A Time Problem

Posted on 1998-08-24
10
172 Views
Last Modified: 2010-03-30
Because the type of elapsedTime in the table "Results" of my MS Access database is Date/Time, when insert a value into the field of elapsedTime, it will automatically add "AM" at the end.
For example: elapsedTime=00:30:10
After the elapsedTime is inserted into table "Results", you will see 00:30:10 AM in the elapsedTime field, not 00:30:10.

I can not change the type of the elapsedTime in table "Results" from "Data/Time" to "Text" because I need compare these values for ranking. Do you have a good way to solve this problem?
0
Comment
Question by:leeann
  • 5
  • 4
10 Comments
 
LVL 8

Expert Comment

by:diakov
Comment Utility
When you have the Date object you can do several things.

- First AM PM is not important, because you know what you have in this field and can always use it in the correct way.
- Then the problem is how to show it in the 24 hour way. Assuming you have written the code.
For showing you can use the java.text.DateFormat class, which can  format the Date as a string in almost any means you might think
of. (Use flag HOUR_OF_DAY0_FIELD or HOUR_OF_DAY1_FIELD)
- The problem is, that the suggested above solution is not the best one. If your time difference (elapsed time) is more than 24 hours, you will again get 13 o'clock, but it is already 37 hours. Thus you need a small routine that calculates this from a given elaspedTime and shows it on screen.
- As for the database field itself, if it is Data/Time and if you're using Access to view the Database file, locale settings from your operating system are used, so you can't do anything about it.
0
 

Author Comment

by:leeann
Comment Utility
Hi! Diakov,
I just want to calculate the elapsedTime during a running competition. So the elapsedTime
is less than one hour. As I said in the last comment of my another question (http://www.experts-exchange.com/topics/comp/lang/java/Q.10074564), your answer will cause calcuting problem. Maybe it is a good way that you can write a code to test your answer.
Thank you!
0
 
LVL 8

Expert Comment

by:diakov
Comment Utility
Hi leeann,

Then just store the differense in milliseconds (long). Each date object is represented this way internally and has a method to get it: getTime().
Afterwards, upon request you can go into Date again and visualize properly.

Success.
0
 

Author Comment

by:leeann
Comment Utility
HI, Diakov,
Please input "12:58:20 PM" in txtstartTime, "1:23:12 PM" in txtendTime when you try the following code (You will get 0-11:0-35:0-8):

    import java.awt.*;
    import java.awt.event.*;
    import java.text.SimpleDateFormat;
    import java.util.Date;

    public class timeT extends Frame implements TextListener
    {
      Label lbstartTime, lbendTime;
      TextField txtstartTime, txtendTime, txtelapsedTime;
      SimpleDateFormat formatter;

      static public void main(String args[])
      {
            timeT t = new timeT();
            t.show();
      }
      public timeT()
      {
            super( "Elapsed Time" );
            //{{INIT_CONTROLS
            setLayout(new FlowLayout());
           

            lbstartTime = new Label("Start Time:");
            add(lbstartTime);
            txtstartTime = new TextField(10);
            txtstartTime.addTextListener(this);
            add(txtstartTime);
            lbendTime = new Label("End Time:");
            add(lbendTime);
            txtendTime = new TextField(10);
            txtendTime.addTextListener(this);
            add(txtendTime);
            txtelapsedTime = new TextField(10);
            add(txtelapsedTime);
            pack();

            formatter = new SimpleDateFormat("HH:mm:ss");
           
            //{{REGISTER_LISTENERS
            this.addWindowListener(new WindowAdapter(){
              public void windowClosing(WindowEvent e){
                    System.exit(0);}
            });

           
    }  
           
    private void ifChanged( ) {
    try {
         Date sd = formatter.parse(txtstartTime.getText());
         Date ed = formatter.parse(txtendTime.getText());
         long diff = ed.getTime()-sd.getTime();
         diff /=1000;
         long seconds = diff % 60;
         diff /= 60;
         long min = diff % 60;
         diff /= 60;
         long hours = diff;
           
         txtelapsedTime.setText(
       (hours<10?"0":"")+hours+":"+(min<10?"0":"")+min+":"+(seconds<10?"0":"")+seconds       );
   
    } catch(Exception e) {
    txtelapsedTime.setText("ERR:"+e.getMessage());
    }
  }

    public void textValueChanged(TextEvent e) {
    if ((e.getSource() == txtstartTime) ) {
    this.ifChanged();
    }
    if ((e.getSource() == txtendTime) ) {
    this.ifChanged();
    }
  }

}

So far, we are not  working on this question. Please go site:
http://www.experts-exchange.com/topics/comp/lang/java/Q.10074564

0
 
LVL 8

Expert Comment

by:diakov
Comment Utility
Perfect.
0
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 

Author Comment

by:leeann
Comment Utility
Diakov,
I don't understand why you said "Perfact". The code I wrote did not work well. The elapsedTime between 12:58:20 PM and 1:23:12 PM should be 0:24:52, not 0-11:0-35:0-8.
0
 
LVL 8

Expert Comment

by:diakov
Comment Utility
Hi Lee-Ann,

I solved your problem for this particular source you posted.

The pattern for the formatter in this case should be:

"h:m:s a"

Actually, when it parses, it uses the mask to tokenize and recognize the elements of the string.
"HH:mm:ss" says that you will provide the string in 0-24 hours, 0-59 minutes, 0-59 seconds, and no AM/PM token.

This is when you whant to parse. If you already have the Date objects your code works perfectly. Look in the description of the pattern format for the SimpleDateFormat class.

Regards,
  Nikolay
0
 

Author Comment

by:leeann
Comment Utility
Hi! Diakov,
I appreciate your help! Because your answer is not for this question, but for my question in site:
 http://www.experts-exchange.com/topics/comp/lang/java/Q.10074564
Please go there and lock it, thanks.
0
 

Author Comment

by:leeann
Comment Utility
Can anybody help me figure out what the problems in the following code are? Why did I get an
    "NumberFormatException" error message during run-time if I input any number in the text fields?

     int getChanged(myTextField t){
              int i;
              if(t.getText().equals(" "))
                  i = 0;
              else
                  i = Integer.parseInt(t.getText());
              return i;
     }

    // myTextField.java
     public class myTextField extends TextField implements   KeyListener {        
          public myTextField (String text) {
               super(text);
               addKeyListener(this);
          }
          public myTextField () {
               super();
               addKeyListener(this);
          }
          public String getText()
          {
                String s = super.getText();
                if((s == null)||(s.equals("")) )
                     s = " ";
                return s;
          }
          public void keyPressed(KeyEvent e)
          {
           if((e.getKeyCode()==KeyEvent.VK_TAB)||(e.getKeyCode() == KeyEvent.VK_ENTER))
                {
                  ((Component) (e.getSource())).transferFocus();
                }
          }
          public void keyTyped(KeyEvent e) { }
          public void keyReleased(KeyEvent e) { }
     }
0
 
LVL 3

Accepted Solution

by:
sailwind earned 20 total points
Comment Utility
locking as requested by leeann
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
matchUp  challenge 9 71
IT Company 5 68
firstChar challenge 13 83
Java array passed to SQL where clause 23 39
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 second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
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…

772 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

16 Experts available now in Live!

Get 1:1 Help Now