current timestamp in java code example
Example 1: java timestamp
Timestamp timestamp = new Timestamp(System.currentTimeMillis());
Copy
Example 2: java timestamp
package com.mkyong.date;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Date;
public class TimeStampExample {
private static final SimpleDateFormat sdf = new SimpleDateFormat("yyyy.MM.dd.HH.mm.ss");
public static void main(String[] args) {
Timestamp timestamp = new Timestamp(System.currentTimeMillis());
System.out.println(timestamp);
Date date = new Date();
System.out.println(new Timestamp(date.getTime()));
System.out.println(timestamp.getTime());
System.out.println(sdf.format(timestamp));
}
}
Copy
Example 3: get current date time timestamp java
Timestamp timestamp = new Timestamp(System.currentTimeMillis());
Date date = new Date();
Timestamp timestamp2 = new Timestamp(date.getTime());
Timestamp ts = Timestamp.from(Instant.now())
Timestamp ts = Timestamp.from(ZonedDateTime.now().toInstant()));
Instant instant = ts.toInstant();