• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Rob Spoor
  • Devaka Cooray
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Jj Roberts
  • Al Hobbs
  • Piet Souris

Get the Difference between 2 dates

 
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Bartender
Posts: 10336
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 67595
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Moving to the Java in General (intermediate) forum.
 
Ranch Hand
Posts: 884
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Ranch Hand
Posts: 5093
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
DateFormat
 
Ranch Hand
Posts: 1228
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Check with the class "SimlpeDateFormat"
 
Ikasari Widiyanto
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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() );
}
 
Ranch Hand
Posts: 221
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here is another example, you can customizing the format and language
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic