Core Java| JSP| Servlets| XML| EJB| JEE5| Web Services| J2ME| Glossary| Questions?

 

 

 

 

 

 

 

 

 

 

 

 

 

Search Tutorials

Latest Questions
Comments
 
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 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.

 

                         

» View all related tutorials
Related Tags: c com file ide class files stl date time variables static directory io make variable dates sed get ria this

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

Audio Version
Reload Image
 

Note: Emails will not be visible or used in any way, and are not required. Please keep comments relevant. Any content deemed inappropriate or offensive may be edited and/or deleted.

No HTML code is allowed. Line breaks will be converted automatically. URLs will be auto-linked. Please use BBCode to format your text.

Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Training Courses
Tell A Friend
Your Friend Name
Software Solutions
Least Viewed
Most Rated
Recently Viewed
Search Tutorials

 

 
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Indian Software Development Company | iPhone Development Company in India | Flex Development Company in India | Java Training Delhi | Java Training at Noida |

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2008. All rights reserved.