java 8 sql timestamp examples
Several examples showing how to use:
import java.sql.Timestamp;
in java 8 - with ZonedDateTime, using with current time and examples on compability with older java versions.
In this post:
- Java 8 SQL timestamp and ZonedDateTime
- Return now or the beginning of counting as timestamp
- Convert timestamp from today's date
- Converting timestamp to instant
- Converting SimpleDateFormat to timestamp
- The complete example
Java 8 SQL timestamp and ZonedDateTime
Converting date from the new java 8 ZonedDateTime to sql timestamp:
ZonedDateTime -> timestamp
2017-11-23T17:00:00-0700 -> 2017-11-24 02:00:00.0
String start_time = "2017-11-23T17:00:00-0700";
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ssZ");
ZonedDateTime formatDateTime = ZonedDateTime.parse(start_time, formatter);
Instant instant2 = formatDateTime.toInstant();
System.out.println(new Timestamp(instant2.toEpochMilli()));
result:
2017-11-24 02:00:00.0
Note that time zone is considered and it's giving a difference from 2 hours.
Several examples on the time addition - my zone has two hours difference with the GTM so in order to get exactly the same time I need to use:
"2017-11-23T17:00:00+0200" -> 2017-11-24 00:00:00.0
"2017-11-23T17:00:00-1000" -> 2017-11-24 05:00:00.0
"2017-11-23T17:00:00-0000" -> 2017-11-23 19:00:00.0
Return now or the beginning of counting as timestamp
In order to get now or the begining of counting (1970-01-01) we can use:
Timestamp timestamp = new Timestamp(System.currentTimeMillis());
System.out.println(timestamp);
Timestamp timestampDate = new Timestamp(0);
System.out.println(timestampDate);
result:
2018-03-01 11:05:53.78
1970-01-01 02:00:00.0
Convert timestamp from today's date
The simplest example is when you want to get today as date and convert it to timestamp:
new Date() -> 2018-03-01 11:05:53.78
Date date = new Date();
System.out.println(new Timestamp(date.getTime()));
result:
2018-03-01 11:05:53.78
Converting timestamp to instant
Converting timestamp to instant and the reverse operation:
2018-03-01T09:11:12.603Z <-> 1519895472603
can be done by methods: toInstant() and from(instant)
Timestamp timestamp = new Timestamp(System.currentTimeMillis());
Instant instant = timestamp.toInstant();
System.out.println(instant);
Timestamp timestampInstant = Timestamp.from(instant);
System.out.println(timestampInstant.getTime());
result:
2018-03-01T09:11:12.603Z
1519895472603
Converting SimpleDateFormat to timestamp
If you are using older versions of java you can use this code which is compabitle with java versions before java 8:
SimpleDateFormat -> sql timestamp
"2020/03/01 22:05:15" -> 2020-03-01 22:05:15.0
String myDate = "2020/03/01 22:05:15";
SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
Date customDate = sdf.parse(myDate);
long millis = customDate.getTime();
Date date1 = new Date(millis);
System.out.println(new Timestamp(date1.getTime()));
result:
2020-03-01 22:05:15.0
The complete example
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.sql.Timestamp;
import java.time.Instant;
public class TimestampTest {
public static void main(String[] args) throws ParseException {
//
//simple date format
//
String myDate = "2020/03/01 22:05:15";
SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
Date customDate = sdf.parse(myDate);
long millis = customDate.getTime();
Date date1 = new Date(millis);
System.out.println(new Timestamp(date1.getTime()));
//
//simple date format
//
String start_time = "2017-11-23T17:00:00-0700";
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ssZ");
ZonedDateTime formatDateTimeZ = ZonedDateTime.parse(start_time, formatter);
Instant instant2 = formatDateTimeZ.toInstant();
System.out.println(instant2);
System.out.println(new Timestamp(instant2.toEpochMilli()));
//
// time stamp from today
//
Date date = new Date();
System.out.println(new Timestamp(date.getTime()));
//
// time stamp beginning
//
Timestamp timestampDate = new Timestamp(0);
System.out.println(timestampDate);
Timestamp timestamp = new Timestamp(System.currentTimeMillis());
System.out.println(timestamp);
//
// time stamp to instant
//
System.out.println(timestamp.getTime());
Instant instant = timestamp.toInstant();
System.out.println(instant);
System.out.println(instant.toEpochMilli());
Timestamp timestampInstant = Timestamp.from(instant);
System.out.println(timestampInstant.getTime());
}
}