user3788063
user3788063

Reputation: 43

converting seconds to hours,minutes, and seconds?

so Im trying to make a program that can convert s from input into h, m and s. my code so far looks like this:

import java.util.Scanner;

class q2_5{
  public static void main(String args[]){

    Scanner input = new Scanner(System.in);
    int s=0;//seconds
    int m=0;//minutes
    int h=0;//hour
    System.out.println("how many seconds?");
    s=input.nextInt();
    if(s >= 60){
      m=s/60;
    } if(m>=60){
      h=m/60;
    }
    System.out.println(s + "s = " + h + " h " + m + " m " + s + "s ");
  }
}

ok so I had to initialize s,m,h to 0 cuz if not I was getting problems in the if statement, so I just put it to 0, since I can change it later :) ok. so the problem with this program right now is that if I type in 3603 I get this output: 3603s = 1 h 60 m 3603s, if I type in 3600 I get this: 3600s = 1 h 60 m 3600s, but the output should have been 3603s = 1h 0m 3s and 3600s = 1h 0m 0s respectively. any tips/advice/solutions on how to solve this problem? :D thanks in advance!

Upvotes: 3

Views: 28385

Answers (11)

Basil Bourque
Basil Bourque

Reputation: 338276

Obviously you are doing some homework/practice to learn Java. But FYI, in real work you needn't do those calculations and manipulations. There’s a class for that.

java.time.Duration

For a span of time, use the Duration class. No need for you to do the math.

Duration d = Duration.ofSeconds( s );

The standard ISO 8601 format for a string representing a span of time unattached to the timeline is close to your desired output: PnYnMnDTnHnMnS where the P marks the beginning and the T separates any years-months-days from any hours-minutes-seconds. So an hour and a half is PT1H30M.

The java.time classes use the ISO 8601 formats by default when parsing or generating strings.

String output = d.toString() ;

In Java 9 and later, you can call the to…Part methods to retrieve the individual parts of hours, minutes, and seconds.


About java.time

The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as java.util.Date, Calendar, & SimpleDateFormat.

To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.

The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.

You may exchange java.time objects directly with your database. Use a JDBC driver compliant with JDBC 4.2 or later. No need for strings, no need for java.sql.* classes. Hibernate 5 & JPA 2.2 support java.time.

Where to obtain the java.time classes?

Table of which java.time library to use with which version of Java or Android

Upvotes: 6

roger_that
roger_that

Reputation: 9791

A simpler solution would be:

T = in.nextInt();

int s = T%60;

int minute = T/60;

int m = minute%60;

int h = minute/60;

return h+"h"+m+"m"+s+"s";

Upvotes: 0

Durgesh Pal
Durgesh Pal

Reputation: 695

class time{
    public static void main (String args[]){
        int duration=1500;
         String testDuration = "";

        if(duration < 60){
            testDuration = duration + " minutes";
        }
        else{

            if((duration / 60)<24)
            {
                if((duration%60)==0){
                    testDuration = (duration / 60) + " hours";
                }
                else{
            testDuration = (duration / 60) + " hours," + (duration%60) + " minutes";
                }
            }
            else{

                if((duration%60)==0){
                    if(((duration/60)%24)==0){
                        testDuration = ((duration / 24)/60) + " days,";

                    }
                    else{
                    testDuration = ((duration / 24)/60) + " days," + (duration/60)%24 +"hours";
                    }
                }
                    else{
                testDuration = ((duration / 24)/60) + " days," + (duration/60)%24 +"hours"+ (duration%60) + " minutes";
                    }
            }
        }

        System.out.println(testDuration);
    }
}

Upvotes: 0

Kandarp
Kandarp

Reputation: 965

import java.util.Scanner;

public class Prg2 {

