Difference between 2 dates Java [duplicate] - java

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?

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

Filtering dates by time of day (beginner) [duplicate]

This question already has answers here:
How to know if now time is between two hours?
(8 answers)
Closed 8 years ago.
I need to write a method which returns true if a given date is between the hours of 8am and 5pm, and false otherwise. How would I implement this? Thanks a lot!
public static boolean isInWorkingHours(String date){
}
Parse the date parameter with the SimpleDateFormat, then set the resulting Date object as time property of a GregorianCalendar instance. Use calendar's get() method to retrieve the hour of day.
http://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html

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 :

How to Compare Time in Java? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How to calculate time difference in java?
I have two Strings "10:00:00" and "14:00:00". I have converted them to Date Format. Now i want two compare the both time.Can anyone suggest me what to do....
use compareTo().
date1.compareTo(date2);
from java docs:
Returns:
the value 0 if the argument Date is equal to this Date; a value less than 0 if this Date is before the Date argument; and a value
greater than 0 if this Date is after the Date argument.
other examples
Check Date#equals, Date#after, Date#before
I preferrably use Calendar class for date related task.. It is quite to use them in some context..
You can find more helpful examples in this link
Before you do the comparison, as the timestamps are still in the String format, you need to convert them to java.util.Date and the simplest way is to assign an arbitrary date part to both time strings (e.g. concatenate both time strings to "1/1/1970 "), then convert them into Date using SimpleDateFormat, then get time in milliseconds and do the comparison.

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