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");
}
}