This week's book giveaway is in the OO, Patterns, UML and Refactoring forum.
We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line!
See this thread for details.
The moose likes Java in General and the fly likes Get the Difference between 2 dates Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

JavaRanch » Java Forums » Java » Java in General
Bookmark "Get the Difference between 2 dates" Watch "Get the Difference between 2 dates" New topic

Get the Difference between 2 dates

Ikasari Widiyanto

Joined: May 31, 2004
Posts: 29

I would like to how how to calculate the difference between 2 dates by using jsp... thanks..
best regards,
Paul Sturrock

Joined: Apr 14, 2004
Posts: 10336

Rememebr that a Date object is actually a long representing the number of milliseconds since the "epoch" (1/1/1970 00:00). So:

will give you the millisecond difference between the two. Then it only takes a little arithmetic to get this in a more user-friendly format.

JavaRanch FAQ HowToAskQuestionsOnJavaRanch
Bear Bibeault
Author and ninkuma

Joined: Jan 10, 2002
Posts: 63208

Moving to the Java in General (intermediate) forum.

[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Chengwei Lee
Ranch Hand

Joined: Apr 02, 2004
Posts: 884
When you say difference, are you trying to find out the number of seconds/minutes/hours/days/months/years between 2 dates?

By taking their differences & getting the results in milliseconds, you could then divide by the right factor to get that difference.

But, if I'm not mistaken, the Calendar class can solve this too. Check it out.


SCJP 1.4 * SCWCD 1.4 * SCBCD 1.3 * SCJA 1.0 * TOGAF 8
Ikasari Widiyanto

Joined: May 31, 2004
Posts: 29
hehe.. thanks.. but I got 1 problem.. the dates is in string format... how to change this to date format?
Jeroen Wenting
Ranch Hand

Joined: Oct 12, 2000
Posts: 5093

Srinivasa Raghavan
Ranch Hand

Joined: Sep 28, 2004
Posts: 1228
Check with the class "SimlpeDateFormat"

Thanks & regards, Srini
MCP, SCJP-1.4, NCFM (Financial Markets), Oracle 9i - SQL ( 1Z0-007 ), ITIL Certified
Ikasari Widiyanto

Joined: May 31, 2004
Posts: 29
I think its the other way around.. my date is in string then how to convert it to date format
Srinivasa Raghavan
Ranch Hand

Joined: Sep 28, 2004
Posts: 1228
Use the SimpleDateFormat, construct the date & use the parse method in that class.

Here it is.
Ikasari Widiyanto

Joined: May 31, 2004
Posts: 29
ok.. thanks.. I've tried the code but it gives me this error:
Error: java.text.ParseException: Unparseable date: "2005-3-1"
my code:
String datestart=request.getParameter("datestart");
DateFormat formatter = new SimpleDateFormat("yyMMddHHmmssZ");
try {
// parse utc into Date
java.util.Date date = formatter.parse(datestart);
catch(ParseException pe) {
out.println( "\nError: " + pe.toString() );
Horatio Westock
Ranch Hand

Joined: Feb 23, 2005
Posts: 221
You are trying to parse "2005-3-1" with "yyMMddHHmmssZ". If you read the SimpleDateFormat, there are example of how to build a suitable pattern.
Karthikeyan Rajendraprasad
Ranch Hand

Joined: Apr 16, 2003
Posts: 70
it should be

DateFormat formatter = new SimpleDateFormat("yy-MM-dd");

to get the your date format("2005-3-1") to be parsed correctly

Karthikeyan<br />SCJP 1.4, SCWCD.
Pedro Garcia

Joined: Sep 12, 2002
Posts: 15
Here is another example, you can customizing the format and language

I’ve looked at a lot of different solutions, and in my humble opinion Aspose is the way to go. Here’s the link:
subject: Get the Difference between 2 dates
It's not a secret anymore!