Created
November 21, 2016 13:36
-
-
Save santhoshv339/72cc1f87ae8e702c4915b4cf621dff39 to your computer and use it in GitHub Desktop.
Get Number of hours between two dates
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class GetHoursBetweenDates { | |
public static void main(String[] args) { | |
int timeAMin = toMins("12:15"); | |
int timeBMin = toMins("22:30"); | |
System.out.println(timeBMin); | |
getJourneyDuration(timeAMin,timeBMin,1,3); | |
} | |
public static int toMins(String s) { | |
String[] hourMin = s.split(":"); | |
int hour = Integer.parseInt(hourMin[0]); | |
int mins = Integer.parseInt(hourMin[1]); | |
int hoursInMins = hour * 60; | |
return hoursInMins + mins; | |
} | |
public static void getJourneyDuration(int aMin, int bMin,int day1,int day2) | |
{ | |
if (day1 ==day2) | |
{ | |
int diffMin = (bMin-aMin); | |
System.out.println(diffMin); | |
int hours = diffMin/60 ; | |
System.out.println(hours); | |
int remMin = diffMin - (hours*60); | |
System.out.println(remMin); | |
String timeCorrect = +hours+ ":" +remMin++; | |
System.out.println(timeCorrect); | |
} | |
else if (day2-day1 == 1) | |
{ | |
int totalMins = (1440-aMin) +(bMin); | |
int hours = totalMins/60 ; | |
System.out.println(hours); | |
int remMin = totalMins - (hours*60); | |
System.out.println(remMin); | |
String timeCorrect = +hours+ ":" +remMin++; | |
System.out.println(timeCorrect); | |
} | |
else | |
{ | |
int inBetMins = (day2-day1-1)*24*60; | |
int totalMins = (1440-aMin) +inBetMins +(bMin); | |
int hours = totalMins/60 ; | |
System.out.println(hours); | |
int remMin = totalMins - (hours*60); | |
System.out.println(remMin); | |
String timeCorrect = +hours+ ":" +remMin++; | |
System.out.println(timeCorrect); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment