-1

I've to write a generic code as such, it should give months difference between two dates.

For eg., my input data will be

1.  date1 = 22/01/2016
date2 = 30/03/2016
2. date1 = 22/01/2016
date2 = 20/12/2015

based on months difference, I've to move forward(if date2 is future date to date1) or backward(if date2 is past date to date1), those many times.

khmarbaise
  • 92,914
  • 28
  • 189
  • 235

3 Answers3

0

You could simply compare these two dates as stated here

According to the result you just have to call the same code as if you have clicked on the button yourself (just call the respective mehtod (if you don't use a method for that I would advise you to do so))

Greetings Raven

EDIT:
The difference betweent two dates can be calculated like this

Community
  • 1
  • 1
Raven
  • 2,951
  • 2
  • 26
  • 42
  • actually, i want no: of months to give clicks using selenium-click – Madhavi Mokkapati Dec 10 '15 at 18:07
  • so you need the difference between the two dates? – Raven Dec 10 '15 at 18:09
  • only months difference. and dates combination can be : [date1(present),date2(future)] or [date1(present), date2(past)] – Madhavi Mokkapati Dec 10 '15 at 18:12
  • [date1(present),date2(future)] - with this difference i have to click on next. And, [date1(present), date2(past)] - with this difference i have to click on previous. But the bad thing is, I cant predict date2 whether it is 'Future' or 'Past'. – Madhavi Mokkapati Dec 10 '15 at 18:19
  • Then just subtract the different month values and multiply by -1 if necessary... Or am I missing something? – Raven Dec 10 '15 at 18:19
  • I think you missed to see my previous comment. Once check with that. – Madhavi Mokkapati Dec 10 '15 at 18:24
  • Thank so much for help. But, if simple subtraction is performed, then how to make clicks on diff buttons like next and previous. – Madhavi Mokkapati Dec 10 '15 at 18:43
  • Well if the difference is greater zero then the second date is in the past (assuming that you substract month1 by month2) and if it is less zero then vise versa...And the modulus gives you the amount of clicks you have to perform on the respective button – Raven Dec 10 '15 at 20:27
  • Thanq u so much, for help – Madhavi Mokkapati Dec 11 '15 at 07:33
  • Assuming Month1-Month2, for getting months difference, if years are same and If month1= 03 and month2 = 01, then it is 2 and ans is fyn but if years are different and if month1 = 12 and month2 = 03, then it is 9 and ans is not fyn – Madhavi Mokkapati Dec 14 '15 at 06:04
  • You have to calculate the difference between 12-difference and the difference itself – Raven Dec 15 '15 at 16:10
0
Calendar data = Calendar.getInstance();
Calendar data2 = Calendar.getInstance();
int diff =(int) Math.round(( (data2.getTimeInMillis() - data.getTimeInMillis()) /* ms*/
              / 1000.0 /* seconds*/
              / 3600.0 /*hours*/
              / 24.0 /*days*/
              / 30.0 ))/* monts*/;

With Math.round you ensure months of 31 and 28 days return the right value Replace data and data2 for your own values

Aitor
  • 195
  • 1
  • 11
  • Thanks for help. But, it gives days differences, i require months difference – Madhavi Mokkapati Dec 10 '15 at 18:35
  • No, it gives months differences. Check it out – Aitor Dec 10 '15 at 18:43
  • FYI, the troublesome old date-time classes such as [`java.util.Date`](https://docs.oracle.com/javase/8/docs/api/java/util/Date.html), [`java.util.Calendar`](https://docs.oracle.com/javase/9/docs/api/java/util/Calendar.html), and `java.text.SimpleDateFormat` are now [legacy](https://en.wikipedia.org/wiki/Legacy_system), supplanted by the [java.time](https://docs.oracle.com/javase/8/docs/api/java/time/package-summary.html) classes built into Java 8 & Java 9. See [Tutorial by Oracle](https://docs.oracle.com/javase/tutorial/datetime/TOC.html). – Basil Bourque Oct 26 '17 at 21:56
0

tl;dr

Period.between( 
    LocalDate.of( 2016 , Month.JANUARY , 22 ) , 
    LocalDate.of( 2016 , Month.MARCH , 30 )
).isNegative()

Details

Your Question is not clear, but this might help point you in the right direction.

Avoid the troublesome old date-time classes such as Date that are now legacy, supplanted by the java.time classes.

LocalDate

The LocalDate class represents a date-only value without time-of-day and without time zone.

date1 = 22/01/2016 date2 = 30/03/2016

LocalDate start = LocalDate.of( 2016 , Month.JANUARY , 22 ) ;
LocalDate stop = LocalDate.of( 2016 , Month.MARCH , 30 ) ;

Period

Calculate the number of days, months, and years elapsed as a Period.

Period period = Period.between( start , stop ) ;

Test if the stop comes before the start, meaning the elapsed time is negative, going backwards along the timeline.

Boolean isTimeGoingBackwards = period.isNegative() ;

Months

Your comments mention getting a number of months. Understand multiple ways to count months:

  • Count days elapsed, divided by 30 as the length of a generic month.
  • Count of calendar months completely covered
  • Count of calendar months touched by the span of time

Java offers at least some of these. Read the documentation to decide which meets your needs. If going backwards in time, the resulting number is negative.

ChronoUnit.MONTHS

long months = ChronoUnit.MONTHS.between( start , stop ) ;

Period.toTotalMonths

int months = Period.between( start , stop ).toTotalMonths() ;

About java.time

The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as java.util.Date, Calendar, & SimpleDateFormat.

The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.

To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.

Where to obtain the java.time classes?

The ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as Interval, YearWeek, YearQuarter, and more.

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154