If you are using Java 8, you should not use java.util.Date in the first place (unless you received a Date object from a library that you cannot control).
In any case, you can convert Date to java.time.Instant using:
Date date = ...; Instant instant = date.toInstant();
Since you are only interested in date and time without time zone information (I assume that everything is in UTC format), you can convert this moment to a LocalDateTime object:
LocalDateTime ldt = instant.atOffset(ZoneOffset.UTC).toLocalDateTime();
Finally, you can print it with:
DateTimeFormatter fmt = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss"); System.out.println(ldt.format(fmt));
Or use the predefined DateTimeFormatter.ISO_LOCAL_DATE_TIME formatter.
System.out.println(ldt.format(DateTimeFormatter.ISO_LOCAL_DATE_TIME));
Please note that if you do not provide a formatter, calling ldt.toString will produce output in the standard ISO 8601 format (including milliseconds) - this may be acceptable to you.
source share