Monday 2 July 2018

How to use getTime() and setTime(long time) methods of Java.util.Date


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

DateDemo1.java

import java.util.Date;

public class DateDemo1
{
    public static void main(String[] args)
    {
        Date date = new Date();

        /*
         * Returns:
         *
         * the number of milliseconds since January 1, 1970, 00:00:00
         * GMT represented by this date.
         */

        long milliSec = date.getTime();
        System.out.println(milliSec);

    }

}

Output

1526191183774

DateDemo2.java

import java.util.Date;

public class DateDemo2
{
    public static void main(String[] args)
    {
        Date date = new Date();
        System.out.println("Date before setting =  " + date);

        /*
         * Sets this Date object to represent a point in time that is
         * time milliseconds after January 1, 1970 00:00:00 GMT.
         *
         * Parameters:
         *
         * time - the number of milliseconds.
         */

        date.setTime(200000);

        System.out.println("Date after setting  =  " + date);

    }

}

Output

Date before setting =  Sun May 13 11:29:50 IST 2018
Date after setting  =  Thu Jan 01 05:33:20 IST 1970

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

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

Bitbucket Link:
https://bitbucket.org/ramram43210/java/src/bb275a728500f81507bdd46f6424332d29623c06/BasicJava_2018/DateDemo_util_getTime_setTime/?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