fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6.  
  7. import java.time.*;
  8. import java.time.format.*;
  9. import java.time.temporal.*;
  10.  
  11. /* Name of the class has to be "Main" only if the class is public. */
  12. class Ideone
  13. {
  14. public static void main (String[] args) throws java.lang.Exception
  15. {
  16.  
  17. // Input
  18. String input = "05:18 AM" ;
  19. Locale locale = Locale.US ;
  20. DateTimeFormatter fTimeUS = DateTimeFormatter.ofLocalizedTime( FormatStyle.SHORT ).withLocale( locale );
  21. LocalTime lt = LocalTime.parse( input , fTimeUS );
  22.  
  23. // Same input can also be parsed as Spanish as well as English.
  24. DateTimeFormatter fTimeSpanishSpain = DateTimeFormatter.ofLocalizedTime( FormatStyle.SHORT ).withLocale( new Locale("es_ES") );
  25. LocalTime lt2 = LocalTime.parse( input , fTimeSpanishSpain );
  26.  
  27. System.out.println( "input: " + input );
  28. System.out.println( "lt.toString(): " + lt );
  29. System.out.println( "lt2.toString(): " + lt2 );
  30.  
  31. // Output
  32. String output = lt.format( fTimeSpanishSpain );
  33.  
  34. System.out.println( "output: " + output );
  35.  
  36. }
  37. }
Success #stdin #stdout 0.11s 712192KB
stdin
Standard input is empty
stdout
input: 05:18 AM
lt.toString(): 05:18
lt2.toString(): 05:18
output: 5:18 AM