fork(1) download
  1. import java.time.LocalDateTime;
  2. import java.time.format.DateTimeFormatter;
  3. import java.time.format.DateTimeFormatterBuilder;
  4. import java.util.Locale;
  5.  
  6. class Main {
  7. public static void main(String[] args) {
  8. DateTimeFormatter dtf = new DateTimeFormatterBuilder()
  9. .append(DateTimeFormatter.ISO_LOCAL_DATE)
  10. .appendLiteral(' ')
  11. .append(DateTimeFormatter.ISO_LOCAL_TIME)
  12. .toFormatter(Locale.ENGLISH);
  13.  
  14. String strDateTime = "2014-04-08 12:30";
  15. LocalDateTime ldt = LocalDateTime.parse(strDateTime, dtf);
  16. System.out.println(ldt);
  17.  
  18. // However, formatting the obtained LocalDateTime using this DateTimeFormatter
  19. // will return a string with time in HH:mm:ss format. To restrict the time
  20. // string to HH:mm format, we still have to use the pattern, uuuu-MM-dd HH:mm as
  21. // other answers have done.
  22. String strDateTimeFormatted = ldt.format(dtf);
  23. System.out.println(strDateTimeFormatted);
  24.  
  25. DateTimeFormatter formatter = DateTimeFormatter.ofPattern("uuuu-MM-dd HH:mm", Locale.ENGLISH);
  26. strDateTimeFormatted = ldt.format(formatter);
  27. System.out.println(strDateTimeFormatted);
  28. }
  29. }
Success #stdin #stdout 0.13s 48920KB
stdin
Standard input is empty
stdout
2014-04-08T12:30
2014-04-08 12:30:00
2014-04-08 12:30