Java getting number of days between two dates [duplicate] - java

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

Related

Java - date time is always -1h from the local time [duplicate]

This question already has answers here:
Converting date to timestamp with timezone in Java
(3 answers)
Datetime behind an hour after insertion. Daylight savings
(2 answers)
Closed 2 years ago.
I have a 'simple' problem with Java and dates. I thought it was simple but I don't understand how to solve it ! I have try a lot of things. Here is the problem.
I save a date in my postgres sql database like this.
user.setCreationDate(Timestamp.valueOf((LocalDateTime.now())));
this date is store with one hour less than the real hour here.
When I want to use it, I would like to get in database and add the UTC +1 of my local zone.
I try this but always get the date -1h...
OffsetDateTime myNewDate = bddDate.toInstant().atZone(ZoneId.systemDefault()).toOffsetDateTime()
Do you have an idea of how just get the local timezone exact hour ?
Thanks for your help !

getting and setting dates with Java [duplicate]

This question already has answers here:
How to subtract X days from a date using Java calendar?
(11 answers)
How to add 7 days to current date while not going over available days of a month?
(8 answers)
Closed 8 years ago.
First off thank you for taking the time to scroll through this.
I am basically a greenhorn when it comes to Java but I am working on a program that asks the user some basic info then sets a start date and then schedules service dates at bi weekly intervals between March and October from their start date.
For the start date variable, I simply set it up as:
STARTDATE = getDate();
to give the current date when the user signs up.
I have been sifting through my searches for days but I cannot figure out how to increment the service dates by 14 days to save my life.
I tried using SERVICEDATE = STARTDATE + (0, 14, 0);
but I cant make sense of whats really happening here. Any ideas?
Use the common-lang library. It has a DateUtils class which can be used for this.
Use it like this:
SERVICEDATE = DateUtils.addDays(STARTDATE, 14);
Maybe have a look at this: Java getDate date-x days
They are subtracting days but I think it can be turned into adding days too.
I suggest you use Joda Library
Date STARTDATE = getDate();
DateTime stDate = new DateTime(STARTDATE);
//Date after 14 days
DateTime rsDate = stDate.plusDays(14);

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?

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

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.

Categories