杰瑞科技汇

java timestamp mysql

Of course! This is a very common and important topic in Java development, especially when interacting with a database like MySQL. Here’s a comprehensive guide covering the key concepts, best practices, and code examples.

java timestamp mysql-图1
(图片来源网络,侵删)

The Core Concepts: java.util.Date vs. java.time

Before diving into MySQL, it's crucial to understand the two main ways Java handles dates and times.

The Legacy: java.util.Date and java.sql.Timestamp

  • java.util.Date: The original Java date class. It represents a specific instant in time, with millisecond precision. It's often misunderstood because its toString() method includes date and time, but it's fundamentally a timestamp.
  • java.sql.Timestamp: A subclass of java.util.Date. It was created specifically for JDBC to interact with SQL TIMESTAMP types. It adds nanosecond precision.

Key Problem: The java.util.Date and java.sql.Timestamp classes are mutable, have clunky APIs, and their design is based on a confusing mix of date and time concepts. They are now considered legacy.

The Modern: java.time (Java 8+)

Since Java 8, the java.time package provides a much better, immutable, and thread-safe API for dates and times. This is the recommended approach for all new development.

  • Instant: Represents a specific moment on the timeline in UTC. It's the closest equivalent to a database timestamp. It has nanosecond precision.
  • LocalDateTime: Represents a date and time, but without a time zone (e.g., "2025-10-27T10:15:30"). This is useful for events that don't have a global context, like a scheduled meeting.
  • ZonedDateTime: Represents a date and time with a specific time zone (e.g., "2025-10-27T10:15:30+02:00[Europe/Paris]").

MySQL Timestamp Types

MySQL has several date/time types. The most common one for storing an exact point in time is TIMESTAMP.

java timestamp mysql-图2
(图片来源网络,侵删)
MySQL Type Description Range Precision
TIMESTAMP Stores a point in time. Converted from the server's time zone to UTC for storage and back to the session's time zone for retrieval. 1970-01-01 00:00:01 UTC to 2038-01-19 03:14:07 UTC Seconds
DATETIME Stores a date and time as a "calendar" value. It is not time-zone aware. 1000-01-01 00:00:00 to 9999-12-31 23:59:59 Seconds
DATETIME(6) Same as DATETIME but with microsecond precision. 1000-01-01 00:00:00.000000 to 9999-12-31 23:59:59.999999 Microseconds

Recommendation:

  • Use DATETIME(6) for modern applications. It avoids the TIMESTAMP limitations (range, time zone conversion quirks) and provides high precision.
  • Use TIMESTAMP if you specifically need automatic time zone conversion or are working with legacy systems.

Best Practices: The Modern java.time Approach

This is the recommended, cleanest, and safest way to handle timestamps.

Step 1: Database Table Setup

Let's create a table with a DATETIME(6) column.

CREATE TABLE events (
    id INT AUTO_INCREMENT PRIMARY KEY,
    event_name VARCHAR(255) NOT NULL,
    event_time DATETIME(6) NOT NULL
);

Step 2: Java Code (JDBC Example)

We will use java.time.Instant to represent the timestamp in Java. JDBC 4.2 and later have built-in support for java.time types, so no extra libraries are needed.

