Time Conversion

  • + 0 comments

    Simple Solution in Java 7

    public static String timeConversion(String s) {
        String format  =  s.substring(8);
        int hours  =  Integer.parseInt(s.substring(0,2));
        String military  =  "";
    		
        DecimalFormat df = new DecimalFormat("00");
        if(format.equals("PM")){
            if(hours >= 1 &&  hours  <= 11)
                hours = hours+12;
            military = df.format(hours)+s.substring(2,8);
        }
        else{
            if(hours == 12)
                hours = 0;
            military = df.format(hours)+s.substring(2,8);
        }
        return military;
        }