Joda Time - difference in "complete" months between two dates - java

How to calculate the number of "full" months between two dates with joda time, dropping incomplete months?
For example, I have 2 dates
LocalDate from = new LocalDate (2018, 9, 10);
LocalDate to = new LocalDate (2018, 11, 15);
Between these dates there is one "full" month - October from 1 to 31.
So I want to get is the number "1" by dropping the "incomplete" months - September and November
I need something like this
System.out.println(Months.monthsBetween(from, to).getMonths()); // returns 2
System.out.println(Months.**completed**MonthsBetween(from, to).getMonths()); // returns 1
UPD 1.
I could achieve what I want as follows:
LocalDate from = new LocalDate (2018, 9, 10);
LocalDate to = new LocalDate (2018, 11, 15);
if (to.getDayOfMonth() != 1)
from = from.plusMonths(1).withDayOfMonth(1);
if (to.getDayOfMonth() != 1)
to = to.withDayOfMonth(1);
System.out.println(Months.monthsBetween(from, to).getMonths());
but maybe there is an out of the box method?

Wouldn't this means that you simply want to remove one month from each interval and do a difference between them?
LocalDate fromMinusOne = from.minus(Months.ONE);
LocalDate toMinusOne = to.minus(Months.ONE);
System.out.println(Months.monthsBetween(fromMinusOne, toMinusOne).getMonths());

Joda Time provides methods to extract days, months and years between two dates. Create two instances of you date.
DateTime startDate = DateTime.parse("1970-01-01", DateTimeFormat.forPattern("yyyy-MM-dd"))
DateTime endDate = DateTime.parse("2015-02-25", DateTimeFormat.forPattern("yyyy-MM-dd"))
You can create your date instances in LocalDate as well instead of DateTime.
Now, complete months between above two dates can be found easily with,
int months = Months.monthsBetween(startDate.withDayOfMonth(1), endDate.withDayOfMonth(1)).getMonths()
For days,
int days = Days.daysBetween(startDate, endDate).getDays()
Difference between two dates in months
For years,
int years = Years.yearsBetween(startDate, endDate).getYears();

Related

java chrono unit difference between year

i have below code to get difference between two years..
long yearsBetween = ChronoUnit.YEARS.between(
customDate.toInstant().atZone(ZoneId.systemDefault()).toLocalDate(),
LocalDate.now());
my date value is as below
customDate = 2022-03-07
LocalDate.now() = 2021-10-07
but when i execute ChronoUnit.YEARS.between, it returns "0", but i am expecting "1" as return value. i want to compare only years for the given date and get the difference, excluding days..
If you want to ignore the month and day components of the LocalDate, you can just get the year from each LocalDate and then compare them, like so:
// Let's say we have two LocalDates, date1 and date2:
LocalDate date1 = LocalDate.of(2021, 10, 6);
LocalDate date2 = LocalDate.of(2022, 3, 7);
// We can get the year for each object and then subtract the two years:
long yearsBetween = date2.getYear() - date1.getYear();
// This will print 1 because 2022 is 1 year ahead of 2021:
System.out.println(yearsBetween);
As a side note, there is no need for yearsBetween to be a long data type (64-bit integer). The getYear() method returns an int (32-bit integer), and I doubt you'll ever have to deal with years that far in the future. We can just use:
int yearsBetween = date2.getYear() - date1.getYear();
You can then just plug in your dates where date1 and date2 are like so:
int yearsBetween = customDate.toInstant().atZone(ZoneId.systemDefault())
.toLocalDate().getYear()
- LocalDate.now().getYear();

Generate random dates within past 12 months

I'm using this Java code to generate random dates:
LocalDate localDate = LocalDate.now().minus(Period.ofDays((new Random().nextInt(365 * 70))));
Date date = Date.from(localDate.atStartOfDay(ZoneId.systemDefault()).toInstant());
Is there a way to limit the random dates within last 12 months?
You probably don't want to deal with timezones, leap years, leap seconds etc. yourself, so I recommend using the java.time library (java 8 and up).
If you only want LocalDate-precision, you could get any day within a specified range like this:
LocalDate now = LocalDate.now();
LocalDate then = now.minusYears(1);
long difference = now.toEpochDay() - then.toEpochDay();
int randomDifference = random.nextInt((int) difference);
LocalDate randomDate = then.plusDays(randomDifference);