java timestamp mysql-图3
(图片来源网络,侵删)
import java.sql.*;
import java.time.Instant;
public class MysqlTimestampExample {
    private static final String DB_URL = "jdbc:mysql://localhost:3306/your_database";
    private static final String USER = "your_username";
    private static final String PASS = "your_password";
    public static void main(String[] args) {
        // 1. Get the current time as an Instant (UTC)
        Instant now = Instant.now();
        System.out.println("Java Instant (UTC): " + now);
        // --- INSERT ---
        try (Connection conn = DriverManager.getConnection(DB_URL, USER, PASS)) {
            String sql = "INSERT INTO events (event_name, event_time) VALUES (?, ?)";
            try (PreparedStatement pstmt = conn.prepareStatement(sql)) {
                pstmt.setString(1, "System Startup");
                // setObject() directly maps Instant to DATETIME(6)
                pstmt.setObject(2, now);
                pstmt.executeUpdate();
                System.out.println("Record inserted successfully!");
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        // --- SELECT ---
        try (Connection conn = DriverManager.getConnection(DB_URL, USER, PASS)) {
            String sql = "SELECT id, event_name, event_time FROM events WHERE event_name = ?";
            try (PreparedStatement pstmt = conn.prepareStatement(sql)) {
                pstmt.setString(1, "System Startup");
                try (ResultSet rs = pstmt.executeQuery()) {
                    if (rs.next()) {
                        // getObject() retrieves the DATETIME(6) column as an Instant
                        Instant retrievedTime = rs.getObject("event_time", Instant.class);
                        String eventName = rs.getString("event_name");
                        System.out.println("\n--- Retrieved Data ---");
                        System.out.println("Event Name: " + eventName);
                        System.out.println("Retrieved Instant (UTC): " + retrievedTime);
                    }
                }
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}

Key Points in the Modern Approach:

  • Instant.now(): Gets the current time in UTC. This is what you should store.
  • pstmt.setObject(2, now): This is the magic. The setObject() method with a java.time type tells the JDBC driver to handle the conversion to the correct SQL format.
  • rs.getObject("event_time", Instant.class): When retrieving, you use getObject() and specify the target Java class (Instant.class). The driver converts the DATETIME(6) value back into an Instant.
  • No Manual Formatting: You completely avoid the SimpleDateFormat and java.text classes, which are a common source of bugs.

Legacy Approach: Using java.sql.Timestamp

If you're working on an older codebase that uses java.util.Date and java.sql.Timestamp, here's how it works.

Step 1: Java Code (JDBC with Timestamp)

import java.sql.*;
import java.util.Date;
public class MysqlTimestampLegacyExample {
    private static final String DB_URL = "jdbc:mysql://localhost:3306/your_database";
    private static final String USER = "your_username";
    private static final String PASS = "your_password";
    public static void main(String[] args) {
        // 1. Get current time as a java.util.Date
        Date now = new Date();
        System.out.println("Java Date: " + now);
        // 2. Convert to java.sql.Timestamp for JDBC
        Timestamp timestamp = new Timestamp(now.getTime());
        System.out.println("Java Timestamp: " + timestamp);
        // --- INSERT ---
        try (Connection conn = DriverManager.getConnection(DB_URL, USER, PASS)) {
            String sql = "INSERT INTO events (event_name, event_time) VALUES (?, ?)";
            try (PreparedStatement pstmt = conn.prepareStatement(sql)) {
                pstmt.setString(1, "Legacy Event");
                // Use setTimestamp() for the legacy type
                pstmt.setTimestamp(2, timestamp);
                pstmt.executeUpdate();
                System.out.println("Legacy record inserted successfully!");
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        // --- SELECT ---
        try (Connection conn = DriverManager.getConnection(DB_URL, USER, PASS)) {
            String sql = "SELECT id, event_name, event_time FROM events WHERE event_name = ?";
            try (PreparedStatement pstmt = conn.prepareStatement(sql)) {
                pstmt.setString(1, "Legacy Event");
                try (ResultSet rs = pstmt.executeQuery()) {
                    if (rs.next()) {
                        // Use getTimestamp() to retrieve the value
                        Timestamp retrievedTimestamp = rs.getTimestamp("event_time");
                        // You can convert it back to a java.util.Date if needed
                        Date retrievedDate = retrievedTimestamp;
                        System.out.println("\n--- Retrieved Legacy Data ---");
                        System.out.println("Retrieved Timestamp: " + retrievedTimestamp);
                        System.out.println("Retrieved Date: " + retrievedDate);
                    }
                }
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}

Key Points in the Legacy Approach:

  • new Timestamp(date.getTime()): You must explicitly convert a java.util.Date to a java.sql.Timestamp before storing it.
  • pstmt.setTimestamp(): This is the correct method to use for the `
分享:
扫描分享到社交APP
上一篇
下一篇