wood burning stoves 2.0*
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


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Get the Difference between 2 dates" Watch "Get the Difference between 2 dates" New topic
Author

Get the Difference between 2 dates

Ikasari Widiyanto
Greenhorn

Joined: May 31, 2004
Posts: 29
Hi,

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

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
Marshal

Joined: Jan 10, 2002
Posts: 61066
    
  66

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.

HTH.


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

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
DateFormat


42
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
Greenhorn

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
Greenhorn

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);
out.println(date);
}
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
Greenhorn

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


Pedro
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Get the Difference between 2 dates