How to transfer Date.getTime() to hours and calculating difference












0















I have a program that is intaking an "AM" "PM" time and calculating out the hours in the day equivalent (in 24 hour format). For some reason it parses and calculates the time I input to the incorrect 24 hour equivalent (ie 5:00 pm comes to equal 22)



System.out.print("Enter the end time (HH:MM am): ");
endTime = input.nextLine();
Date ETime = time_to_date.parse(endTime);


Class method



public int get_Family_A_Calulation(Date STime, Date ETime) {
Date startTimeCalc = STime, endTimeCalc = ETime;
int pay = 0, hoursWorked, StartHour, EndHour;
StartHour = ((((int) startTimeCalc.getTime()) / 1000) / 60) / 60;
EndHour = ((((int) endTimeCalc.getTime()) / 1000) / 60) / 60;
pay = hoursWorked * 15;
return pay;
}


I am not sure where my error is can anyone give me advice on how to correct this error?










share|improve this question























  • why are you casting to int ?

    – Scary Wombat
    Nov 22 '18 at 2:33











  • Consider this code Date now = new Date (); System.out.println(now.getTime()); System.out.println(((int)now.getTime()));

    – Scary Wombat
    Nov 22 '18 at 2:34













  • @ScaryWombat The reason I am casting to an int is because I am going to need to do payroll calculation based on a start and end time input. The rate per hour changes depending on what hour of the day the individual works. 5pm-11pm per hour rate is $15 and any time after 11pm the rate changes to $20. So I have a few if statements later in the program where all the calculations are based of off a 24 hour calculations in int form.

    – waffle and bacon
    Nov 22 '18 at 14:46











  • Possible duplicate of Java: getMinutes and getHours

    – Ole V.V.
    Nov 22 '18 at 19:59
















0















I have a program that is intaking an "AM" "PM" time and calculating out the hours in the day equivalent (in 24 hour format). For some reason it parses and calculates the time I input to the incorrect 24 hour equivalent (ie 5:00 pm comes to equal 22)



System.out.print("Enter the end time (HH:MM am): ");
endTime = input.nextLine();
Date ETime = time_to_date.parse(endTime);


Class method



public int get_Family_A_Calulation(Date STime, Date ETime) {
Date startTimeCalc = STime, endTimeCalc = ETime;
int pay = 0, hoursWorked, StartHour, EndHour;
StartHour = ((((int) startTimeCalc.getTime()) / 1000) / 60) / 60;
EndHour = ((((int) endTimeCalc.getTime()) / 1000) / 60) / 60;
pay = hoursWorked * 15;
return pay;
}


I am not sure where my error is can anyone give me advice on how to correct this error?










share|improve this question























  • why are you casting to int ?

    – Scary Wombat
    Nov 22 '18 at 2:33











  • Consider this code Date now = new Date (); System.out.println(now.getTime()); System.out.println(((int)now.getTime()));

    – Scary Wombat
    Nov 22 '18 at 2:34













  • @ScaryWombat The reason I am casting to an int is because I am going to need to do payroll calculation based on a start and end time input. The rate per hour changes depending on what hour of the day the individual works. 5pm-11pm per hour rate is $15 and any time after 11pm the rate changes to $20. So I have a few if statements later in the program where all the calculations are based of off a 24 hour calculations in int form.

    – waffle and bacon
    Nov 22 '18 at 14:46











  • Possible duplicate of Java: getMinutes and getHours

    – Ole V.V.
    Nov 22 '18 at 19:59














0












0








0








I have a program that is intaking an "AM" "PM" time and calculating out the hours in the day equivalent (in 24 hour format). For some reason it parses and calculates the time I input to the incorrect 24 hour equivalent (ie 5:00 pm comes to equal 22)



System.out.print("Enter the end time (HH:MM am): ");
endTime = input.nextLine();
Date ETime = time_to_date.parse(endTime);


Class method



public int get_Family_A_Calulation(Date STime, Date ETime) {
Date startTimeCalc = STime, endTimeCalc = ETime;
int pay = 0, hoursWorked, StartHour, EndHour;
StartHour = ((((int) startTimeCalc.getTime()) / 1000) / 60) / 60;
EndHour = ((((int) endTimeCalc.getTime()) / 1000) / 60) / 60;
pay = hoursWorked * 15;
return pay;
}


I am not sure where my error is can anyone give me advice on how to correct this error?










share|improve this question














I have a program that is intaking an "AM" "PM" time and calculating out the hours in the day equivalent (in 24 hour format). For some reason it parses and calculates the time I input to the incorrect 24 hour equivalent (ie 5:00 pm comes to equal 22)



System.out.print("Enter the end time (HH:MM am): ");
endTime = input.nextLine();
Date ETime = time_to_date.parse(endTime);


Class method



public int get_Family_A_Calulation(Date STime, Date ETime) {
Date startTimeCalc = STime, endTimeCalc = ETime;
int pay = 0, hoursWorked, StartHour, EndHour;
StartHour = ((((int) startTimeCalc.getTime()) / 1000) / 60) / 60;
EndHour = ((((int) endTimeCalc.getTime()) / 1000) / 60) / 60;
pay = hoursWorked * 15;
return pay;
}


I am not sure where my error is can anyone give me advice on how to correct this error?







java date parsing






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 22 '18 at 2:30









waffle and baconwaffle and bacon

32




32













  • why are you casting to int ?

    – Scary Wombat
    Nov 22 '18 at 2:33











  • Consider this code Date now = new Date (); System.out.println(now.getTime()); System.out.println(((int)now.getTime()));

    – Scary Wombat
    Nov 22 '18 at 2:34













  • @ScaryWombat The reason I am casting to an int is because I am going to need to do payroll calculation based on a start and end time input. The rate per hour changes depending on what hour of the day the individual works. 5pm-11pm per hour rate is $15 and any time after 11pm the rate changes to $20. So I have a few if statements later in the program where all the calculations are based of off a 24 hour calculations in int form.

    – waffle and bacon
    Nov 22 '18 at 14:46











  • Possible duplicate of Java: getMinutes and getHours

    – Ole V.V.
    Nov 22 '18 at 19:59



















  • why are you casting to int ?

    – Scary Wombat
    Nov 22 '18 at 2:33











  • Consider this code Date now = new Date (); System.out.println(now.getTime()); System.out.println(((int)now.getTime()));

    – Scary Wombat
    Nov 22 '18 at 2:34













  • @ScaryWombat The reason I am casting to an int is because I am going to need to do payroll calculation based on a start and end time input. The rate per hour changes depending on what hour of the day the individual works. 5pm-11pm per hour rate is $15 and any time after 11pm the rate changes to $20. So I have a few if statements later in the program where all the calculations are based of off a 24 hour calculations in int form.

    – waffle and bacon
    Nov 22 '18 at 14:46











  • Possible duplicate of Java: getMinutes and getHours

    – Ole V.V.
    Nov 22 '18 at 19:59

















why are you casting to int ?

– Scary Wombat
Nov 22 '18 at 2:33





why are you casting to int ?

– Scary Wombat
Nov 22 '18 at 2:33













Consider this code Date now = new Date (); System.out.println(now.getTime()); System.out.println(((int)now.getTime()));

– Scary Wombat
Nov 22 '18 at 2:34







Consider this code Date now = new Date (); System.out.println(now.getTime()); System.out.println(((int)now.getTime()));

– Scary Wombat
Nov 22 '18 at 2:34















@ScaryWombat The reason I am casting to an int is because I am going to need to do payroll calculation based on a start and end time input. The rate per hour changes depending on what hour of the day the individual works. 5pm-11pm per hour rate is $15 and any time after 11pm the rate changes to $20. So I have a few if statements later in the program where all the calculations are based of off a 24 hour calculations in int form.

– waffle and bacon
Nov 22 '18 at 14:46





@ScaryWombat The reason I am casting to an int is because I am going to need to do payroll calculation based on a start and end time input. The rate per hour changes depending on what hour of the day the individual works. 5pm-11pm per hour rate is $15 and any time after 11pm the rate changes to $20. So I have a few if statements later in the program where all the calculations are based of off a 24 hour calculations in int form.

– waffle and bacon
Nov 22 '18 at 14:46













Possible duplicate of Java: getMinutes and getHours

– Ole V.V.
Nov 22 '18 at 19:59





Possible duplicate of Java: getMinutes and getHours

– Ole V.V.
Nov 22 '18 at 19:59












2 Answers
2






active

oldest

votes


















1














Use the latest classes available fron java8



LocalDateTime now = LocalDateTime.now();
System.out.println(now.getHour());





share|improve this answer































    0














    The actual data behind Date is milliseconds since the epoch. Any hour or time representation is based on the calendar date portion and takes into account timezone and daylight savings.
    Regardless of what you do, there will be calculation issues across days, etc.
    As suggested by Scary Wombat, use the new classes in java.time package. For your specific case, you need a LocalTime as the code is trying to represent a time element (hours, minutes, seconds, etc) without consideration for Date, TimeZone, etc.
    https://docs.oracle.com/javase/8/docs/api/java/time/LocalTime.html






    share|improve this answer























      Your Answer






      StackExchange.ifUsing("editor", function () {
      StackExchange.using("externalEditor", function () {
      StackExchange.using("snippets", function () {
      StackExchange.snippets.init();
      });
      });
      }, "code-snippets");

      StackExchange.ready(function() {
      var channelOptions = {
      tags: "".split(" "),
      id: "1"
      };
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function() {
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled) {
      StackExchange.using("snippets", function() {
      createEditor();
      });
      }
      else {
      createEditor();
      }
      });

      function createEditor() {
      StackExchange.prepareEditor({
      heartbeatType: 'answer',
      autoActivateHeartbeat: false,
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader: {
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      },
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      });


      }
      });














      draft saved

      draft discarded


















      StackExchange.ready(
      function () {
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53423082%2fhow-to-transfer-date-gettime-to-hours-and-calculating-difference%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      1














      Use the latest classes available fron java8



      LocalDateTime now = LocalDateTime.now();
      System.out.println(now.getHour());





      share|improve this answer




























        1














        Use the latest classes available fron java8



        LocalDateTime now = LocalDateTime.now();
        System.out.println(now.getHour());





        share|improve this answer


























          1












          1








          1







          Use the latest classes available fron java8



          LocalDateTime now = LocalDateTime.now();
          System.out.println(now.getHour());





          share|improve this answer













          Use the latest classes available fron java8



          LocalDateTime now = LocalDateTime.now();
          System.out.println(now.getHour());






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 22 '18 at 2:42









          Scary WombatScary Wombat

          35.3k32252




          35.3k32252

























              0














              The actual data behind Date is milliseconds since the epoch. Any hour or time representation is based on the calendar date portion and takes into account timezone and daylight savings.
              Regardless of what you do, there will be calculation issues across days, etc.
              As suggested by Scary Wombat, use the new classes in java.time package. For your specific case, you need a LocalTime as the code is trying to represent a time element (hours, minutes, seconds, etc) without consideration for Date, TimeZone, etc.
              https://docs.oracle.com/javase/8/docs/api/java/time/LocalTime.html






              share|improve this answer




























                0














                The actual data behind Date is milliseconds since the epoch. Any hour or time representation is based on the calendar date portion and takes into account timezone and daylight savings.
                Regardless of what you do, there will be calculation issues across days, etc.
                As suggested by Scary Wombat, use the new classes in java.time package. For your specific case, you need a LocalTime as the code is trying to represent a time element (hours, minutes, seconds, etc) without consideration for Date, TimeZone, etc.
                https://docs.oracle.com/javase/8/docs/api/java/time/LocalTime.html






                share|improve this answer


























                  0












                  0








                  0







                  The actual data behind Date is milliseconds since the epoch. Any hour or time representation is based on the calendar date portion and takes into account timezone and daylight savings.
                  Regardless of what you do, there will be calculation issues across days, etc.
                  As suggested by Scary Wombat, use the new classes in java.time package. For your specific case, you need a LocalTime as the code is trying to represent a time element (hours, minutes, seconds, etc) without consideration for Date, TimeZone, etc.
                  https://docs.oracle.com/javase/8/docs/api/java/time/LocalTime.html






                  share|improve this answer













                  The actual data behind Date is milliseconds since the epoch. Any hour or time representation is based on the calendar date portion and takes into account timezone and daylight savings.
                  Regardless of what you do, there will be calculation issues across days, etc.
                  As suggested by Scary Wombat, use the new classes in java.time package. For your specific case, you need a LocalTime as the code is trying to represent a time element (hours, minutes, seconds, etc) without consideration for Date, TimeZone, etc.
                  https://docs.oracle.com/javase/8/docs/api/java/time/LocalTime.html







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 22 '18 at 3:20









                  John CamerinJohn Camerin

                  436211




                  436211






























                      draft saved

                      draft discarded




















































                      Thanks for contributing an answer to Stack Overflow!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53423082%2fhow-to-transfer-date-gettime-to-hours-and-calculating-difference%23new-answer', 'question_page');
                      }
                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      MongoDB - Not Authorized To Execute Command

                      How to fix TextFormField cause rebuild widget in Flutter

                      in spring boot 2.1 many test slices are not allowed anymore due to multiple @BootstrapWith