As said in other answers, you should use yyyy
instead of YYYY
. If you are woking with java8 or higher, you can also use DateTimeFormatterBuilder
to avoid the raw pattern, for example:
DateTimeFormatter formatter = new DateTimeFormatterBuilder()
.appendValue(ChronoField.YEAR_OF_ERA, 4, 19, SignStyle.EXCEEDS_PAD)
.appendLiteral("-")
.appendText(ChronoField.MONTH_OF_YEAR, TextStyle.SHORT)
.appendLiteral("-")
.appendValue(ChronoField.DAY_OF_MONTH, 2)
.appendLiteral(" ")
.appendValue(ChronoField.CLOCK_HOUR_OF_AMPM, 2)
.appendLiteral(":")
.appendValue(ChronoField.MINUTE_OF_HOUR, 2)
.appendLiteral(":")
.appendValue(ChronoField.SECOND_OF_MINUTE, 2)
.appendLiteral(" ")
.appendText(ChronoField.AMPM_OF_DAY, TextStyle.SHORT)
.toFormatter();
LocalDateTime localDateTime = LocalDateTime.now();
System.out.println(formatter.format(localDateTime));