杰瑞科技汇

java date to string

Of course! Converting a java.util.Date object to a String is a very common task in Java. The modern and recommended way to do this is using the java.time package, introduced in Java 8. I'll cover this first, and then show the older java.text.SimpleDateFormat method for completeness.

java date to string-图1
(图片来源网络,侵删)

The Modern Way: java.time (Java 8 and later)

This is the best approach. The java.time package is immutable, thread-safe, and much easier to use correctly.

The main classes you'll use are:

  • java.time.Instant: Represents a specific moment on the timeline in UTC.
  • java.time.ZonedDateTime: Represents a date and time with a time zone (e.g., "2025-10-27T10:15:30+02:00[Europe/Paris]").
  • java.time.format.DateTimeFormatter: The class used to format dates and times into strings.

Scenario A: Your Date object has no time zone information (UTC)

If you have a java.util.Date, it represents a moment in time but doesn't carry time zone information. It's essentially a wrapper around the number of milliseconds since the Unix epoch (January 1, 1970, 00:00:00 UTC).

You should convert it to an Instant, which is the modern equivalent.

java date to string-图2
(图片来源网络,侵删)
import java.util.Date;
import java.time.Instant;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
public class DateToStringModern {
    public static void main(String[] args) {
        // 1. Get a java.util.Date object (e.g., from an old API)
        Date date = new Date();
        // 2. Convert it to an Instant (the modern UTC representation)
        Instant instant = date.toInstant();
        // 3. Define a formatter with a desired pattern
        // Pattern: yyyy-MM-dd HH:mm:ss
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")
                                                     .withZone(ZoneId.systemDefault()); // Use the system's default time zone
        // 4. Format the Instant to a String
        String formattedDate = formatter.format(instant);
        System.out.println("Original java.util.Date: " + date);
        System.out.println("Formatted String:       " + formattedDate);
        // --- Example with a different time zone ---
        DateTimeFormatter formatterUtc = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")
                                                        .withZone(ZoneId.of("UTC"));
        String formattedUtc = formatterUtc.format(instant);
        System.out.println("Formatted UTC String:   " + formattedUtc);
    }
}

Common Patterns for DateTimeFormatter:

Symbol Meaning Example
yyyy 4-digit year 2025
MM 2-digit month (01-12) 10
dd 2-digit day of month (01-31) 27
HH 2-digit hour (0-23) 14
mm 2-digit minute (00-59) 30
ss 2-digit second (00-59) 05
SSS 3-digit millisecond (000-999) 123
z Time zone name (e.g., "PST") PST
O Offset (e.g., "+0800") +0800

Scenario B: Your Date object represents a specific time zone

If you know your java.util.Date was created with a specific time zone (e.g., from a database that stores time zone info), it's better to convert it to a ZonedDateTime.

import java.util.Date;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
public class DateToStringWithTimeZone {
    public static void main(String[] args) {
        // Let's pretend this date was created for "America/New_York"
        ZoneId newYorkZone = ZoneId.of("America/New_York");
        ZonedDateTime zonedDateTime = ZonedDateTime.now(newYorkZone);
        // Convert ZonedDateTime to java.util.Date for demonstration
        Date dateFromZoned = Date.from(zonedDateTime.toInstant());
        // Now, convert it back
        ZonedDateTime originalZoned = dateFromZoned.toInstant().atZone(newYorkZone);
        // Format it
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss z");
        String formattedString = formatter.format(originalZoned);
        System.out.println("Original java.util.Date: " + dateFromZoned);
        System.out.println("Recreated ZonedDateTime: " + originalZoned);
        System.out.println("Formatted String:         " + formattedString);
    }
}

The Legacy Way: java.text.SimpleDateFormat (Pre-Java 8)

This was the standard for many years, but it has major drawbacks:

  • Not Thread-Safe: You cannot share a single instance of SimpleDateFormat across multiple threads. You must create a new instance for every use, which is inefficient.
  • Error-Prone: The API is clunky and can lead to ParseException if the input string doesn't match the expected format.

Use this only if you are working with legacy code that cannot be updated.

java date to string-图3
(图片来源网络,侵删)
import java.util.Date;
import java.text.SimpleDateFormat;
public class DateToStringLegacy {
    public static void main(String[] args) {
        // 1. Get a java.util.Date object
        Date date = new Date();
        // 2. Create a new SimpleDateFormat instance for every call!
        // Pattern: yyyy-MM-dd HH:mm:ss
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        // 3. Format the date to a string
        String formattedDate = formatter.format(date);
        System.out.println("Original java.util.Date: " + date);
        System.out.println("Formatted String:       " + formattedDate);
        // --- Example with a different time zone ---
        formatter.setTimeZone(java.util.TimeZone.getTimeZone("UTC"));
        String formattedUtc = formatter.format(date);
        System.out.println("Formatted UTC String:   " + formattedUtc);
    }
}

Summary: Which one should I use?

Feature java.time (Modern) SimpleDateFormat (Legacy)
Recommendation Highly Recommended Avoid in new code
Thread Safety Thread-Safe (immutable objects) Not Thread-Safe (must be re-created per use)
Ease of Use Excellent, intuitive API Clunky, error-prone API
Key Classes Instant, ZonedDateTime, DateTimeFormatter SimpleDateFormat
Time Zone Handling Built-in, robust Requires TimeZone object manipulation
Java Version Java 8+ All versions

Final Recommendation:

Always prefer the java.time package. It's the modern standard, safer, and more powerful. Only use SimpleDateFormat if you are forced to maintain old code that relies on it.

分享:
扫描分享到社交APP
上一篇
下一篇