Last active
April 20, 2019 07:38
-
-
Save ashikuzzaman-ar/c37d9005a4ca219c809228447f1b36b0 to your computer and use it in GitHub Desktop.
different style of adding two time in hour minute and second format
This file contains hidden or 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
import java.util.Scanner; | |
/** | |
* @author ashik | |
* | |
*/ | |
public class Main { | |
/** | |
* @param args | |
*/ | |
public static void main(String[] args) { | |
TimeSum.style0(); | |
} | |
} | |
class TimeSum { | |
static void style0() { | |
try (Scanner scanner = new Scanner(System.in)) { | |
System.out.print("please input time1 : "); | |
int hour1 = scanner.nextInt(), minute1 = scanner.nextInt(), second1 = scanner.nextInt(); | |
System.out.print("please input time2 : "); | |
int hour2 = scanner.nextInt(), minute2 = scanner.nextInt(), second2 = scanner.nextInt(); | |
int hour3 = hour1 + hour2, minute3 = minute1 + minute2, second3 = second1 + second2; | |
if (second3 > 60) { | |
second3 -= 60; | |
minute3++; | |
} | |
if (minute3 > 60) { | |
minute3 -= 60; | |
hour3++; | |
} | |
System.out.println(String.format("Hour = %s, Minute = %s, Second = %s", hour3, minute3, second3)); | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} | |
} | |
static void style1() { | |
try (Scanner scanner = new Scanner(System.in)) { | |
System.out.print("please input time1 : "); | |
int hour = scanner.nextInt(), minute = scanner.nextInt(), second = scanner.nextInt(); | |
System.out.print("please input time2 : "); | |
hour += scanner.nextInt(); | |
minute += scanner.nextInt(); | |
second += scanner.nextInt(); | |
if (second > 60) { | |
second = second - 60; | |
minute++; | |
} | |
if (minute > 60) { | |
minute = minute - 60; | |
hour++; | |
} | |
System.out.println(String.format("Hour = %s, Minute = %s, Second = %s", hour, minute, second)); | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} | |
} | |
static void style2() { | |
try (Scanner scanner = new Scanner(System.in)) { | |
System.out.print("please input time1 : "); | |
int hour = scanner.nextInt(), minute = scanner.nextInt(), second = scanner.nextInt(); | |
System.out.print("please input time2 : "); | |
hour += scanner.nextInt(); | |
minute += scanner.nextInt(); | |
second += scanner.nextInt(); | |
minute += second / 60; | |
second %= 60; | |
hour += minute / 60; | |
minute %= 60; | |
System.out.println(String.format("Hour = %s, Minute = %s, Second = %s", hour, minute, second)); | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} | |
} | |
static void style3() { | |
try (Scanner scanner = new Scanner(System.in)) { | |
System.out.print("please input time1 : "); | |
long time = scanner.nextInt() * 60L * 60L; | |
time += scanner.nextInt() * 60L; | |
time += scanner.nextInt(); | |
System.out.print("please input time2 : "); | |
time += scanner.nextInt() * 60L * 60L; | |
time += scanner.nextInt() * 60L; | |
time += scanner.nextInt(); | |
System.out.println(String.format("Hour = %s, Minute = %s, Second = %s", time / (60 * 60), (time / 60) % 60, | |
time % 60)); | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment