Tuesday, 18 December 2018

How to use setTime(long time) and setNanos(int n) methods of java.sql.Timestamp Class


Click here to watch on Youtube: 
https://www.youtube.com/watch?v=lvOBCM9sIZk&list=UUhwKlOVR041tngjerWxVccw

TimestampDemo.java

import java.sql.Timestamp;

public class TimestampDemo
{

    public static void main(String[] args)
    {

        Timestamp timestamp = Timestamp.valueOf("2018-01-23 19:09:58.743");
        System.out.println(timestamp);

        long time = System.currentTimeMillis();

        /*
         * Parameters:
         *
         * time - the number of milliseconds.
         */

        timestamp.setTime(time);
        System.out.println(timestamp);

        /*
         * Parameters:
         *
         * n - the new fractional seconds component
         */

        timestamp.setNanos(3);
        System.out.println(timestamp);
    }

}

Output

2018-01-23 19:09:58.743
2018-11-25 08:33:21.444
2018-11-25 08:33:21.000000003

Click the below link to download the code:
https://sites.google.com/site/ramj2eev2/java_basics/TimestampDemo_settime_nanos.zip?attredirects=0&d=1

Github Link:
https://github.com/ramram43210/Java/tree/master/BasicJava_2018/TimestampDemo_settime_nanos

Bitbucket Link:
https://bitbucket.org/ramram43210/java/src/5bada39e8532ab4b75aaa28b80cdcee21bf19ab7/BasicJava_2018/TimestampDemo_settime_nanos/?at=master

See also:
  • All JavaEE Videos Playlist
  • All JavaEE Videos
  • All JAVA EE Links
  • Spring Tutorial
  • Servlets Tutorial
  • All Design Patterns Links
  • JDBC Tutorial
  • Java Collection Framework Tutorial
  • JAVA Tutorial
  • Kids Tutorial
  • Cooking Tutorial
  • No comments:

    Post a Comment