Link to home
Start Free TrialLog in
Avatar of leeann
leeann

asked on

A Time Problem

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?
Avatar of diakov
diakov

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.
Avatar of leeann

ASKER

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 (https://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!
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.
Avatar of leeann

ASKER

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:
https://www.experts-exchange.com/topics/comp/lang/java/Q.10074564

Perfect.
Avatar of leeann

ASKER

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.
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
Avatar of leeann

ASKER

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

ASKER

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) { }
     }
ASKER CERTIFIED SOLUTION
Avatar of sailwind
sailwind

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial