Reputation: 1
I am trying to create a time conversion function that converts 12-hour time formats to 24-hour formats. My code works for all inputs except the following: 12:05:39AM.
I am having trouble understanding what I am doing wrong
import java.io.*;
import java.math.*;
import java.security.*;
import java.text.*;
import java.util.*;
import java.util.concurrent.*;
import java.util.function.*;
import java.util.regex.*;
import java.util.stream.*;
import static java.util.stream.Collectors.joining;
import static java.util.stream.Collectors.toList;
class Result {
/*
* Complete the 'timeConversion' function below.
*
* The function is expected to return a STRING.
* The function accepts STRING s as parameter.
*/
public static String timeConversion(String s) {
// Write your code here
if (s.substring(8).equals("PM")){
String hour = s.substring(0,2);
if (Integer.parseInt(hour) != 12){
int newHour = (Integer.parseInt(hour) + 12) % 24;
return (newHour + s.substring(2,8));
}
return (hour + s.substring(2,8));
}
else if (s.substring(8).equals("AM")){
String AMhour = s.substring(0,2);
if (Integer.parseInt(AMhour) == 12){
int newHour = ((Integer.parseInt(AMhour) + 12) % 24);
return (newHour + s.substring(2,8));
}
return (AMhour + s.substring(2,8));
}
else {
return("Not found");
}
}
}
Upvotes: 0
Views: 553
Reputation: 390
To convert time from a 12-hour AM/PM format to 24-hour) time, you need to handle the AM and PM cases properly:
class Result
{
/*
* Complete the 'timeConversion' function below.
*
* The function is expected to return a STRING.
* The function accepts STRING s as parameter.
*/
public static String timeConversion(String s)
{
// Write your code here
String period = time.substring(time.length() - 2); // Last two characters am or pm)
String timeWithoutPeriod = time.substring(0, time.length() - 2); // Time without am or pm
String[] timeParts = timeWithoutPeriod.split(":"); // Split into hour, minute, second
int hour = Integer.parseInt(timeParts[0]); // Get the hour
String minutes = timeParts[1]; // Get the minutes
String seconds = timeParts[2]; // Get the seconds
if (period.equals("AM")) {
if (hour == 12) {
hour = 0; // Midnight case: 12 AM -> 00 in 24 hour time
}
} else if (period.equals("PM")) {
if (hour != 12) {
hour += 12;
}
}
// Return the formatted time
return String.format("%02d:%s:%s", hour, minutes, seconds);
}
}
Upvotes: 0
Reputation: 2937
The problem is in this segment:
if (Integer.parseInt(AMhour) == 12){
int newHour = ((Integer.parseInt(AMhour) + 12) % 24);
return (newHour + s.substring(2,8));
}
You're getting 0
in newHour
, and when you're converting it into string, you're getting 0:mm:ss
.
To solve this, type of problem you can add an extra 0
in the front as this is a fixed case. Or, just do:
return String.format("%02d%s", newHour, s.substring(2,8));
NOTE: USE java.time
Please use actual java libraries to deal with these type of conversions. You could do the conversion with java.time
by doing:
return LocalTime.parse(s, DateTimeFormatter.ofPattern("hh:mm:ssa", Locale.US))
.format(DateTimeFormatter.ofPattern("HH:mm:ss"));
Upvotes: 2
Reputation: 631
A simple example of Date and Time formatting:
public static void main(String[] args) {
String timePattern = "H:mm:ss SSS";
DateTimeFormatter timeFormatter = DateTimeFormatter.ofPattern(timePattern);
LocalDateTime ldt = LocalDateTime.now(ZoneId.systemDefault());
System.out.println("The date is: " +DateTimeFormatter.ofLocalizedDate(FormatStyle.FULL).format(ldt));
System.out.println("The time is: " + timeFormatter.format(ldt));
}
Upvotes: 0
Reputation: 79015
I recommend you do it using java.time
, the modern Date-Time API.
Demo:
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.Locale;
public class Main {
public static void main(String[] args) {
// Test
System.out.println(timeConversion("12:05:39AM"));
}
public static String timeConversion(String s) {
DateTimeFormatter dtfInput = DateTimeFormatter.ofPattern("h:m:sa", Locale.ENGLISH);
LocalTime time = LocalTime.parse(s, dtfInput);
DateTimeFormatter dtfOutput = DateTimeFormatter.ofPattern("HH:mm:ss", Locale.ENGLISH);
return time.format(dtfOutput);
}
}
Output:
00:05:39
Learn more about the modern Date-Time API* from Trail: Date Time.
* For any reason, if you have to stick to Java 6 or Java 7, you can use ThreeTen-Backport which backports most of the java.time functionality to Java 6 & 7. If you are working for an Android project and your Android API level is still not compliant with Java-8, check Java 8+ APIs available through desugaring and How to use ThreeTenABP in Android Project.
Upvotes: 1