Number of days between two joda dates including holidays [duplicate] - java

This question already has answers here:
Joda Time: How to get dates of weekdays on some date interval?
(3 answers)
Closed 4 years ago.
I want to get the difference between two joda dates objects. I know how to calculate the difference between two dates using the below code:
Days.daysBetween(startDate, endDate)
I need to calculate the difference in business days where I want to exclude weekends and holidays. Is there is way for the same?

Consider these for the number of weekdays
Joda Time: How to get dates of weekdays on some date interval?
Calculate number of weekdays between two dates in Java
As to holidays these are specific to your locale. A Set populated by a config file containing dates should suffice.

Related

Java getting number of days between two dates [duplicate]

This question already has answers here:
Java, Calculate the number of days between two dates [duplicate]
(10 answers)
Closed 1 year ago.
I am trying to calculate the number of days between two dates.
Even though I found many similar questions, I just cannot come up with a solution.
Date lastpickup = (Date) section_userdata.get("lastpickup");
Date today = new Date();
Instant instant_lastpickup = lastpickup.toInstant().truncatedTo(ChronoUnit.DAYS);
Instant instant_today = today.toInstant().truncatedTo(ChronoUnit.DAYS);
This is my code at the moment.
A date is read from a config and should be compared to the actual date.
With the code I have I am able to determine whether the date is the same or not, but I want to know which amount of days (ideally as an Integer) is between those two.
I want to look at the calendar days, not 24h rhythm.
Well, it was way easier than I thought.
Here's my solution
long daysCount = ChronoUnit.DAYS.between(instant_lastpickup, instant_today);

how to add number of days into a given date in the format mm/dd/yyyy? [duplicate]

This question already has answers here:
How can I increment a date by one day in Java?
(32 answers)
Closed 6 years ago.
How to add number of days into a given date in the format mm/dd/yyyy .
If my date is 9/12/2007, I want to add 30 days into the date and the result should be 10/12/2007.
I have many frequencies like Weekly, monthly, Every 2 weeks, Twice a month, Every 4 weeks, Once in 2 months, Every 3 months, Every 6 months, Every 3 months,
Annually, etc.
If we select the different frequencies from the list, the result should vary based on the frequency. Can anyone help me on this ?
Convert your date to a LocalDate, add the required values to it and then convert it back to the format you need it.
For example adding 30 days would look like this:
LocalDate d = LocalDate.of(2007,9,12).plus(30, ChronoUnit.DAYS)
And if you look at ChronoUnit you can see there are some units defined like weeks, days, months and so on...
String dt = "9/12/2007"; // Start date
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
Calendar c = Calendar.getInstance();
c.setTime(sdf.parse(dt));
c.add(Calendar.DATE, 1);

Set a date from the subtraction of the same - Java [duplicate]

This question already has answers here:
Java - Subtract Days from date [duplicate]
(6 answers)
Closed 8 years ago.
I'm doing a calendar / organizer in java
This calendar / organizer features as inputs
Event setup time
Date / Time of event begins
Date / Time Event finish
I can not mark two events in the same period.
Solved this problem by consulting this link How can I determine if a date is between two dates in Java?
My doubts on how to determine date with setup time and date / time of the event beginning.
What I need is a date start (12/12/12 00:00:00) subtracting time setup (00/00/01 00:00:00) have this (12/12/11 00:00:00)
**sorry my bad english
You can use java.util.Date.getTime() to find a long representation of date and then do a simple comparison or arithematic.

Java - Calculate Number of Weeks Between 2 Dates [duplicate]

This question already has answers here:
Get the number of weeks between two Dates.
(19 answers)
Closed 9 years ago.
Is it possible to determine the number of weeks between 2 dates in Java/JSP? For example if date one is 2013-10-29 and date two is 2013-11-12, I would like the number of weeks to be output.
Could somebody pleas help? :-)
Joda can help you, but I'm never able to use it because of its license.
If like me, Joda is not appropriate for you, you can solve this problem as follows:
initialize endDate object
initialize startDate object
initialize weeksBetween as
milliseconds between end&start/milliseconds per day, divided by seven (integer floor, ceiling or round this).
//may need to normalize dates and set them to be both midnight or noon or some common time
output weeksBetween
You can get the milliseconds between them by converting the calendars to Date (Calendar has such a method to do this).
I lifted this from: How to calculate the total hour worked between two dates?
You can use the Joda Time library :
Object Weeks, method weeksBetween :

Difference between 2 dates Java [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Difference in days between two dates in Java?
I need to find difference between 2 dates. First one is in string format,
s1=2012-10-01T15:33:34.652905Z
I need to convert s1 into date d1 and then find the difference in integer between today's date d2 and d1.
How do I do this?
To convert your string to a date, you can use SimpleDateFormat.parse("yyyy-MM-dd'T'HH:mm:ss.SSSZ").
Given two Date objects, you can get whatever differences (days, seconds...) you need with JODA Time as advised in SO entry : Difference in days between two dates in Java?

Categories