Its quite easy to get the current timestamp in java. In this tutorial we will see how to get the timestamp using Date
and Timestamp
class.
Here are the steps that we have followed in the below example:
1) Created the object of Date class.
2) Got the current time in milliseconds by calling getTime()
method of Date.
3) Created the object of Timtestamp
class and passed the milliseconds that we got in step 2, to the constructor of this class during object creation. It constructs the timestamp using the provided milliseconds value.
import java.sql.Timestamp; import java.util.Date; public class TimeStampDemo { public static void main( String[] args ) { //Date object Date date= new Date(); //getTime() returns current time in milliseconds long time = date.getTime(); //Passed the milliseconds to constructor of Timestamp class Timestamp ts = new Timestamp(time); System.out.println("Current Time Stamp: "+ts); } }
Output:
Current Time Stamp: 2014-01-08 18:31:19.37
References:
Leave a Reply