aspose file tools*
The moose likes Java in General and the fly likes How to 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 "How to get the difference between 2 dates ?" Watch "How to get the difference between 2 dates ?" New topic
Author

How to get the difference between 2 dates ?

Eusebio Floriano
Ranch Hand

Joined: Mar 07, 2004
Posts: 237
Hi folks,

I have 2 dates and need get the difference between these dates in days.
How can i do it ?

Regards,


SCJP 1.4 / 5.0 - SCBCD 1.3 - SCWCD 1.4 - IBM 484
Arnaud Burlet
Ranch Hand

Joined: Oct 08, 2004
Posts: 31
Hi,

I assume you have 2 java.util.Date, "just substract them".


Don't forget to check the sign of delta !

Arnaud
Ravi Harishchandra
Greenhorn

Joined: Oct 29, 2012
Posts: 17
i havent used java.util.date instead i have used ecternal plug-in Jcalender......since i have to so a complete Graphical app for the hotel....i need to use jCalender
K. Tsang
Bartender

Joined: Sep 13, 2007
Posts: 2531
    
    8

Ravi Hckd wrote:i havent used java.util.date instead i have used ecternal plug-in Jcalender......since i have to so a complete Graphical app for the hotel....i need to use jCalender


Not sure about Jcalendar plug in. But if it's similar to the Java class Calendar, then you can first convert the Calendar or Jcalendar to a Date then subtract.


K. Tsang JavaRanch SCJP5 SCJD/OCM-JD OCPJP7 OCPWCD5 OCPBCD5
Ravi Harishchandra
Greenhorn

Joined: Oct 29, 2012
Posts: 17
The functionality of the plug-in is different......you get the time /date in IS format.....but i'm unable to subtract the dates...all that i've done is or can be done is
convert it into string...can you tell me if is it possible to reconvert it into integer(the date stored as String) then subtract it.....
also should i upload the jCalender plug-in so that you can have a look??
K. Tsang
Bartender

Joined: Sep 13, 2007
Posts: 2531
    
    8

Ravi Hckd wrote:The functionality of the plug-in is different......you get the time /date in IS format.....but i'm unable to subtract the dates...all that i've done is or can be done is
convert it into string...can you tell me if is it possible to reconvert it into integer(the date stored as String) then subtract it.....
also should i upload the jCalender plug-in so that you can have a look??


You should check the API for JCalendar plug-in. Look for methods that converts a string to Date or long.

Having code examples can help us better understand your issue.
Ravi Harishchandra
Greenhorn

Joined: Oct 29, 2012
Posts: 17
I have pasted the code below and will also give a screen shot of the same

CODE:
Date dateFromDateChooser = jDateChooser1.getDate();
String dateString = String.format("%1$tY/%1$tm/%1$td", dateFromDateChooser);
dateFromDateChooser = jDateChooser2.getDate();
String dateString1 = String.format("%1$tY/%1$tm/%1$td", dateFromDateChooser);

The 2 different Date Choosers are used to choose the different dates....I'm unable to get the total number of Days from the above code.



[Thumbnail for 1.jpg]

Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4422
    
    8

Your code shows that you do in fact get the dates as Date objects. You're the one who is then formatting them to a string. Just use the Date objects, and you can use the approach that Arnaud suggested.

(Edit - that Arnaud suggested 8 years ago - you really should have opened a new thread for this).
Ravi Harishchandra
Greenhorn

Joined: Oct 29, 2012
Posts: 17
I'm new to this site....can you post that thread you mentioned for me?? Regarding the code...yes you do get the object but that's the dead end for it....
You cannot process it further( considering my knowledge:::: working on java first time------pretty different from .NET that;s why) or maybe there is another way....i don't know...
please can you give me the exact code for that...should i upload the entire application so that you get a better understanding....
waiting for your reply...do let me know
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4422
    
    8

Ravi Hckd wrote:yes you do get the object but that's the dead end for it....
You cannot process it further

Since you've got Date objects, you can do exactly what the message you replied to suggested:

Then divide by whatever is necessary to get it into whatever units you want.

Ravi Harishchandra
Greenhorn

Joined: Oct 29, 2012
Posts: 17
Thanks buddy.....the problem is solved.......
Mark Butcher
Ranch Hand

Joined: Jun 28, 2012
Posts: 46
hey there,

I am in a similar situation where am getting a DATE from oracle and counting the number of days till the current day.
Now, how to convert oracle DATE into java.util.DATE to find the difference in days ?

Thanks
Mark
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Mark Butcher wrote:
Now, how to convert oracle DATE into java.util.DATE to find the difference in days ?


There's no such thing as "convert oracle DATE into java.util.DATE to find the difference in days". At least, not without you making it.

There are two main parts to your question:

1) Convert oracle DATE into java.util.DATE. This is accomplished by calling ResultSet.getDate(...).

2) Find the difference between two dates. I believe that is answered in this thread.

So, what specifically are you having trouble with?
Ravi Harishchandra
Greenhorn

Joined: Oct 29, 2012
Posts: 17
@Jeff Verdegan :: If you have dates stored in Oracle you can write a simple function to get the number of days.....then all you need is to retrieve that integer value.......
Will post that function in sometime....also Oracle 11g Release 2 has excellent features regarding date manipulation.....hope this solves your problem
Ravi Harishchandra
Greenhorn

Joined: Oct 29, 2012
Posts: 17
Jeff Verdegan wrote:
Mark Butcher wrote:
Now, how to convert oracle DATE into java.util.DATE to find the difference in days ?


There's no such thing as "convert oracle DATE into java.util.DATE to find the difference in days". At least, not without you making it.

There are two main parts to your question:

1) Convert oracle DATE into java.util.DATE. This is accomplished by calling ResultSet.getDate(...).

2) Find the difference between two dates. I believe that is answered in this thread.

So, what specifically are you having trouble with?


@Jeff Verdegan :: If you have dates stored in Oracle you can write a simple function to get the number of days.....then all you need is to retrieve that integer value.......
Will post that function in sometime....also Oracle 11g Release 2 has excellent features regarding date manipulation.....hope this solves your problem
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Ravi Hckd wrote:@Jeff Verdegan ::....hope this solves your problem


I'm not the one having a problem. I was asking questions trying to get the OP to clarify what his problem is.

Good information about the Oracle date utilities though.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to get the difference between 2 dates ?