Here are the Java examples that shows how you can get the current timestamp in Java
-To get Unix (Epoch) timestamp
public class App
{
public static void main( String[] args )
{
System.out.println(System.currentTimeMillis());
}
}
The output you will see is the current unix timestamp.
1430628059502
- To get Timestamp using the Date class in Sql timestamp format.
import java.sql.Timestamp;
import java.util.Date;
public class App
{
public static void main( String[] args )
{
Date date= new java.util.Date();
System.out.println(new Timestamp(date.getTime()));
}
}
Output will be like
2015-05-03 10:10:59.514
- To get the sql Timestamp using Calendar class
import java.sql.Timestamp;
import java.util.Calendar;
import java.util.Date;
public class App
{
public static void main( String[] args )
{
Calendar calendar = Calendar.getInstance();
Date now = calendar.getTime();
System.out.println(new Timestamp(now.getTime()));
}
}
Output will be like
2015-05-03 10:10:59.514
Comments