Home | Ajax | BioInformatics | Dojo | EAI | EJB | Hibernate | J2ME | Java | Java Glossary | Java Servlets | JavaScript | Jboss | JDBC | JDO | Jmeter | JSF | JSP | JUnit | Maven | MySQL | Spring Framework | SQL | Struts | Technology | WAP | Web Services | XML

Search:
   Programming Tutorials: Ajax | Articles | JSP | Bioinformatics | Database | Free Books | Hibernate | J2EE | J2ME | Java | JavaScript | JDBC | JMS | Linux | MS Technology | PHP | RMI | Web-Services | Servlets | Struts | UML Facing Programming Problem? Ask Questions?, Browse Latest Questions, Question-Answer Guidelines
Java
  JDO Tutorials
  EAI Articles
  Struts Tutorials
  Java Tutorials
  Java Certification
  Java Applet
Questions
Comments
Date Difference
This example learns how to make the difference between two dates and how to convert milliseconds into seconds, seconds into minutes, minutes into hours, hours into days.
 
 

Date Difference

                         

This example learns how to make the difference between two dates and how to convert milliseconds into seconds, seconds into minutes, minutes into hours, hours into days.

Here we are using Calendar, an abstract base class that extends Object class and makes a difference between a Date object and a set of integer fields. Calendar class provides a getInstance()  method for returning a Calendar object whose time fields have been initialized with the current date and time.

The methods used:
setTimeInMillis(long millis): This method is used to set current time in calendar object.

getInstance(): This method is used to get a calendar using the default time zone, locale and current time.

The code of the program is given below:

import java.util.Calendar;
 
public class DateDifferent{    
    public static void main(String[] args){
    Calendar calendar1 = Calendar.getInstance();
    Calendar calendar2 = Calendar.getInstance();
    calendar1.set(20070110);
    calendar2.set(20070701);
    long milliseconds1 = calendar1.getTimeInMillis();
    long milliseconds2 = calendar2.getTimeInMillis();
    long diff = milliseconds2 - milliseconds1;
    long diffSeconds = diff / 1000;
    long diffMinutes = diff / (60 1000);
    long diffHours = diff / (60 60 1000);
    long diffDays = diff / (24 60 60 1000);
    System.out.println("\nThe Date Different Example");
    System.out.println("Time in milliseconds: " + diff
 + 
" milliseconds.");
    System.out.println("Time in seconds: " + diffSeconds
 + 
" seconds.");
    System.out.println("Time in minutes: " + diffMinutes 
" minutes.");
    System.out.println("Time in hours: " + diffHours 
" hours.");
    System.out.println("Time in days: " + diffDays 
" days.");
  }
}

The output of the program is given below:

C:\rajesh\kodejava>javac DateDifferent.java
C:\rajesh\kodejava>java DateDifferent
The Date Different Example
Time in milliseconds: 14860800000 milliseconds.
Time in seconds: 14860800 seconds.
Time in minutes: 247680 minutes.
Time in hours: 4128 hours.
Time in days: 172 days.

Download this example.

                         

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 

Current Comments

1 comments so far (
post your own) View All Comments Latest 10 Comments:

the program is good . but can u help in my problem .
my problem is how can,we print the output of java program into printer or in proper view.


please reply on my e-mail address

looking forward............

Posted by damanjeet on Saturday, 12.15.07 @ 19:05pm | #42518

Java String toLowerCase Example
Java String toCharArray Example
Java String substring Example
Java String indexOf Example
Java String startsWith Example
Java String hashCode Example
Java String matches Example
Java String length Example
Java String lastIndexOf Example
Java String isEmpty Example
Java String equalsIgnoreCase Example
Java String equals Example
Java String endsWith Example
Java String copyValueOf Example
Java String contentEquals Example
  EAI Articles
  Java Certification
Tell A Friend
Your Friend Name
Search Tutorials

 

 
 
Browse all Java Tutorials
Java JSP Struts Servlets Hibernate XML
Ajax JDBC EJB MySQL JavaScript JSF
Maven2 Tutorial JEE5 Tutorial Java Threading Tutorial Photoshop Tutorials Linux Technology
Technology Revolutions Eclipse Spring Tutorial Bioinformatics Tutorials Tools SQL
 

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 | 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.