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.
Related
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
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:
convert joda-time seconds to formatted time string
(3 answers)
Closed 7 years ago.
I am using JodaTime for time management in my app.
I want to get the hour and minute from a DateTime with 2 digits.
Example:
if the hour is 8 and the minutes 5
I want to get 08:05
DateTime dt = DateTime.now();
DateTimeFormatter dtf = DateTimeFormat.forPatter("H:m");
dt.toString(dtf);
I know that I can achieve this with manual string concat, but I don't want it.
have you tried "HH:mm" for pattern? it will print Hours and minutes using 2 digits for each (in your case 08:05)
This question already has answers here:
Calculating the difference between two Java date instances
(45 answers)
Closed 8 years ago.
I am trying to remove the entries in my database that have Date (java.util.Date) older than 10 days of the current date. Is there any way to compare just the "day" value inside the Date. Not just comparing which Date value is greater, but actually making sure there is X day in between the two Date values.
Say you have your two Date values as time1 and time2.
int daysApart = (int)((time2.getTime() - time1.getTime()) / (1000*60*60*24l));
if (abs(daysApart) >= 10)
System.out.println("10+ days apart.");
else
System.out.println("Less than 10 days apart.");
Just create a new Date object for current date + 10 days and compare to that.
i.e.
new Date(System.currentTimeMillis()+10L*24*60*60*1000);
As the comments say though the Date objects are pretty much obsolete now, you should look to using one of the more current time approaches.
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();