-6

How can I accept input from STDIN in the format HH:MM:SSAM here in place of AM , PM can also be there and display output in the form HH:MM:SS

M.L
  • 1
  • by checking if the input format is `HH:MM:SSAM` and using the formatter provided by the java Library to pass the date into a different format. – SomeJavaGuy Jan 15 '16 at 10:00
  • http://stackoverflow.com/questions/999172/how-to-parse-a-date. http://stackoverflow.com/questions/4772425/format-date-in-java. And, welcome to stackoverflow! Please check the FAQ [ask] on how to ask good questions, so that you get good answers. You should at least have tried something yourself and ideally show some **code** of what you have tried. – Andreas Fester Jan 15 '16 at 10:01

1 Answers1

0

The simplest way would probably be to parse it to a LocalTime:

DateTimeFormatter fmt = DateTimeFormatter.ofPattern("hh:mm:ssa");
LocalTime time = LocalTime.parse("10:20:05PM", fmt);
System.out.println("time = " + time); //22:20:05

Note: the a in the format is for AM/PM.

assylias
  • 321,522
  • 82
  • 660
  • 783
  • thanks, if I want to perform an operation hh+12 manually how to do that.. as here it is being done automatically – M.L Jan 15 '16 at 10:19
  • I don't understand what you mean by "perform an operation hh+12 manually"... – assylias Jan 15 '16 at 10:26
  • I need to add 12 to the HH value if the input time is PM – M.L Jan 15 '16 at 11:08
  • @M.L. My example already does that... – assylias Jan 15 '16 at 11:10
  • yeah I saw that but in the example provided by you, it is done automatically but if we want it to be done manually then?? – M.L Jan 15 '16 at 11:17
  • @M.L If you mean that you are not allowed to use a built-in class like LocalTime, then you need to parse the string manually and manipulate the content. – assylias Jan 15 '16 at 11:18