You can not format the java.time
types using the legacy formatting type, java.text.DateFormat
. Use java.time.format.DateTimeFormatter
instead.
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
public class Main {
public static void main(String[] args) {
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMddHHmmss");
System.out.println(formatter.format(LocalDateTime.now().plusMinutes(10)));
}
}
Output:
20200415203347
Note that the java.time
API, released with Java-8 in March 2014, supplanted the error-prone legacy date-time API. Since then, using this modern date-time API has been strongly recommended. Learn more about the modern Date-Time API from Trail: Date Time.