Date Example

In this section we are discussing about the specific date using the Date class object, retrieving milliseconds from the Date object, elapsed time.

Date Example

In this section we are discussing about the specific date using the Date class object, retrieving milliseconds from the Date object, elapsed time.

Date Example

Date Example

     

In this section we are discussing about the specific date using the Date class object, retrieving milliseconds from the Date object, elapsed time, date contained in the Date object before and after it has change and displaying both the values on the console.

Description of program:  

In this example first we are creating the DateExample() method and inside this we are creating Date object that contains the current date and time by using the date constructor and then print it on the console. Now we are creating the two Date objects containing two different dates and print them on the console. After that we are retrieving the elapsed time of the two objects in milliseconds by using the getTime() method of the Date class and also printing it on the console. Now we are creating an object chngdate containing the current date and after that changing its value by using the setTime() method of Date class and also printing both the values on the console.

 

import java.util.Date;
import java.text.ParseException;
import java.text.SimpleDateFormat;
public class DateExample {

    private static void DateExample() {

        Date date = new Date();
        System.out.println("Current Date and Time is : " + date);
        System.out.println();

        System.out.println("Date object showing specific date and time");
        Date particulardate1 = new Date(24L*60L*60L*1000L);
        Date particulardate2 = new Date(0L);
        System.out.println();
        System.out.println("First Particular date : " + particulardate1);
        System.out.println("Second Particular date: " + particulardate2);
        System.out.println();       

        System.out.println("Demo of getTime() method returning milliseconds");
        System.out.println();
        Date strtime = new Date();
        System.out.println("Start Time: " + strtime);
        Date endtime = new Date();
        System.out.println("End Time is: " + endtime);
        long elapsed_time = endtime.getTime() - strtime.getTime();
        System.out.println("Elapsed Time is:" + elapsed_time + " milliseconds");
        System.out.println();

        System.out.println("Changed date object using setTime() method");
        System.out.println();
        Date chngdate = new Date();
        System.out.println("Date before change is: " + chngdate);
        chngdate.setTime(24L*60L*60L*1000L);
        System.out.println("Now the Changed date is: " + chngdate);
        System.out.println();
  }

    public static void main(String[] args) {
        System.out.println();
        DateExample();
    }

}

Here is the output:

C:\Examples>java DateExample

Current Date and Time is : Mon Dec 10 18:39:27 GMT+05:30 2007

Date object showing specific date and time

First Particular date : Fri Jan 02 05:30:00 GMT+05:30 1970
Second Particular date: Thu Jan 01 05:30:00 GMT+05:30 1970

Demo of getTime() method returning milliseconds

Start Time: Mon Dec 10 18:39:28 GMT+05:30 2007
End Time is: Mon Dec 10 18:39:28 GMT+05:30 2007
Elapsed Time is:0 milliseconds

Changed date object using setTime() method

Date before change is: Mon Dec 10 18:39:28 GMT+05:30 2007
Now the Changed date is: Fri Jan 02 05:30:00 GMT+05:30 1970

Download of this program.