I have a system using Java 7 and I need to generate a date equivalente to Instant.now
(Java 8).
For example, Instant.now().toString()
generate a date like that:
"2018-12-19T12:32:46.816Z"
Using java.util.Date
I have this date: "2018-12-19T10:38:13.892"
Date date = new Date(System.currentTimeMillis());
SimpleDateFormat sdf;
sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS");
String text = sdf.format(date);
System.out.println(text);
I don't know if I can simply concatenate a "Z" at the end of this date.
Without "Z" another system that parse this date using Instant.parse
throws the error:
java.time.format.DateTimeParseException: Text '2018-12-19T10:38:13.892' could not be parsed at index 23 at java.base/java.time.format.DateTimeFormatter.parseResolved0(DateTimeFormatter.java:1988) at java.base/java.time.format.DateTimeFormatter.parse(DateTimeFormatter.java:1890) at java.base/java.time.Instant.parse(Instant.java:395)