Java 8 Date time for calculating age in decimals [duplicate] - java

This question already has answers here:
Java Time period in decimal number of years
(3 answers)
Closed 5 years ago.
I am new in using Java 8 date time API and was wondering how can i able to calculate the age in decimals which returns the double value like 30.5 which means 30 years and 6 months? For example the below sample code gets me the output as 30.0 but not 30.5 which probably am trying for.
LocalDate startDate = LocalDate.of(1984, Month.AUGUST, 10);
LocalDate endDate = LocalDate.of(2015, Month.JANUARY, 10);
double numberOfYears = ChronoUnit.YEARS.between(startDate, endDate);
System.out.println(numberOfYears); //Getting output as 30.0 but not 30.5

The JavaDoc for ChronoUnit's between method clearly states:
The calculation returns a whole number, representing the number of complete units between the two temporals.
So you can't get 30.5 by just querying for years between. It only will give you the whole number -- 30.
But what you can do is get the months and divide by 12. For greater precision, you could instead use days, or even smaller units.
LocalDate startDate = LocalDate.of(1984, Month.AUGUST, 10);
LocalDate endDate = LocalDate.of(2015, Month.JANUARY, 10);
double numberOfMonths = ChronoUnit.MONTHS.between(startDate, endDate) / 12.0;
System.out.println(numberOfMonths); // prints 30.416666666666668
(If your endDate was February 10, 2015, then it prints 30.5....)

Related

Subtract fiscal year quarters from current date using Java LocalDate

By using Java LocalDate how can I subtract eight quarters from the current quarter?
For example:
2019 Q3 - (8 x quarter) = 2017 Q4
I've tried using
LocalDate.now(ZoneId.of("Europe/London"));
now.minus(1, IsoFields.QUARTER_OF_YEAR);
but I am getting a negative value and I don't know how to handle this.
You are using QUARTER_OF_YEAR which is of type TimeField. You should use IsoFields.QUARTER_YEARS which is of type TemporalUnit:
Unit that represents the concept of a quarter-year. For the ISO calendar system, it is equal to 3 months.
LocalDate now = LocalDate.now(ZoneId.of("Europe/London"));
LocalDate ago = now.minus(3, IsoFields.QUARTER_YEARS);
System.out.println(ago);

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

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();

change date 5 digits int to date string in java [duplicate]

This question already has answers here:
Converting number representing a date in Excel to a Java Date object
(6 answers)
Closed 4 years ago.
Excel converts dates into 5 digits int so I'm asking for a way to convert this 5 digits int ex. 43277 to date String dd-MMM-yy and how to change it "date String" back to 5 digits int.
I'm trying to make the app reads and writes from an google spreadsheet.
It's true that a similar question asked here "Converting Number representation of Date in excel to Date in java" but the code provided did not work or me as it showed cannot resolve symbol 'DateUtil' and the i couldnt understand the second answer.
Also I am asking how to convert it back and forth not from int to String only.
Excel’s serialized dates are the number of days since 1/1/1900. In order to figure out the date again, we have to add the serial number worth of days.
Java 8 version
/*
1900-1-0 0
1900-1-1 1
1900-1-2 2
1900-1-3 3
*/
int days = 43323;
LocalDate start = LocalDate.of(1900, 1, 1);
LocalDate today = LocalDate.of(2018, 8, 11);
// days to date
LocalDate date = start.plusDays(days).minusDays(2);
System.out.println(date);
// date to days
long days1 = ChronoUnit.DAYS.between(start, today) + 2;
System.out.println(days1);

How to convert given days into year, months, days in java [duplicate]

This question already has answers here:
Adding days to a date in Java [duplicate]
(6 answers)
Closed 6 years ago.
How do I convert given days into Calendar format in java.
Example Initial date is 01-01-2015. Given days are 125 days. This should be converted as 0 years, 4 months, 5 days and added to initial date which would become 06-05-2015.
You can use Period class from java8's new java.time API to convert the difference between two dates into years, months and days:
LocalDate initial = LocalDate.of(2015, 1, 1);
LocalDate end = initial.plusDays(125);
Period p = Period.between(initial, end);
int years = p.getYears(); // 0
int months = p.getMonths(); // 4
int days = p.getDays(); // 5

how to find the total number of months between the two dates including extra days?

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));

Categories

Resources