Format LocalDateTime To String

April 20, 2018
// Sample output: 20180420-121341
LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMMdd-kkmmss"))

Check for format pattern

You can also used pre-defined DateTimeFormatter

  • ISO_LOCAL_DATE_TIME: e.g. 2011-12-03T10:15:30.111222
  • BASIC_ISO_DATE: e.g. 20111203
  • ISO_LOCAL_DATE: e.g. 2011-12-03
  • ISO_LOCAL_TIME: e.g. 10:15:30
  • ISO_OFFSET_DATE_TIME: e.g. 2011-12-03T10:15:30+01:00
  • RFC_1123_DATE_TIME: e.g. Tue, 3 Jun 2008 11:05:30 GMT
// Sample output: 2018-04-10T03:34:18.115
LocalDateTime.now().format(DateTimeFormatter.ISO_LOCAL_DATE_TIME)
This work is licensed under a
Creative Commons Attribution-NonCommercial 4.0 International License.