TimeStamp In Java Example
Chapter:
Date and Time
Last Updated:
05-09-2016 16:43:19 UTC
Program:
/* ............... START ............... */
import java.sql.Timestamp;
import java.util.Calendar;
import java.util.Date;
public class JavaTimeStampExample {
public static void main(String[] args) {
Timestamp timestamp = new Timestamp(new Date().getTime());
System.out.println(timestamp);
Calendar cal = Calendar.getInstance();
cal.setTimeInMillis(timestamp.getTime());
// add 30 seconds
cal.add(Calendar.SECOND, 30);
timestamp = new Timestamp(cal.getTime().getTime());
System.out.println(timestamp);
// add 5 hours
cal.setTimeInMillis(timestamp.getTime());
cal.add(Calendar.HOUR, 5);
timestamp = new Timestamp(cal.getTime().getTime());
System.out.println(timestamp);
}
}
/* ............... END ............... */
Output
2016-04-26 20:01:11.89
2016-04-26 20:01:41.89
2016-04-27 01:01:41.89
Notes:
-
getTime() method of Date is used to get current time in milliseconds.
Tags
TimeStamp In Java Example