Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java Timestamp - How can I create a Timestamp with the date 23/09/2007?

How can I create a Timestamp with the date 23/09/2007?

like image 896
pigouina Avatar asked Jun 10 '09 11:06

pigouina


People also ask

How do you create a timestamp in Java?

Java SQL Timestamp setTime() function with examples The function is used to set the time of the Timestamp object. The function takes time in milliseconds which represents the time in milliseconds after 1st January, 1970. Syntax: ts1.

How do I display a timestamp from a Date?

You can use Instant#ofEpochSecond to get an Instant out of the given timestamp and then use LocalDate. ofInstant to get a LocalDate out of the obtained Instant . Learn more about the modern Date-Time API from Trail: Date Time.


2 Answers

By Timestamp, I presume you mean java.sql.Timestamp. You will notice that this class has a constructor that accepts a long argument. You can parse this using the DateFormat class:

DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy"); Date date = dateFormat.parse("23/09/2007"); long time = date.getTime(); new Timestamp(time); 
like image 71
Adam Paynter Avatar answered Oct 04 '22 02:10

Adam Paynter


What about this?

java.sql.Timestamp timestamp = java.sql.Timestamp.valueOf("2007-09-23 10:10:10.0"); 
like image 37
pigouina Avatar answered Oct 04 '22 01:10

pigouina