Solved

Get num days between 2 Calendars

Posted on 2006-06-25
4
263 Views
Last Modified: 2010-03-31
I have two Calendar objects, with their time fields (hour, minute, second, millisecond) all set to zeros. I want to get the days difference between these two objects.

Example:

Calendar 1: 2006-06-26
Calendar 2: 2006-06-27
results: -1

Calendar 1: 2006-06-26
Calendar 2: 2006-06-25
results: 1
0
Comment
Question by:yongsing
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 10

Expert Comment

by:mukundha_expert
ID: 16981078
Use compareTo() to get the difference in time in Milliseconds,

from this you can get the difference of days,

int time = cal1.compareTo(cal2);

int days = time / 1000*60*60*24

0
 
LVL 10

Accepted Solution

by:
mukundha_expert earned 30 total points
ID: 16981086
sorry am not sure about compareTo(),

try this

long cal1time = cal.getTimeinMillis()
long cal2time = cal2.getTimeinMillis()

long diff = cal1time - cal2time;

int days = diff / 1000*60*60*24
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 16981168
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 16981169
0

Featured Post

Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The viewer will learn how to implement Singleton Design Pattern in Java.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

738 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question