?
Solved

A Time Problem

Posted on 1998-08-24
10
Medium Priority
?
179 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
[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
  • 5
  • 4
10 Comments
 
LVL 8

Expert Comment

by:diakov
ID: 1222539
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
ID: 1222540
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
ID: 1222541
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
Technology Partners: 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!

 

Author Comment

by:leeann
ID: 1222542
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
ID: 1222543
Perfect.
0
 

Author Comment

by:leeann
ID: 1222544
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
ID: 1222545
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
ID: 1222546
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
ID: 1222547
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 80 total points
ID: 1222548
locking as requested by leeann
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
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 …
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
Suggested Courses
Course of the Month14 days, 3 hours left to enroll

801 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