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.
Related
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);
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.
This question already has an answer here:
How to change the base date for parsing two letter years with Java 8 DateTimeFormatter?
(1 answer)
Closed 7 years ago.
The java.time framework built into Java 8 and later parses two-digit year strings as being in the 2000s. So 90 becomes 2090.
From the java.time.DateTimeFormatter class documentation:
If the count of letters is two… will parse using the base value of 2000, resulting in a year within the range 2000 to 2099 inclusive.
I have data where values such as 90 meant 1990.
How can I change the pivot year for parsing two-digit year strings to 1900 rather than 2000?
Even better, how can I set any arbitrary pivot year, as I know some data sources have rules for partial century. For example, “If under 80, assume 2000s, otherwise assume 1900s”.
Yes, I know, using two-digit year values is dopey. But the data was not under my control.
Just use the API to get a value in the 2000-2099 range and use some conditionals to do the rest of the work. Don't worry about trying to change the API itself.
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 :
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?