0

I'm trying to take in a date in 12H format and print it out in 24H format. Here's my code.

public static void main(String[] args)throws Exception {

        Scanner sc = new Scanner(System.in);
        String str = sc.nextLine();
        SimpleDateFormat sdf = new SimpleDateFormat("hh:mm:ss a");
        Date date = sdf.parse(str);
            System.out.println(sdf.format(date));


    }

I keep getting the following error.

Exception in thread "main" java.text.ParseException: Unparseable date: "07:05:45PM"
    at java.text.DateFormat.parse(DateFormat.java:366)
    at Solution.main(Solution.java:12)
Zeus
  • 2,213
  • 8
  • 28
  • 45
  • 1
    Spaces are important. – Tunaki Feb 10 '16 at 14:37
  • And the current `Locale`. – Tom Feb 10 '16 at 14:40
  • try { String now = new SimpleDateFormat("hh:mm aa").format(new java.util.Date().getTime()); System.out.println("time in 12 hour format : " + now); SimpleDateFormat inFormat = new SimpleDateFormat("hh:mm aa"); SimpleDateFormat outFormat = new SimpleDateFormat("HH:mm"); String time24 = outFormat.format(inFormat.parse(now)); System.out.println("time in 24 hour format : " + time24); } catch (Exception e) { System.out.println("Exception : " + e.getMessage()); } – Akhil Feb 10 '16 at 14:47

0 Answers0