Java time to seconds Example

Java time to seconds Example


Posted in : Java Posted on : August 18, 2011 at 5:44 PM Comments : [ 0 ]

In this tutorial you will learn how can you change a time value into seconds.

Java time to seconds Example

In this tutorial you will learn how can you change a time value into seconds.

Here I am giving a simple example for changing a time into seconds. In the example given below I have done some mathematical processes such as to change the hour into seconds multiplied the hour by 3600 because an hour is of 3600 sec., to change minute into second multiplied the minute by 60 because 1 minute = 60 sec.

Example :

import java.util.*;

class TimeToSec
{
static int h;
static int m;
static int s;
public TimeToSec()
{
int h = 0;
int m = 0;
int s = 0;
} 
public static void main(String arr[])
{
int sec;
System.out.println("Conversion of time value into second(s)");
System.out.println("Enter time value :");

Scanner scan = new Scanner(System.in);

System.out.println("Enter the value of hour : ");
h= scan.nextInt();
System.out.println("Enter the value of minute : ");
m = scan.nextInt(); 
System.out.println("Enter the value of second : ");
s = scan.nextInt();
sec = h*3600+m*60+s;
System.out.println("Time after calculating into second(s) : "); 
System.out.println(sec);
}
}

Output :

When you will execute the above example you will get the output as :

Download Source Code

Go to Topic «PreviousHomeNext»

Your Comment:


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 

 
Tutorial Topics