java tutorial - How to get current timestamp using Java - java programming - learn java - java basics - java for beginners



 current timestamp in java

Learn java - java tutorial - current time stamp in java - java examples - java programs

Current timestamp in Java:

  • It is quite easy to get the current timestamp in java. By using Date and Timestamp class.
  • Here are the steps that we have followed in the below example:
  • Created the object of Date class.
  • Got the current time in milliseconds by calling getTime() method of Date.
  • Now created object of Timtestamp class and then passed the given milliseconds which we get in step two, to the constructor of this class through the creation of object. It will build timestamp using the already provided milliseconds value.

Syntax

String timeStamp = new SimpleDateFormat("yyyy.MM.dd.HH.mm.ss").format(new Timestamp());
  • A thin wrapper around “java.util.Date” that allows the JDBC API to identify this as an SQL TIMESTAMP value. It includes the capacity to hold SQL TIMESTAMP for fractional seconds value, which permits this requirement of fractional seconds into precision of nanoseconds.
  • A Timestamp can also offer formatting and then parsing operations to keep up the JDBC (Java Database Connectivity) escape syntax for timestamp values.

sample code:

import java.sql.Timestamp;
import java.util.Date;
 
public class kaashiv_infotech
{
    public static void main ( String[] args )
    {
        Date datetime= new Date();
        System.out.println(new Timestamp(datetime.getTime()));
    }
}

Output:

2018-02-06 16:21:26.36

Related Searches to How to get current timestamp using java