Core Java Tutorial

Introduction of Core Java

How To Install JDk and Set of Path

Syntax of java Program

Difference between Java and C/C++

Advantage and Disadvantage of Java

What is Java

Why Java is not Pure Object Oriented Language

Java has Following Features/Characteristics

Limitation of Java Language and Java Internet

Common Misconception about Java

Simple Program of Java

Integrated Development Environment in java

Compile and Run Java Program

Applet and Comments in Java

Tokens in Java

Keywords in Java

Identifier and Variables in Java

Literals/Constants

Data Type in Java

Assignments and Initialization in Java

Operators in Java

Rule of Precedence in Java

Operator on Integer and Separators in Java Programming

Java Control Flow of Statements

If and If-else Selection Statement

Nested If-else and If-else-If Selection Statement

switch case and conditional operator Selection Statement

for and while Loop

do..while and for each Loop

break and labeled break statement

continue and labeled continue statement

return Statement and exit() Method

Escape Sequence for Special Characters and Unicode Code

Constants and Block or Scope

Statement in Java

Conversions between Numeric Types in Java

Import Statement in Java

User Input in Java using Scanner Class

User Input in Java using Console Class

Array in Java

One Dimensional Array

Two Dimensional Array

Two Dimensional Array Program

Command Line Argument in Java

String args Types in Java

Uneven/Jagged array in java

Math Class Function and Constant

Math Class all Function used in a program

Enumerated Types in Java

Object Oriented Programming v/s Procedural Programming

Object Oriented Programming Concepts in Java

Introduction to Class,Object and Method in Java

Class Declaration in Java

Class & Objects in java

Encapsulation in Java

Modifiers/Visibility for a Class or Interrface or member of a Class

Polymorphism in Java

Runtime polymorphism (dynamic binding or method overriding)

how to calculate the difference between the two dates using collections in java

how to calculate the difference between the two dates using collections in java

Previous Home Next

 

In this example we are going to calculate the difference of the two given dates.

In this example we have created a class named CollectionExample in which we have created the Date objects and then displayed the difference between their dates.
 In this example we have used the getTime() method on the Date object for getting the time. The general syntax of the getTime() is as follows,
Date getTime()
The above method returns a Date object representing the current calendar time values.

In this example we have created a Date object named date1 and initialized it with using the constructor of the GregorianCalnedar(int year, int month, int day, int hour, int minute).
We have also created another Date object named todaysdate which is initialized with the default constructor and then invoked the getTime() method on it. Then we have displayed the difference between the two days created.

 
package r4r.co.in;
import java.util.*;


public class CollectionExample
{

/**
* @param args
*/

public static void main(String args[])
{


Date date1= new GregorianCalendar(2009, 6, 4, 11, 47).getTime(); // this statement sets a date-time from which difference is to be calculate
System.out.println("Time set is given by: "+date1); // displaying the set date
Date todaysdate=new Date(); // creating the Date object
System.out.println("Todays time is given by: "+todaysdate); // displaying the today's date
long DateDifference=todaysdate.getTime()-date1.getTime(); // calculating the difference between the dates in long milliseconds
System.out.println("The difference between the two dates in milliseconds is given by: "+DateDifference); // displaying the date difference in milliseconds
System.out.println("The difference between the two dates is:"+(DateDifference/(1000*24*60*60))+"days"); // displaying the date difference in terms of days



}

}

The output of the above example is given below:

Time set is given by: Sat Jul 04 11:47:00 PDT 2009
Todays time is given by: Tue Nov 16 11:32:31 PST 2010
The difference between the two dates in milliseconds is given by: 43202731234
The difference between the two dates is:500days

Previous Home Next