Home » Java programs » Java basic programs

Java program to convert given number of seconds to hours, minutes and second

In this java program, we are reading number of seconds from the user and converting the seconds into hours, minutes and seconds and printing in HH:MM:SS format.
Submitted by Chandra Shekhar, on February 03, 2018

Given seconds and we have to convert it into hours, minutes and seconds using java program.

Example:

    Input:
    Input seconds: 6530
    Output:
    HH:MM:SS  - 1:48:50

    Test: Convert HH:MM:SS to seconds again,
    1*60*60 + 48*60 + 50 = 3660 + 2880 + 50 = 6530

Convert seconds to hours, minutes and seconds in java

import java.util.Scanner;

public class SecondstoHrMinSec 
{
	public static void main(String[] args)
	{
		// create object of scanner class.
		Scanner in = new Scanner(System.in);

		// enter the seconds here.
		System.out.print("Enter seconds : ");

		int seconds = in.nextInt(); 

		int p1 = seconds % 60;
		int p2 = seconds / 60;
		int p3 = p2 % 60;

		p2 = p2 / 60;

		System.out.print("HH:MM:SS - " +p2 + ":" + p3 + ":" + p1);
		System.out.print("\n");
	}  
}

Output

Enter seconds : 6530
HH:MM:SS - 1:48:50






Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.





Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com some rights reserved.