How to Convert String Date to Timestamp in Java

  1. Convert String Date to Timestamp With Timestamp Constructor in Java
  2. Convert Date String to Timestamp With LocalDate in Java
  3. Convert String Date to Timestamp With the atStartOfDay() Method in Java

This tutorial introduces how to convert string date to timestamp in Java.

There are several ways to convert string date to timestamp like using Timestamp constructor or Java 8 LocalDate class.

Convert String Date to Timestamp With Timestamp Constructor in Java

Here, we use the Timestamp constructor to get a Timestamp object. First, we use the SimpleDateFormat class to set the format of date and then get the date object using the parser() method, then we use the geteTime() method to add time with the date and get a timestamp as a result. See the example below.

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

public class SimpleTesting {

	public static void main(String[] args) {
		try {
		      DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
		      Date date = dateFormat.parse("12/10/1990");
		      Timestamp timeStampDate = new Timestamp(date.getTime());
		      System.out.println(timeStampDate);
		    } catch (ParseException e) {
		      System.out.println(e);
		    }
	}
}

Output:

 1990-10-12 00:00:00.0

Convert Date String to Timestamp With LocalDate in Java

In Java 8, it adds a new java.time package that contains several classes to deal with date and time. Here, we use the LocalDate class of that package. We use parse() method of LocalDate class to to get LocalDate object from the date string and then use atTime() method to get LocalDateTime object which returns a date with time. The now() is used to get the current time of the system.

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;

public class SimpleTesting {

	public static void main(String[] args) {
		      LocalDate date = LocalDate.parse("2025-11-25");
		      System.out.println(date);
		      LocalDateTime dateTime = date.atTime(LocalTime.now());
		      System.out.println(dateTime);
		      
	}
}

Output:

2025-11-25
2025-11-25T09:44:56.814795

Convert String Date to Timestamp With the atStartOfDay() Method in Java

The atStartOfDay() method in LocalDate class is also use to get LocalDateTime object. This method adds the time to the date at the start of the day and returns a date-time rather than a simple date. See the example below.

import java.time.LocalDate;
import java.time.LocalDateTime;

public class SimpleTesting {

	public static void main(String[] args) {
		String strDate = "2019-10-20";
		LocalDate date = LocalDate.parse(strDate);
	    LocalDateTime dateTime = date.atStartOfDay();
	    System.out.println(dateTime);
	}
}

Output:

2019-10-20T00:00

Related Article - Java Date

  • How to Convert Date to String in Java
  • Related Article - Java String

  • How to Convert a Char to a String in Java
  • How to Convert an Integer to a String in Java
  • comments powered by Disqus