5

I'm very new to Joda api and I have the call like this:

 LocalTime time = new LocalTime("13");

it prints as:13:00:00.000.

I wish I could display it like this: 1:00 PM.

How I can achieve that?

Thanks in advance

Freiheit
  • 8,408
  • 6
  • 59
  • 101
batman
  • 4,728
  • 8
  • 39
  • 45

2 Answers2

2

Try the following:

DateTimeFormatter builder = DateTimeFormat.forPattern("hh:mm:ss.SSa");
ltalhouarne
  • 4,586
  • 2
  • 22
  • 31
  • its throwing me an error:Exception in thread "main" java.lang.IllegalArgumentException: Invalid format: "09:00:00.000" is malformed at "0" for new LocalTime("9"), when I call builder.parseLocalTime(time.toString()) – batman Sep 03 '14 at 13:58
  • @batman Why do you want to parse now? In your question you speak about formatting/printing, and this answer is sufficient for that purpose. If you want to parse a string in another format then you need another pattern, of course. – Meno Hochschild Sep 03 '14 at 15:01
  • 2
    @StackFlowed I dislike the "holier than thou, gatekeepers of knowledge" attitude. There's a difference btwn documentation and knowledge. That's why classes are taught with both teachers and books. – ekeyser Jun 26 '15 at 18:16
2

I did this:

    LocalTime time = new LocalTime("13");    
    DateTimeFormatter fmt = DateTimeFormat.forPattern("h:mm a");
    String str = fmt.print(time);

And got this output for str:

"1:00 PM"
Jeff Anderson
  • 799
  • 7
  • 18
  • Giving me error like this its throwing me an error:Exception in thread "main" java.lang.IllegalArgumentException: Invalid format: "09:00:00.000" is malformed at "0" for new LocalTime("9"), when I call builder.parseLocalTime(time.toString()) – batman Sep 03 '14 at 14:18