Joda Time - difference in months between two dates [duplicate]

This question already has answers here:
Number of days between two dates in Joda-Time
(9 answers)
Closed 7 years ago.
I need to get the difference in months between two dates, I'm using Joda Time, the problem is this:
DateTime date1 = new DateTime().withDate(2015, 2, 1);
DateTime date2 = new DateTime().withDate(2015, 1, 1);
Months m = Months.monthsBetween(date1, date2);
int monthDif = m.getMonths();//this return 0
it returns 0 because there is no month in the middle of the two dates, I need to return the difference in months not a few months in between, and add 1 would be problematic when the dates are the same.
Changing the first date to 2015-02-02, Joda correctly returns 1 month:
DateTime date1 = new DateTime().withDate(2015, 2, 2);
DateTime date2 = new DateTime().withDate(2015, 1, 1);
System.out.println(Months.monthsBetween(date2, date1).getMonths());
// Returns 1.
So my guess is that because you didn't provide a time portion, Joda cannot be precise about exactly which point in time of 2015-01-01 date2 refers to. You might have as well referred to 23:59:59, in which case a full month wouldn't have elapsed yet, technically.
If you provide a zero time portion explicitly, it works as you initially expected:
DateTime date1 = new DateTime().withDate(2015, 2, 1).withTime(0, 0, 0, 0);
DateTime date2 = new DateTime().withDate(2015, 1, 1).withTime(0, 0, 0, 0);
System.out.println(Months.monthsBetween(date2, date1).getMonths());
// Returns 1.
Therefore, I recommend you specify a 00:00:00 time portion in each date explicitly.
While other answers are correct they still mask the real problem.
it returns 0 because there is no month in the middle of the two dates
No. It returns 0 because there is time part of DateTime object. You creating two istances of DateTime filled with current moment in time (with hours, minutes, seconds and milliseconds) and then modify just date part. There is no reasons to do it if you want to compare just two dates. use LocalDate instead.
LocalDate date1 = new LocalDate(2015, 2, 1);
LocalDate date2 = new LocalDate(2015, 1, 1);
Months m = Months.monthsBetween(date1, date2);
int monthDif = Math.abs(m.getMonths());//this return 1
Also need to pay attention to the fact that despite the fact that Months docs say nothing about it, Month can contain negative value if first date is after second date. So we need to use Math.abs to really count the number of months between two dates.
The docs say:
Creates a Months representing the number of whole months between the two specified partial datetimes.
But it isn't true. It really calculates the difference in months. Not the number of months.
The way this is calculated depends on the business logic that is to be used. Each month varies in length. One option would be to, in the monthsBetween() function, get the start of the month for both date1 and date2, and compare that.
Something like:
DateTime firstOfMonthDate1 = new DateTime(date1.getYear(), date1.getMonthOfYear(), 1, 0, 0);
DateTime firstOfMonthDate2 = new DateTime(date2.getYear(), date2.getMonthOfYear(), 1, 0, 0);
Months m = Months.monthsBetween(firstOfMonthDate1, firstOfMonthDate2)

How many instances of a partial date (month & day) appear in a range, and its day of the week

