Solved

Get num days between 2 Calendars

Posted on 2006-06-25
4
226 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
  • 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:mayankeagle
ID: 16981168
0
 
LVL 30

Expert Comment

by:mayankeagle
ID: 16981169
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:

746 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now