For the following Period calculation:
Period.between(LocalDate.of(2015, 8, 1), LocalDate.of(2015, 9, 2))
the result is:
P1M1D
This is equivalent to 31 days + 1 day = 32 days.
For this Period:
Period.between(LocalDate.of(2015, 8, 1), LocalDate.of(2015, 10, 2))
the result is:
P2M1D
This is equivalent to: 31 days (in August) + 30 days (in September) + 1 (in October) = 62 days
Is there a method in the java.time package which will give the number of days in a Period? I can't find one. Not sure if I have overlooked anything or if it is just plain not there.
From the documentation:
To define an amount of time with date-based values (years, months,
days), use the Period class. The Period class provides various get
methods, such as getMonths, getDays, and getYears.To present the amount >of time measured in a single unit of time, such as days, you can use the
ChronoUnit.between method.
LocalDate today = LocalDate.now();
LocalDate birthday = LocalDate.of(1960, Month.JANUARY, 1);
Period p = Period.between(birthday, today);
long p2 = ChronoUnit.DAYS.between(birthday, today);
System.out.println("You are " + p.getYears() + " years, " + p.getMonths() +
" months, and " + p.getDays() +
" days old. (" + p2 + " days total)");
The code produces output similar to the following:
You are 53 years, 4 months, and 29 days old. (19508 days total)
There is no way to do what you ask. The reason is that it is not possible from a Period to deduce the actual number of calendar days in the period. A Period is not tied to specific dates, once constructed in the way you show, it loses track of the actual calendar dates.
For example your first period represents a period of 1 month and 1 day. But the period does not care which month. It is simply a concept of "a month and a day".
If you need the number of days between two dates you should use ChronoUnit.DAYS.between as Saket Mittal writes.
There's a specific object depending at the amount of time you'd like to deal with.
This page here is very useful explaining which is best for your scenario.
The ChronoUnit.between method is useful when you want to measure an amount of time in a single unit of time only, such as days or seconds
LocalDate localDateStartDate = LocalDate.of(2016, 06, 10);
LocalDate localDateEndDate = LocalDate.of(2016,06,23);
long days = ChronoUnit.DAYS.between(localDateStartDate, localDateEndDate);
Related
I am trying to calculate the difference between two LocalDate objects and the result seems to be off, but not every time.
I am using the Period construct. The below code shows one example which returns the expected result (I got that here) and another one which gives me the "wrong" result. I put that in quotes because I am not sure if that truly is wrong, or if the expected value is wrong. Note however, that if I use the online calculator from calculator.net, that gives me the result I expect.
public void manualTestPeriodBetween() {
//works fine - expected result obtained
LocalDate start = LocalDate.of(2014, 2, 14);
LocalDate end = LocalDate.of(2017, 8, 1);
Period result = Period.between(start, end);
Period expected = Period.of(3, 5, 18);
checkPeriods(expected, result);
//does not work as expected
start = LocalDate.of(2017, 5, 19);
end = LocalDate.of(2019, 7, 13);
result = Period.between(start, end);
expected = Period.of(2, 1, 25);
checkPeriods(expected, result);
}
private void checkPeriods(Period expected, Period result) {
System.out.println("expected Period = " + expected + ", resulting Period = " + result);
if (result.equals(expected)) {
System.out.println("SUCCESS - result Period matches expected");
} else {
System.out.println("FAIL - result Period not matched");
}
}
Output:
expected Period = P3Y5M18D, resulting Period = P3Y5M18D
SUCCESS - result Period matches expected
expected Period = P2Y1M25D, resulting Period = P2Y1M24D
FAIL - result Period not matched
Can someone help me figure out whether I am missing something or the expected result is wrong (both in my code and the online date calculator)? or maybe something else I am not even considering.
Here is a screenshot of the results obtained from the online date calculator:
The online date calculator you have provided may have been implemented incorrectly. From my favorite time resource, timeanddate
From and including: Friday, May 19, 2017
To, but not including Saturday, July 13, 2019
Result: 785 days
It is 785 days from the start date to the end date, but not including the end date.
Or 2 years, 1 month, 24 days excluding the end date.
Or 25 month, 24 days excluding the end date.
This is identical to the response provided by thecalculatorsite.com
I don't have the implementation for your online calculator, but the discrepancy appears to occur because May is a 31-day month.
It looks to be calculating the day-difference before the month difference, using some mathematical assumption for month length.
The Java method uses epochday to calculate the distances between two days in case of underflow, rather than adding an arbitrary value to offset a month.
if (totalMonths > 0 && days < 0) {
totalMonths--;
LocalDate calcDate = this.plusMonths(totalMonths);
days = (int) (end.toEpochDay() - calcDate.toEpochDay()); // safe
} else if (totalMonths < 0 && days > 0) {
totalMonths++;
days -= end.lengthOfMonth();
I mean, it should be 24 days depending on how you do the calculations.
If you add a month to get to june 19th, and june has 30 days- then it's only 24 days between the two dates.
https://www.wolframalpha.com/input/?i=days+between+5%2F19%2F2017+and+7%2F13%2F2019
The website you are checking it against is wrong, I can think of several ways that could happen depending on how they are implementing their date difference functions.
Hello I'm able to get the seconds, minutes, hours and days, but when I try to get the years the problem comes up.
I'm using the following code:
Calendar startDate = new GregorianCalendar(year, month, day);
Calendar date = Calendar.getInstance();
long diff = date.getTimeInMillis() - startDate.getTimeInMillis();
long seconds = diff / 1000;
long minutes = seconds / 60;
long hours = minutes / 60;
long days = hours / 24;
double years = date.get(Calendar.YEAR) - startDate.get(Calendar.YEAR);
Let's say the start date is 07/25/1994 and the end date is 07/28/2015
the result that I get is (21.0) instead of the of 20.97 . I need to get the exact age in YEARS for the users so; can you help me guys out?. Thanks!
Use BigDecimal instead of double and long. When you want precise calculations of floating point numbers, always use BigDecimal. Floating point math is not reliable in java. Simple example is System.out.println(2.00 - 1.10); guess what, it doesnt print .90.
You need to cast your variables:
double years = (double) date.get(Calendar.YEAR) - (double) startDate.get(Calendar.YEAR);
Is your question about elapsed time or about decimal number calculation?
If the first, you should know that decimal numbers for elapsed years is somewhat odd. If the second, you should change the title of your Question.
Elapsed Time
For Android, you should be using the Joda-Time library rather than the old java.util.Date/.Calendar classes. The old classes are notoriously troublesome.
The ISO 8601 standard defines string formats for various kinds of date-time values. For a span of time in terms of a count of years, months, days, hours, minutes, and seconds, the format is PnYnMnDTnHnMnS where P defines the beginning and T separates the days portion from hours portion. The Question is about a span of P21Y3D, or 21 years and 3 days. The Joda-Time library uses this standard format for both parsing and generating such strings.
LocalDate start = new LocalDate( 1994, 7, 25 ); // 07/25/1994
LocalDate stop = new LocalDate( 2015, 7, 28 ); // 07/28/2015
Period period = new Period( start, stop );
System.out.println("start: " + start + " to stop: " + stop + " is " + period );
When run.
start: 1994-07-25 to stop: 2015-07-28 is P21Y3D
Decimal Number Calculation
If you are just asking about the decimal numbers, then the answer by Samrat Dutta is correct: Use BigDecimal if you care about accuracy. Otherwise you are using primitives with floating-point calculations. Floating-point trades off accuracy for speed of execution. As a general rule, if in doubt about which to use for business problems, go with BigDecimal rather than floating-point.
What do you mean by "exact age in years"? Here's the number of years with up to 200 decimal places:
21.00752908966461327857631759069130732375085557837097878165639972621492128678986995208761122518822724161533196440793976728268309377138945927446954140999315537303216974674880219028062970568104038329911020
Integer days = Days.daysBetween( start, stop ).getDays( );
BigDecimal daysPerYear = new BigDecimal( 365.25 ); // Approximate.
int scale = 200; // Number of fractional digits desired.
BigDecimal years = new BigDecimal( days ).divide( daysPerYear, scale, RoundingMode.HALF_EVEN ); // Banker's rounding.
System.out.println( "days: " + days + " ÷ " + daysPerYear + " = " + years + " years." );
When run.
days: 7673 ÷ 365.25 = 21.00752908966461327857631759069130732375085557837097878165639972621492128678986995208761122518822724161533196440793976728268309377138945927446954140999315537303216974674880219028062970568104038329911020 years.
As I said, you may find P21Y3D makes more sense than this decimal number.
I need the number of days in a year and I wanted to use Java8's new time api.
However, I can't do Duration.ofDays(365) because it doesn't account for leap years. And Duration.of(1, ChronoUnit.YEARS) doesn't fly because of java.time.temporal.UnsupportedTemporalTypeException: Unit must not have an estimated duration
I looked into Period, but it doesn't appear useful for going from years to days.
I feel like I'm missing something here? I could write something to add a day if the year is a leap year, but it seems like I should be able to handle this out of the box.
As per the response in Getting Duration using the new dateTime API you should be using
Period p = Period.ofYears(1);
It's important to understand the difference between Duration (exact number of nanoseconds < 1 day) and Period (variable > 1 day).
Duration won't account for leap days, daylight savings time or leap seconds, for example, and is intended for durations of less than a day, at most a few days.
So you should use Period instead.
Because different years have different number of days, if you want to find the number of days in a year, you need to specify which year you're talking about.
If you want the number of days in a specific year, you can use
Year.of(year).length()
If you want the date one year from now, you can use
LocalDate.now().plusYears(1)
or
LocalDate.now().plus(Period.ofYears(1))
If you need the number of days between two dates, you can use
ChronoUnit.DAYS.between(start, end)
So to find the number of days to the date a year from now, you can use
LocalDate today = LocalDate.now();
long days = ChronoUnit.DAYS.between(today, today.plusYears(1));
If you want to see whether a membership of one year is still valid, you can use
Period membershipLength = Period.ofYears(1);
LocalDate membershipStart = ...;
LocalDate membershipEnd = membershipStart.plus(membershipLength);
LocalDate today = LocalDate.now();
boolean memberShipEnded = today.isAfter(membershipEnd);
boolean membershipValid = !membershipEnded;
It seems clear you do not want a duration (= between two dates), but the year length of a specific date.
LocalDate dateLeap = LocalDate.of(2004, Month.MARCH, 1);
System.out.println("leap year of " + dateLeap
+ " has days: " + dateLeap.lengthOfYear());
leap year of 2004-03-01 has days: 366
Java 8 Date & Time is astonishing complete.
If you mean, in January 5th 2004 to January 5th 2005 = 366 and March 2nd 2004 to March 2rd 2005 = 365:
int lengthOfYear(LocalDate date) {
return date.getMonthValue() <= 2
? date.lengthOfYear() // Count Feb in this year
: date.plusYears(1).lengthOfYear(); // Count Feb in next year
}
Explanation: basically the length is 365. But if date is >= March, the February in the next year is counted, otherwise this year's February.
Mind that plusYears(1) will not change DAY or MONTH.
Also neither leap second nor hour/minuts on February, 29th are considered.
I have a requirement where I need to find out number of months between two dates including extra days.
example:
start date:01/01/2014
end date:21/02/2014
LocalDate startDate = new LocalDate(startDate1);
LocalDate endDate = new LocalDate(endDate1);
PeriodType monthDay =PeriodType.yearMonthDay().withYearsRemoved();
Period difference = new Period(startDate, endDate, monthDay);
int months = difference.getMonths();
int days = difference.getDays()
the out put I will get is:
months:1 days:20
but my requirement is I want get total months including that extra day.
like:1.66 months.
How to get this one in java?
In order to be able to say 1.66 months you need to define how long a month is. It's not always the same. If you assume that a month is 30 days long then you can solve this by using:
Date startDate = new SimpleDateFormat("dd/MM/yyyy").parse("01/01/2014");
Date endDate = new SimpleDateFormat("dd/MM/yyyy").parse("21/02/2014");
double result = (endDate.getTime() - startDate.getTime()) / (1000D*60*60*24*30);
This gives us 1.7 and if you divide with 31 you get 1.6451612903225807.
If you want a better (but not perfect) approximation of how long a month is you can try 365/12 which will give you 1.6767123287671233 but still this is not perfect because leap years have 366 days.
The problem though is not with the formula, but with the problem definition. Nobody in real life says "I'll be there in exactly 1.66 months" and nobody will ever ask you to convert 1.66 months in days.
This is my own answer, a variation on cherouvim's
final Date startDate = new GregorianCalendar (2014, 0, 1).getTime ();
final Date endDate = new GregorianCalendar (2014, 1, 21).getTime ();
System.out.println ((endDate.getTime () - startDate.getTime ()) / (float) (1000L * 60 * 60 * 24 * 30));
I'm not sure what I'm doing wrong, but I've got a piece of code which calculates the number of days between two dates, and which looks something like the following:
final Calendar first = new GregorianCalendar(2010, Calendar.OCTOBER, 1);
final Calendar last = new GregorianCalendar(2010, Calendar.NOVEMBER, 1);
final long difference = last.getTimeInMillis() - first.getTimeInMillis();
final long days = difference / (1000 * 60 * 60 * 24);
System.out.println("difference: " + difference);
System.out.println("days: " + days);
To summarise, the code block above calculates the number of days between 1st October 2010 and 1 November 2010. I'm expecting to see it return 31 days (seeing as there's 31 days in October)
difference: xxxx
days: 31
but instead it's showing 30 days in October!
difference: 2674800000
days: 30
I've managed to narrow it down to between the the dates 2 October 2010 and 3 October 2010, which seems to only have 82800000 milliseconds, instead of a full 86400000 milliseconds (exactly one hour missing).
Does anyone have any ideas what I'm doing wrong? Or is the 2nd October a special date which has one minute less than a regular day?
(86400000 - 82800000)/1000 = 3600, which is one hour. You're seeing daylight savings time, combined with the rounding of integer math
You could get around it by doing the calculations with floating point numbers and rounding at the end, or you could check out a library like Joda time which offers much better date math than what's built in.
You may be better off comparing the year and day or year instead of the milliseconds that pass in a day.
int lastYear= last.get(Calendar.YEAR);
int firstYear= first.get(Calendar.YEAR);
int lastDayofYear = last.get(Calendar.DAY_OF_YEAR);
int firstDayofYear = first.get(Calendar.DAY_OF_YEAR);
int nDaysElapse = lastDayofYear - firstDayofYear;
int nYearsElapse = lastYear- firstYear;
int days = (nYearsElapse*365)+nDaysElapse;
You should read this post to get a better understanding of how Calendar is interrelated with date/time stamps.
Having read that site, my initial questions were:
What do you mean by days? Do you mean '24-hour blocks' or do you mean calendar days? In the same vein, do you care if you are off slightly due to daylight savings etc?
If you mean Calendar days, your best bet is probably to:
final Calendar first = new GregorianCalendar(2010, 9, 1);
final Calendar last = new GregorianCalendar(2010, 10, 1);
Calendar difference = Calendar.getInstance();
difference.setTimeInMillis(last.getTimeInMillis() - first.getTimeInMillis());
int numDays = difference.get(Calendar.DAY_OF_YEAR) - difference.getMinimum(Calendar.DAY_OF_YEAR);
Of course, the above code will only work if the number of days < 365. You will need to create a rolling calculation e.g.
int yearDiff = last.get(Calendar.YEAR) - first.get(Calendar.YEAR);
Calendar tmp = new GregorianCalendar();
tmp.setTimeInMillis(first.getTimeInMillis());
for(int i = 0; i < yearDiff; i++) {
numDays += tmp.getActualMaximum(Calendar.DAY_OF_YEAR);
i++;
tmp.add(Calendar.YEAR, 1);
}
This should allow you to get the number of days in a correct and consistent manner, without worrying about Daylight Savings, Leap Years etc.
That said, JodaTime probably has this functionality built in.
The answer by Brad Mace is correct.
Use a Library
This question is a good example of why you should use a good date-time library wither than roll your own. For Java that means using either Joda-Time or the new java.time package in Java 8.
Joda-Time
Example code in Joda-Time.
DateTimeZone timeZone = DateTimeZone.forID( "Australia/Melbourne" );
DateTime theFirst = new DateTime( 2014, DateTimeConstants.OCTOBER, 1, 0, 0, 0, timeZone ).withTimeAtStartOfDay();
DateTime nextMonth = theFirst.plusMonths( 1 ).withTimeAtStartOfDay();
int days = Days.daysBetween( theFirst, nextMonth ).getDays();
Or if you don't care about time-of-day, use the LocalDate class.
java.time
Java 8 and later comes with a new java.time framework to supplant the old java.util.Date/.Calendar classes. Inspired by Joda-Time, defined by JSR 310, and extended by the ThreeTen-Extra project.
Example code using Java 8. Note that the enum ChronoUnit returns a 64-bit long rather than int.
LocalDate firstOfOctober = LocalDate.of( 2010 , java.time.Month.OCTOBER , 1 );
LocalDate nextMonth = firstOfOctober.plusMonths( 1 );
long daysInMonth = ChronoUnit.DAYS.between( firstOfOctober , nextMonth );
The code you put in your post is calculating the time between September 1 and October 1, not October 1 and November 1. The output is correct for the code you posted.