HOWTO · Java

How to Convert String to Timestamp in Java

It shows four methods to convert a string to a timestamp in Java

On this page

A java.sql.Timestamp represents a date and time with fractional-second precision and is often used with JDBC. Choose the parser from the shape of your input: use Timestamp.valueOf() for a SQL-style date-time, DateTimeFormatter for a custom date-time, or LocalDate when the input contains only a date. These conversions do not discover a time zone that the input does not contain.

Use Timestamp.valueOf() for a Standard Date-Time String

For a string such as 2024-06-15 14:30:00, Timestamp.valueOf(String) parses the SQL timestamp form directly. The API accepts a JDBC timestamp escape-format value, including optional fractional seconds; it is not limited to the exact seconds-only shape shown here. Use a different parser when your input uses another ordering or separators.

Run this example:

import java.sql.Timestamp;

public class StringToTimestamp {
  public static void main(String[] args) {
    String input = "2024-06-15 14:30:00";
    Timestamp timestamp = Timestamp.valueOf(input);
    System.out.println(timestamp);
  }
}
2024-06-15 14:30:00.0

The printed .0 is the fractional-second portion. A malformed value is rejected by the API; validate external input and handle its parsing error at the boundary of your application.

Parse a Custom Date-Time String with DateTimeFormatter

When a value uses a pattern such as 15/06/2024 14:30:00, parse it with DateTimeFormatter into LocalDateTime, then pass that object to Timestamp.valueOf(LocalDateTime). The pattern must match the input: dd/MM/uuuu means day, month, and year, while HH:mm:ss uses a 24-hour clock. LocalDateTime deliberately has no offset or zone, so this route preserves the wall-clock fields without identifying a unique instant.

import java.sql.Timestamp;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

public class CustomTimestamp {
  public static void main(String[] args) {
    String input = "15/06/2024 14:30:00";
    DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd/MM/uuuu HH:mm:ss");
    LocalDateTime localDateTime = LocalDateTime.parse(input, formatter);
    Timestamp timestamp = Timestamp.valueOf(localDateTime);
  }
}

Convert a Parsed Date to Timestamp with SimpleDateFormat

For legacy code built around java.util.Date, SimpleDateFormat can parse a custom pattern. Parsing produces a Date; getTime() returns its epoch-millisecond value, which the Timestamp constructor accepts. SimpleDateFormat uses the JVM default time zone unless you set one, and it is lenient by default. Set both explicitly when predictable interpretation matters. The example fixes UTC and disables lenient parsing; the API is also mutable and not thread-safe, so do not share one instance concurrently.

import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimeZone;

public class LegacyTimestamp {
  public static void main(String[] args) throws ParseException {
    SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
    formatter.setTimeZone(TimeZone.getTimeZone("UTC"));
    formatter.setLenient(false);
    Date parsed = formatter.parse("15/06/2024 14:30:00");
    Timestamp timestamp = new Timestamp(parsed.getTime());
  }
}

Convert a Date-Only String to Timestamp with LocalDate.atStartOfDay()

A date-only value does not specify a time. Parse it as LocalDate, choose midnight as the intended time with atStartOfDay(), and convert the resulting LocalDateTime to Timestamp. This makes the assumption explicit; it does not preserve a time that was absent from the input.

import java.sql.Timestamp;
import java.time.LocalDate;
import java.time.LocalDateTime;

public class DateOnlyTimestamp {
  public static void main(String[] args) {
    LocalDate date = LocalDate.parse("2024-06-15");
    LocalDateTime startOfDay = date.atStartOfDay();
    Timestamp timestamp = Timestamp.valueOf(startOfDay);
  }
}

Choose Between LocalDateTime and Timestamp

Use LocalDateTime when the value is a local calendar date and clock time and your application should not assign a zone yet. Use Timestamp when an API such as JDBC requires java.sql.Timestamp; it can carry fractional seconds and represents a point on the timeline through its millisecond value. A conversion from LocalDateTime uses the JVM’s default time zone to derive that value, so identical local fields can correspond to different instants on machines with different defaults.

If the input identifies an offset or region, parse and retain that zone information with an offset-aware or zoned java.time type before converting to an instant or a database type. Neither Timestamp.valueOf() nor LocalDateTime can infer the sender’s intended zone. Database column types and JDBC drivers can also apply their own time-zone rules, so keep the application’s storage and retrieval assumptions consistent.