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.
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:
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:
How in Java find dates for previous 2 mondays?
(3 answers)
Closed 6 years ago.
I got a date YYYY-MM-DD how can I get the Monday date of that date whatever the given date is?
Edited: got the answer from How in Java find dates for previous 2 mondays?.
LocalDate monday =
localDate.with(TemporalAdjusters.previousOrSame(DayOfWeek.MONDAY);
This question already has answers here:
How do I calculate someone's age in Java?
(28 answers)
Closed 6 years ago.
I have 2 Calendar objects for the current date and birthdate.
I need to subtract birthdate from the current date and return the difference in years (a truncated Int is all I need).
What's the simplest way of doing this?
You can get EPOCH time (miliseconds since 01.01.1900), subtract both values and divide it by 1000(miliseconds)/60(seconds)/60(minutes)/24(hours)/365(days).
This would give you approximate result (in years) because of ignoring leap years.
To get epoch time, use Date#getTime();
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.