0

All this is the output i'm getting from my code:

Enter hours: 12

Enter minutes: 60

Enter seconds: 60

Enter AM or PM: pm

24 hour clock time: 25:01:00

and my expected output is :

Enter hours: 12

Enter minutes: 60

Enter seconds: 60

Enter AM or PM: pm



24 hour clock time: 13:01:00

This is the function responsible for converting the time, where I'm entering the hr, min, sec and str i.e. am or pm from the user :

public static void print24HourTime(int hr, int min, int sec, String str)
{
    //formatting the hours, minutes, seconds to 24 hour clock
    int hours = (hr + (sec / 60 + min) / 60) % 24;
    int minutes = (min + sec / 60) % 60;
    int seconds = sec % 60;
    if(minutes > 59)
    {
        minutes = 00;
        hours = hours + 1;
    }
    if(seconds > 59)
    {
        seconds = 00;
        minutes = minutes + 1;
    }
    if (str.toUpperCase().equals("AM"))
    {
        if(hours == 12)
        {
            hours = 00;
            System.out.printf("24 hour clock time: %02d:%02d:%02d%n", hours, minutes, seconds);
        }
        else
        {
            System.out.printf("24 hour clock time: %02d:%02d:%02d%n", hours, minutes, seconds);
        }
    }
    else if(str.toUpperCase().equals("PM"))
    {
        if(hours == 12)
        {
            hours = 12;
            System.out.printf("24 hour clock time: %02d:%02d:%02d%n", hours, minutes, seconds);
        }
        else
        {
            System.out.printf("24 hour clock time: %02d:%02d:%02d%n", hours + 12, minutes, seconds);
        }
    }
    else
    {
        System.out.println("Invalid AM or PM");
    }
}
khelwood
  • 55,782
  • 14
  • 81
  • 108
Jonardan Cena
  • 373
  • 3
  • 11
  • The code isn't terribly clear, but the 12-hr math should be done *after* checking AM or PM. – Dave Newton May 25 '21 at 14:40
  • By the way, `00` is equivalent to just `0`, but in general you should avoid putting leading zeroes on an int literal. – khelwood May 25 '21 at 14:42
  • Do you have to write your code on your own? Otherwise check out SimpleDateFormat https://stackoverflow.com/questions/4216745/java-string-to-date-conversion/4216767#4216767 – Jordi Laforge May 25 '21 at 14:42
  • Yes @JordiLaforge , I have to write the code on my own. – Jonardan Cena May 25 '21 at 14:43
  • 2
    Your input 12:60:60 pm itself is faulty. – Kitswas May 25 '21 at 14:43
  • 1
    The seconds and minutes must be within 0 to 59 and the other must be within 1 to 12. – Kitswas May 25 '21 at 14:45
  • You have this test, `if(hours == 12)` but because you added 1 hour due to there being 60 minutes your hours == 13. Also, because you're doing minutes first you could have 59 minutes and 60 seconds. The seconds will add a minute and then you'll have 60 minutes that never gets fixed. – matt May 25 '21 at 15:06

1 Answers1

1

You can use java.time.LocalTime and java.time.format.DateTimeFormatter to achieve it.

import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeParseException;
import java.util.Locale;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        System.out.print("Enter hours: ");
        int hours = Integer.parseInt(scanner.nextLine());

        System.out.print("Enter minutes: ");
        int minutes = Integer.parseInt(scanner.nextLine());

        System.out.print("Enter seconds: ");
        int seconds = Integer.parseInt(scanner.nextLine());

        System.out.print("Enter AM or PM: ");
        String amPm = scanner.nextLine();

        print24HourTime(hours, minutes, seconds, amPm);
    }

    public static void print24HourTime(int hr, int min, int sec, String str) {
        String strTime = hr + ":" + min + ":" + sec + " " + str;
        DateTimeFormatter dtf = DateTimeFormatter.ofPattern("h:m:s a", Locale.ENGLISH);
        try {
            LocalTime time = LocalTime.parse(strTime, dtf);
            System.out.println("24 hour clock time: " + time);
        } catch (DateTimeParseException e) {
            System.out.println("Invalid time");
        }
    }
}

A sample run:

Enter hours: 10
Enter minutes: 20
Enter seconds: 30
Enter AM or PM: AM
24 hour clock time: 10:20:30

Another sample run:

Enter hours: 10
Enter minutes: 20
Enter seconds: 30
Enter AM or PM: PM
24 hour clock time: 22:20:30

Another sample run:

Enter hours: 30
Enter minutes: 20
Enter seconds: 10
Enter AM or PM: AM
Invalid time
Arvind Kumar Avinash
  • 71,965
  • 6
  • 74
  • 110
  • Please see [this comment](https://stackoverflow.com/questions/67690151/formatting-the-time-to-24-hour-in-java-by-taking-the-input-from-the-user#comment119646405_67690151). – Kitswas May 25 '21 at 14:46
  • Thank you for your time. But i am bound to not to import any packages. – Jonardan Cena May 25 '21 at 14:51
  • @JonardanCena If this is a schoolwork assignment, with artificial limits, you should say so in your Question. – Basil Bourque May 26 '21 at 01:06