Weather Observation Station 5

  • + 0 comments

    JAVA

    public static String timeConversion(String s) {
        // Write your code here
            String period = s.substring(s.length()-2);
            String timeWithoutPeriod = s.substring(0, s.length() - 2);
            String[] timeParts = timeWithoutPeriod.split(":");
            
            int hour = Integer.parseInt(timeParts[0]);
            int minute = Integer.parseInt(timeParts[1]);
            int second = Integer.parseInt(timeParts[2]);
            
            if (period.equals("AM") && hour == 12) {
                hour = 0; 
            } else if (period.equals("PM") && hour != 12) {
                hour += 12; 
            }
    
            // Format the time in 24-hour format (HH:mm:ss)
            return String.format("%02d:%02d:%02d", hour, minute, second);
        
        }