    public static void main(String[] args) {
        int seconds = 0;
        int hours = 0;
        int minutes = 0;
        int secondsDisp = 0;
        Scanner in = new Scanner(System.in);
        System.out.println("Enter the number of seconds: ");
        seconds = in.nextInt();
        hours = seconds/3600;
        minutes = (seconds - (hours*3600))/60;
        secondsDisp = ((seconds - (hours*3600)) - (minutes * 60));
        System.out.println(seconds + " seconds equals " + hours + " hours, " + minutes + " minutes and " + secondsDisp + " seconds");
    }
}

Upvotes: 0

Shohan Ahmed Sijan
Shohan Ahmed Sijan

Reputation: 4531

My solution is:

 String secToTime(int sec) {
    int second = sec % 60;
    int minute = sec / 60;
    if (minute >= 60) {
        int hour = minute / 60;
        minute %= 60;
        return hour + ":" + (minute < 10 ? "0" + minute : minute) + ":" + (second < 10 ? "0" + second : second);
    }
    return minute + ":" + (second < 10 ? "0" + second : second);
}

Upvotes: 5

Felipe Martins Melo
Felipe Martins Melo

Reputation: 1333

Java 8 brings a great API for date and time manipulation.

import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;

public class Converter {

    DateTimeFormatter formatter = DateTimeFormatter.ofPattern("HH:mm:ss");

    public String convert(int seconds) {
        LocalTime time = LocalTime.MIN.plusSeconds(seconds).toLocalTime();

        return formatter.format(time);
    }
}

Basically, this code adds the number of number of seconds to the minimum datetime supported, which, naturally, has HH:mm:ss equals to 00:00:00.

As your are interested in the time only, you extract it by calling toLocalTime() and format it as wanted.

Upvotes: 4

High Zedd
High Zedd

Reputation: 55

You should try this

import java.util.Scanner;
public class Time_converter {


    public static void main(String[] args) 
    {

        Scanner input = new Scanner (System.in);
        int seconds;
        int minutes ;
        int hours;
        System.out.print("Enter the number of seconds : ");
        seconds = input.nextInt();
        hours = seconds / 3600;
        minutes = (seconds%3600)/60;
        int seconds_output = (seconds% 3600)%60;


        System.out.println("The time entered in hours,minutes and seconds is:");
        System.out.println(hours  + " hours :" + minutes + " minutes:" + seconds_output +" seconds"); 
    }

}

Upvotes: 0

drum
drum

Reputation: 5651

You never changed the value of s. A quick work around would be s = s - (h*3600 + m*60)

EDIT: t = s - (h*3600 + m*60)

Upvotes: 1

Yagel
Yagel

Reputation: 75

use this code:

import java.util.Scanner;
class q2_5{
public static void main(String args[]){

Scanner input = new Scanner(System.in);
int s=0;//seconds
int m=0;//minutes
int h=0;//hour
int s_input=0;
System.out.println("how many seconds?");
s_input=input.nextInt();
s=s_input%60;
if(s >= 60){

m=s_input/60;
}if(m>=60){

h=m/60;
m=m%60;
}
System.out.println(s + "s = " + h + " h " + m + " m " + s + "s ");
 }
}

Upvotes: -1

jcaron
jcaron

Reputation: 17710

You can do it all in a single line:

System.out.println((s/3600) + ' hours ' + ((s/60)%60) + ' minutes ' + (s%60) + ' seconds');

Upvotes: 2

Kyranstar
Kyranstar

Reputation: 1720

Try subtracting from the first term every time you set a lower term.

class Test{
  public static void main(String args[]){

    Scanner input = new Scanner(System.in);
    int s=0;//seconds
    int m=0;//minutes
    int h=0;//hour
    System.out.println("how many seconds?");
    s=input.nextInt();
    if(s >= 60){
      m=s/60;
      s = s- m*60;
    } if(m>=60){
      h=m/60;
      m = m - h*60;
    }
    System.out.println(s + "s = " + h + " h " + m + " m " + s + "s ");
  }
}

Just by the way, too, remember to close your input scanner! Like so : input.close() near the end of the program.

Upvotes: 0

Related Questions