To convert the 12 - hour time to 24 - hour time, we need to perform string manipulation in Java. We will extract the hour, minute, second, and AM/PM indicator from the input string and then convert the hour based on the AM/PM indicator.
import java.util.Scanner;
public class TimeConversion {
public static String timeConversion(String s) {
// Extract the AM/PM indicator
String amPm = s.substring(8);
// Extract the hour, minute, and second
String timePart = s.substring(0, 8);
String[] parts = timePart.split(":");
int hour = Integer.parseInt(parts[0]);
int minute = Integer.parseInt(parts[1]);
int second = Integer.parseInt(parts[2]);
if (amPm.equals("PM") && hour != 12) {
// If it is PM and not 12 PM, add 12 to the hour
hour += 12;
} else if (amPm.equals("AM") && hour == 12) {
// If it is AM and 12 AM, set the hour to 0
hour = 0;
}
// Format the hour, minute, and second to have two digits
String formattedHour = String.format("%02d", hour);
String formattedMinute = String.format("%02d", minute);
String formattedSecond = String.format("%02d", second);
return formattedHour + ":" + formattedMinute + ":" + formattedSecond;
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String s = scanner.nextLine();
String result = timeConversion(s);
System.out.println(result);
scanner.close();
}
}
timeConversion
Method:substring(8)
.substring(0, 8)
and split it using the split
method.String.format("%02d", ...)
and return the converted time.main
Method:Scanner
object to read the input from the user.timeConversion
method with the input string and print the result.hh:mm:ssAM
or hh:mm:ssPM
). If the input is not in the correct format, the code may throw a NumberFormatException
or produce incorrect results.substring
and split
methods can lead to incorrect extraction of the hour, minute, second, and AM/PM indicator.java.time
package, which provides a more robust and flexible way to handle date and time. You can use the DateTimeFormatter
class to parse and format time in different formats.import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
public class TimeConversionBestPractice {
public static String timeConversionBestPractice(String s) {
DateTimeFormatter inputFormatter = DateTimeFormatter.ofPattern("hh:mm:ssa");
DateTimeFormatter outputFormatter = DateTimeFormatter.ofPattern("HH:mm:ss");
LocalTime time = LocalTime.parse(s, inputFormatter);
return time.format(outputFormatter);
}
public static void main(String[] args) {
String s = "03:45:23PM";
String result = timeConversionBestPractice(s);
System.out.println(result);
}
}
Converting 12 - hour time to 24 - hour time in Java is a fundamental problem that requires an understanding of string manipulation and time format conversion. By following the best practices and avoiding common pitfalls, you can write robust and efficient code to solve this problem. Whether you are solving a HackerRank challenge or working on a real - world application, this knowledge will be valuable.
A1: The 24 - hour format is preferred because it is more precise and eliminates the confusion between AM and PM. It is also widely used in international contexts and in scheduling systems.
A2: If the input string is not in the correct format, the code may throw a NumberFormatException
or produce incorrect results. You can add input validation using regular expressions to handle such cases.
java.util.Date
class to solve this problem?A3: While you can use the java.util.Date
class, it is recommended to use the java.time
package introduced in Java 8, as it provides a more modern and user - friendly API for handling date and time.