1 2 Previous Next 20 Replies Latest reply on Nov 26, 2007 11:34 AM by 807603

    converting time to an integer

    807603
      hi everyone

      in my program i have to type of time
      one is the current time from the system and the other one is the time stored into the program by myself

      i need to do a calculation and find out the time difference between them, if anyone knows how can i do it please
      let me know i', stuck with this part for a week now

      cheers
        • 1. Re: converting time to an integer
          807603
          How are you keeping track of the time within the program? You can easily get the System time in a double value.
          • 2. Re: converting time to an integer
            807603
            I'm taking the system time as a string using the time method
            it doesn't allow me to take it as a double

            and the flight time is stored as 14:00pm format
            i tried to use the following code but it doesn't give me the correct output

            int hour = (deptime.charAt(0) + deptime.charAt(1));

            deptime is where the flight time is stored
            do you have any idea why this is wrong
            • 3. Re: converting time to an integer
              Herko_ter_Horst
              Java internally represents time as a long (not a double) representing the number of milliseconds since 1-1-1970.

              java.util.Date is a "wrapper" for this information.

              Converting Strings to and from Dates is best done using a (Simple)DateFormat that represents your input and/or output formats.

              For example, 14.00pm can be parsed/printed by the DateFormat.getTimeInstance(DateFormat.SHORT) format:
              String flightTimeString = "14:00pm";
              DateFormat flightTimeFormat = DateFormat.getTimeInstance(DateFormat.SHORT);
              Date flightTime = flightTimeFormat.parse(flightTimeString); // note: this is 14:00pm on Jan 1, 1970!
              Date now = new Date(); // current system date and time
              ...
              Also take a look at java.util.Calendar, if you need to manipulate Dates.

              PS what you did wrong in your original solution is adding the single digits 1 and 4 (which of course is 5) instead of taking into account that the 1 actually represents 10 (you know, the decimal system ;)).
              • 4. Re: converting time to an integer
                DrClap
                Shanofer wrote:
                I'm taking the system time as a string using the time method
                What "time" method is that?
                it doesn't allow me to take it as a double
                Sure it does. System.currentTimeMillis() returns a long value (not a double, but you don't need floating-point numbers to represent an integral number of milliseconds anyway).
                • 5. Re: converting time to an integer
                  807603
                  thanks a lot for helping me out

                  i did try to execute the code but it gives me an error saying that

                  variable DateFormat couldn't be found

                  i did import the java.util.date package as well
                  do you know why this happens

                  cheers
                  • 6. Re: converting time to an integer
                    niceonedave
                    Just to point out something about this piece of code. You have deptime stored as a string. Then you get the first character from the string and and it with the second character then assign it to an integer variable. What this does is take the ascii values of the characters and add them together. If the time was 14:00, then the result would be 101 (ascii 1 has value 49 and ascii 4 has value 52).

                    To do it like this you would have to parse the string to an integer first,
                    Integer.parseInt(deptime.charAt(0));
                    This would throw a NumberFormatException if the time was not in the correct format.
                    • 7. Re: converting time to an integer
                      807603
                      yes, you were 100% right thats the answer i got

                      but when i.m trying to convert to integer using parseInt
                      it gives me the following error

                      symbol  : method parseInt(char)

                      i don't understand this
                      • 8. Re: converting time to an integer
                        niceonedave
                        Ok the problem is, as I expected, you can't parse a char type to an integer using parseInt.

                        What I would suggest would be to split your time into hours and minutes (I assume it is always in the form hh:mm).
                        String [] token = deptime.split(":");
                        int hours = Integer.parseInt(token[0]);
                        int minutes = Integer.parseInt(token[1]);
                        What this does is split the string using : as the delimiter giving you an array of strings (in this case of size 2). The first element of the array will contain the string for the number of hours and the second element will contain the string for the number of minutes. You can now parse these strings to integers to get the number of hours and the number of minutes.
                        • 9. Re: converting time to an integer
                          807603
                          there are no any compile time errors but it causes a runtime error

                          saying NumberFormatException

                          Exception in thread "main" java.lang.NumberFormatException: For input string: "14.00"
                          at java.lang.NumberFormatException.forInputString(NumberFormatException.java:48)
                          • 10. Re: converting time to an integer
                            807603
                            oops sorry the mistake was mine i used a '.' instead of ':'

                            now it working but it gives me 0 as the output

                            why is dat
                            • 11. Re: converting time to an integer
                              niceonedave
                              Sorry, I didn't realise your time was in the form 14.00, you just need to change the delimiter of the split method from : to .
                              String [] token = deptime.split(".");
                              • 12. Re: converting time to an integer
                                niceonedave
                                You should have 2 outputs, the first an integer called hours, which should have the value 14 and the second an integer called minutes with the value 0.
                                • 13. Re: converting time to an integer
                                  807603
                                  thanks a lot you really saved my life

                                  thank you finally its working perfect
                                  im graetful
                                  • 14. Re: converting time to an integer
                                    niceonedave
                                    You're welcome.
                                    1 2 Previous Next