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

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 :

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

Algorithm to find the days between two dates

I was working with a school question to figure out the algorithm to find the days between two given dates which would then be implemented in Java.
The algorithm of interest was found here:
http://www.sunshine2k.de/articles/coding/datediffindays/calcdiffofdatesindates.html
(Point 4)
It was one of the more efficient algorithms because it would have the least conditions to consider during the implementation. I understand how it works in this context, but I couldn't quite wrap my mind around the use of an origin/reference point anywhere else because it seems that a simple subtraction would get most jobs done.
Eg. To find the difference between 9 and 5,
I could just do 9-5 instead of
ref = 1
difference = (9-ref) - (5-ref)
Question: Why does using this reference/origin point work in this situation? What other situations can I consider using this reference/origin point to solve problems?
First rule of Software Engineering is "Don't reinvent the wheel".
Getting the days between two dates in Java 8 and later is trivial, there's no need to code your own algorithm:
LocalDate d2 = LocalDate.now();
LocalDate d1 = LocalDate.of(1950, Month.JANUARY, 1);
long days = d1.until(d2,ChronoUnit.DAYS);
Or even better
long days = ChronoUnit.DAYS.between(d1, d2);
To get difference between 9-th and 5-th day of the same month, you can just subtract 9-5 (if there was no calendar revolution in that period :)). But for different months and different years you must account for varying number of days in the month and varying number of days in the year.
The simplest way to account for these factors is to get absolute number of days from some origin day - so-named Julian day with our era beginning as origin
Actually, 9-5 uses a reference of 0 in the same way as (9-0) - (5-0).
Unlike numbers, where 0 is the reference point, for dates, the reference point is not obvious. Should it be 0 AD, 1900 AD, or something else? With the different number of days in months, leap years etc, the day scale is not uniform. So, it is useful to find the "distance" of a date from a common date, for e.g. 1970-01-01.

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

age calculation in Java [duplicate]

This question already has answers here:
How do I calculate someone's age in Java?
(28 answers)
Closed 10 years ago.
i'm trying to create a small command line java app that will allow the input of a date and compare it to the current date and return an integer that represents the number of years between the entered date and the current date. this is to calculate the age of a living person in years. So far, I've found that I need to use the Calendar object, but I can't figure out how to subtract the entered year from the current year and return an integer. Can you help? Just hit the high points on the mechanics of comparing the dates. Thanks.
Once you have a Calendar object initialized with the correct date, you can call the get(Calendar.YEAR) value to get an integer representing the year. Do this for both dates and subtract.
You'll also want to get the month and date and compare, because if the end date has a lower month/date than the start, then you'll want to subtract 1 from the earlier result (the year has not yet been completed).
Typically for birthdays you don't care about the time component (hour/minute/second), so some libraries may give you a different result (in an extremely small number of cases) -- it's justifiable to roll your own for this case.

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