Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Get the Difference between 2 dates

 
Ikasari Widiyanto
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I would like to how how to calculate the difference between 2 dates by using jsp... thanks..
best regards,
Ika
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64967
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Moving to the Java in General (intermediate) forum.
 
Chengwei Lee
Ranch Hand
Posts: 884
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ikasari Widiyanto
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hehe.. thanks.. but I got 1 problem.. the dates is in string format... how to change this to date format?
 
Jeroen Wenting
Ranch Hand
Posts: 5093
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
DateFormat
 
Srinivasa Raghavan
Ranch Hand
Posts: 1228
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Check with the class "SimlpeDateFormat"
 
Ikasari Widiyanto
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think its the other way around.. my date is in string then how to convert it to date format
 
Srinivasa Raghavan
Ranch Hand
Posts: 1228
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Use the SimpleDateFormat, construct the date & use the parse method in that class.

Here it is.
 
Ikasari Widiyanto
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 221
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
it should be

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

to get the your date format("2005-3-1") to be parsed correctly
 
Pedro Garcia
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is another example, you can customizing the format and language
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic