Java如何打印含时区偏移的指定格式ISO标准日期时间?
Hey there! Let's sort out this date formatting problem for you. The issue with your current code is that you're hardcoding the Z (which represents UTC time) and not properly handling the time zone offset in the format you need (-05:00). Plus, the old Date and SimpleDateFormat classes have known flaws like thread unsafety—so let's cover both modern and legacy solutions.
Recommended: Use Java 8+ Date/Time API (Modern Approach)
Java 8 introduced a far more robust date/time API in the java.time package, built to fix the shortcomings of the old APIs. Here's how to get your desired format easily:
import java.time.ZonedDateTime; import java.time.format.DateTimeFormatter; import java.time.ZoneId; public class IsoDateFormatter { public static void main(String[] args) { // Get current date/time with a specific time zone (e.g., Eastern Time, UTC-5) ZonedDateTime currentDateTime = ZonedDateTime.now(ZoneId.of("America/New_York")); // Define formatter for your target ISO format DateTimeFormatter isoFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSSXXX"); // Format and print the result System.out.println(isoFormatter.format(currentDateTime)); } }
Quick Breakdown:
ZonedDateTime: Captures date/time along with time zone information, which is essential for generating the correct offset like-05:00.SSS: Ensures we get three digits for milliseconds (matching your000requirement).XXX: Generates the time zone offset with a colon (e.g.,-05:00or+02:00), exactly what you need.
If you want to use your system's default time zone instead, just replace ZoneId.of("America/New_York") with ZoneId.systemDefault().
Legacy Solution: Using SimpleDateFormat (For Java 7 or Older)
If you're stuck with the old API, you can adjust your code to handle the time zone offset properly. Note that SimpleDateFormat supports the XXX format specifier starting from Java 7:
import java.util.Date; import java.text.SimpleDateFormat; import java.util.TimeZone; public class LegacyIsoFormatter { public static void main(String[] args) { Date date = new Date(); // Set the desired time zone (e.g., Eastern Time, UTC-5) TimeZone targetTimeZone = TimeZone.getTimeZone("America/New_York"); SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSXXX"); formatter.setTimeZone(targetTimeZone); System.out.println(formatter.format(date)); } }
For Java 6 or Earlier:
If you're on an even older Java version, XXX isn't supported. You'll need a small workaround to add the colon to the offset:
import java.util.Date; import java.text.SimpleDateFormat; import java.util.TimeZone; public class Java6IsoFormatter { public static void main(String[] args) { Date date = new Date(); TimeZone targetTimeZone = TimeZone.getTimeZone("America/New_York"); // Format the date/time part SimpleDateFormat datePartFormatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS"); datePartFormatter.setTimeZone(targetTimeZone); String datePart = datePartFormatter.format(date); // Format the offset part and insert the colon SimpleDateFormat offsetFormatter = new SimpleDateFormat("Z"); offsetFormatter.setTimeZone(targetTimeZone); String rawOffset = offsetFormatter.format(date); String formattedOffset = rawOffset.substring(0, 3) + ":" + rawOffset.substring(3); // Combine and print the final result System.out.println(datePart + formattedOffset); } }
Whichever approach you pick, the core fix is to stop hardcoding Z and ensure your formatter properly accounts for the time zone offset using the right pattern to get the colon-separated format you need.
内容的提问来源于stack exchange,提问作者azaveri7

