Date: 2019jun20
Update: 2025sep29
Language: Java
Keywords: GMT, UTC
Q. Java: Format date/time as ISO 8601 with 'T'
A. Use SimpleDateFormat as shown in this full example:
import java.text.SimpleDateFormat;
import java.util.Locale;
import java.util.TimeZone;
import java.util.Date;
class Demo {
private static SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'", Locale.US);
static String iso8601Date(final Date date) {
fmt.setTimeZone(TimeZone.getTimeZone("UTC"));
return fmt.format(date);
}
public static final void main(String []args) {
var now = new Date();
System.out.println("out=" + iso8601Date(now));
}
}
Output (at one time):
out=2025-09-29T14:28:36.214Z
Notice this is UTC time as indicated by the Z