In Java, how would I go about constructing a utility that would take a range of dates (start and end date) and then would see how many times a given partial date ( the month and day-of-month) appears in that range, and will add an entry to a list for each match.
In my instance, I want to give it a range of say 5 years - starting Jan 1st 2014 and going to Dec 31st 2019. My check date is the 2nd August. I want the method to return the full information about each match of any August 2 of any year in the range. So for 2014 is will return Saturday 2nd August 2014, then Sunday 2nd August 2015 etc and so on.
I've been trying to get something working so far with Joda Time and the default date/calendar classes in Java and I'm just getting myself in a mess.
Thanks,
S
Edit: How silly of me, apologies for not adding my code :(
public static List<Date> getDaysInRange(Date startdate,
Date enddate,
Date checkDate) {
SimpleDateFormat sdf = new SimpleDateFormat("MMdd");
List<Date> dates = new ArrayList<>();
Calendar cal = new GregorianCalendar();
cal.setTime(startdate);
while (cal.getTime().before(enddate)) {
if (sdf.format(cal.getTime()).equals(sdf.format(checkDate))) {
Date result = cal.getTime();
dates.add(result);
}
cal.add(Calendar.DATE, 1);
}
return dates;
}
Date-Only
Since you want only a date without time-of-day and without time zone, use a date-only class. The old java.util.Date/.Calendar classes lack such a class. And those old classes are notoriously troublesome and flawed.
Instead use either:
Joda-Time
java.time, built into Java 8, inspired by Joda-Time.
Joda-Time
Here is some untested code using Joda-Time 2.6.
The main idea is to focus on the small set of possible year numbers rather than test every day of year. In the example below, that means six date-time values to compare rather than thousands. Besides efficiency, the purpose of the code becomes more apparent.
The arguments to your routine should be a month number and a day-of-month number, a pair of ints or Integers, rather than a Date. As seen in this examples two int variables, month and day.
LocalDate start = new LocalDate( 2011, 2, 3 );
LocalDate stop = new LocalDate( 2016, 4, 5 );
int yearStart = start.getYear();
int yearStop = stop.getYear();
int month = 11;
int day = 22;
for ( i = yearStart, i <= yearStop, i++ )
{
LocalDate x = new LocalDate( i, month, day );
boolean matchStart = ( x.isEqual( start ) || x.isAfter( start ) );
boolean matchStop = x.isBefore( stop ); // Half-Open approach where beginning of range is inclusive while ending is exclusive.
if ( matchStart && matchStop )
{
// Add to collection of LocalDate objects.
// Later you can ask each LocalDate object for its day-of-week.
{
}
java.time
The java.time package also offers a LocalDate class. The code would be similar to the above Joda-Time example.
I think using SimpleDateFormat is a bad idea. Use Calendar for comparison directly, like this
cal1.get(Calendar.MONTH) == cal2.get(Calendar.MONTH) && cal1.get(Calendar.DAY_OF_MONTH) == cal2.get(Calendar.DAY_OF_MONTH)

Joda-Time convert days to months and days

How can I convert the number of days to number of months and days using Joda-Time. For example, when I have 33 days, it should display 1 month and 2 days.
public static void main(String[]args)
{
Calendar calendar = new GregorianCalendar();
int years = calendar.get(Calendar.YEAR);
int month = (calendar.get(Calendar.MONTH))+1;
int day = calendar.get(Calendar.DATE);
DateTime startDate = new DateTime(years, month, day, 0, 0, 0, 0);
DateTime endDate = new DateTime(2014, 7, 3, 0, 0, 0, 0);
Days d = Days.daysBetween(startDate, endDate);
int days = d.getDays();
int t = 1000 * 60 * 60 *24;
int days = days/t;
System.out.println(days);
}
You can use class org.joda.time.Period for this.
Example:
Period p = new Period(startDate, endDate, PeriodType.yearMonthDayTime());
System.out.println(p.getMonths());
System.out.println(p.getDays());
Trying to create a decimal fraction number to represent months makes no sense to me, as months have different numbers of days (28, 29, 30, 31).
ISO 8601
The sensible ISO 8601 standard defines a textual way to represent a span of time in terms of months and days, called Durations. Joda-Time has a class for this purpose called Period. Forgive the mismatch in terms, as there is no standard definition of date-time terminology yet.
For an example of using the Period class, see this other answer by Ilya on this question.
ISO Duration
The textual format is PnYnMnDTnHnMnS where P means "Period" and the T separates the date portion from time portion. The other parts are optional. One month and two days would be P1M2D. The Joda-Time Period class both parses and generates such strings.

Categories

Resources