Skip to content

Instantly share code, notes, and snippets.

@enseitankad0
Created February 28, 2018 22:44
Show Gist options
  • Save enseitankad0/a5c4893d6a7b0ac8cc4834edce4180e5 to your computer and use it in GitHub Desktop.
Save enseitankad0/a5c4893d6a7b0ac8cc4834edce4180e5 to your computer and use it in GitHub Desktop.
Simple time converted using 2 methods. An input is seconds
package com.kamilszufnara;
//import java.time.LocalTime;
import java.time.LocalTime;
public class Main {
public static void main(String[] args) {
//for(int i=0; i<=1000; i+=15)
//
{
// input i is time in seconds
// it's being converted to format HH:MM:SS
// Method 1
int i = 590;
int a = i / 3600;
int b = (i - a * 3600) / 60;
int c = (i - a * 3600) % 60;
System.out.format("%02d:%02d:%02d \n", a, b, c);
// Method 2 - no need to import java.time in 9th version od Java
LocalTime timeOfDay = LocalTime.ofSecondOfDay(i);
String time = timeOfDay.toString();
System.out.println(time);
// the result is the same 23:53:20
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment