How to transfer Date.getTime() to hours and calculating difference
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
add a comment |
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
why are you casting toint
?
– Scary Wombat
Nov 22 '18 at 2:33
Consider this codeDate 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
add a comment |
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
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
java date parsing
asked Nov 22 '18 at 2:30
waffle and baconwaffle and bacon
32
32
why are you casting toint
?
– Scary Wombat
Nov 22 '18 at 2:33
Consider this codeDate 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
add a comment |
why are you casting toint
?
– Scary Wombat
Nov 22 '18 at 2:33
Consider this codeDate 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
add a comment |
2 Answers
2
active
oldest
votes
Use the latest classes available fron java8
LocalDateTime now = LocalDateTime.now();
System.out.println(now.getHour());
add a comment |
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
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Use the latest classes available fron java8
LocalDateTime now = LocalDateTime.now();
System.out.println(now.getHour());
add a comment |
Use the latest classes available fron java8
LocalDateTime now = LocalDateTime.now();
System.out.println(now.getHour());
add a comment |
Use the latest classes available fron java8
LocalDateTime now = LocalDateTime.now();
System.out.println(now.getHour());
Use the latest classes available fron java8
LocalDateTime now = LocalDateTime.now();
System.out.println(now.getHour());
answered Nov 22 '18 at 2:42


Scary WombatScary Wombat
35.3k32252
35.3k32252
add a comment |
add a comment |
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
add a comment |
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
add a comment |
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
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
answered Nov 22 '18 at 3:20


John CamerinJohn Camerin
436211
436211
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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