This question already has answers here:
How to calculate the number of days in a period?
(3 answers)
Closed 3 years ago.
I have Period object which comes from api. I have to calculate total days it contains. I found many answers how to get days between two dates, but no one answers how I can get total days exactly from Period object.
E.g:
LocalDate start = LocalDate.now();
LocalDate end = LocalDate.now().plusYears(1);
Period period = Period.between(start, end);
I have only the last object with name period and i have to get 365 days from it.
getDays() returns only days count within one month. And I don't have two dates objects. Only period.
You might wanna read up on the Period Java API Documentation.
Period represents the time duration in this format, "2 years, 3 months and 4 days"
For your case above, its exactly 1 year, 0 Months and 0 days.
When you do a get Days, it will show you 0 days.
Try with an end date of LocalDate end = LocalDate.now().minusDays(398);
The getYears(), getMonths(), getDays() each returns its own value. ;)
Since you already have a Period object, you can do a function to get the number of days from a Period Object using the getYears, getMonths and getDays() methods then sum them together.
The Duration object might help with that.
did you tried import java.time package and use Period class? if you didn't, here's a little help for it https://docs.oracle.com/javase/8/docs/api/java/time/Period.html
Related
This question already has answers here:
Calculate number of weekdays between two dates in Java
(20 answers)
How to get every day except weekend or Saturday or Sunday between two dates in java?
(4 answers)
Closed 4 years ago.
if user selected the start and end date of leave of someone then I must to get the sum of days of weekend : sum of "Sturday" and "Sunday" days
You can use the DateTimeFormatter class docs here
When you're using the date format object to leave out the weekend you can check for weekends from the attribute "E" (Day number of week) which is returned as an Integer. (please see the link posted). It unclear what your goal here with the dates, But it seems you are having problems knowing when a certain day is a weekend or not so I hope this helps.
This question already has answers here:
Number of days between two dates in Joda-Time
(9 answers)
Closed 5 years ago.
Upon testing JODA time to get number of days that has been selected, it is excluding the start day.
Date from = day1.getTime();
Date to = day2.getTime();
int daysBetween = Days.daysBetween(new DateTime(from), new DateTime(to)).getDays();
So if I select, December 14 - 16, it is only showing "2" as result instead of 3.
Is it safe to just "+1" the result or is there a right way to do this in JODA time?
Actually Days.daysBetween(-,-) method just subtracts the start day from the end day. If you want to get the total no of days including the start day then you must have to minus 1 from the start date.
This question already has answers here:
Do we have a TimeSpan sort of class in Java
(4 answers)
Closed 6 years ago.
I am trying to convert the following code which is in c# to java. And I am facing difficulty in converting it. Please can anyone suggest me a simple way to do it in Java.
DateTime StartDate = new DateTime(PWUpdatedOn.Year, 01, 01);
TimeSpan ts = new TimeSpan(PWUpdatedOn.Ticks - StartDate.Ticks);
//Response.Write(ts.Days+1);
days = ts.Days + 1;
lngN = 0;
PWUpdatedOn.Year = 2016 // current year
As Jigar Joshi answered in an other Question.
Interval from JodaTime will do..
A time interval represents a period of time between two instants. Intervals >are inclusive of the start instant and exclusive of the end. The end instant is always greater than or equal to the start instant.
Intervals have a fixed millisecond duration. This is the difference between the start and end instants.
The duration is represented separately by ReadableDuration. As a result, intervals are not comparable. To compare the length of two intervals, you should compare their durations.
An interval can also be converted to a ReadablePeriod. This represents the difference between the start and end points in terms of fields such as years and days.
Interval is thread-safe and immutable.
I am not sure what Ticks are in C#. But it would be something like:
LocalDateTime startDate = LocalDateTime.of(PWUpdatedOn.getYear(), 1, 1);
Period ts = Period.between(PWUpdatedOn, startDate.toLocalDate());
days = ts.getDays() + 1;
Note that Period.between() requires two LocalDate instances. If PWUpdateOn is a LocalDateTime instance, it needs to be converted with the method toLocalDate().
Some potentially relevant remarks: for zoned datetimes, use ZonedDateTime rather than LocalDateTimel; all time and period objects are immutable in Java.
This question already has answers here:
Android how to get time difference between two time zones in android?
(4 answers)
Rails: Difference between timezones in hours
(1 answer)
Closed 8 years ago.
I'd like to get the number of hours between 2 different time-zones. I'm currently using Joda time but it looks like it's taking daylight savings into account because it's off by an hour. The only correct one seems to be my current timezone where it returns 0 hours but if I use London for instance which for me is 5 hr difference it returns 4.
Current code:
DateTime endTime = new DateTime(date);
DateTime startTime = new DateTime();
Period period = new Period(startTime, endTime);
int hours = period.getHours();
I have this code:
return DurationFormatUtils.formatDuration(2034430000000L, yyyy-MM-dd_HH:mm:ss.SSS)
the result is: 0000-00-23546_15:26:40.000
why the year and days are 0 and so many days?
Because the documentation says so: DurationFormatUtils.formatDuration().
From the docs:
This method formats durations using the days and lower fields of the format pattern. Months and larger are not used.
The reason is that when you are talking about durations, how long is a month? How long is a year? A month is a variable number of days, as is a year. Now if these durations are anchored, such a thing can make sense; in these cases these are what Joda-Time would call periods.
But in general, a duration is just a number of milliseconds, so months and years really do not make sense.