Time difference while taking into a/c - java

If we have 2 dates
Previous Date : Wed Jun 02 17:30:00 CDT 2010
Next Date : Sun Feb 13 22:00:00 CST 2011
and need to find difference in mins. between these 2 dates
Is there a way to accurately get it?

Yes, you can get an accurate difference of those times:
Parse each one with SimpleDateFormat to get a Date.
Get the time in milliseconds since the Epoch from each.
Subtract the two times and divide by 60000 for minutes.
Here's the code:
SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy");
Date prevDate = sdf.parse("Wed Jun 02 17:30:00 CDT 2010");
Date nextDate = sdf.parse("Sun Feb 13 22:00:00 CST 2011");
long diffTime = nextDate.getTime() - prevDate.getTime();
System.out.println(diffTime / 60000 + " minutes");

date1.getTime() - date2.getTime() will give you the difference in milliseconds. You can then divide it by 60000 to get the difference in minutes.

Use TimeUnit class for convertion.
// specify the input format
SimpleDateFormat dateFormat = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy");
String s1 = "Wed Jun 02 17:30:00 CDT 2010";
String s2 = "Sun Feb 13 22:00:00 CST 2011";
// parse to Date object
Date d1 = dateFormat.parse(s1);
Date d2 = dateFormat.parse(s2);
// get time in milliseconds
long l1 = d1.getTime();
long l2 = d2.getTime();
// absolute difference
long diff = Math.abs(l1 - l2);
// convert milliseconds to minute
long min = TimeUnit.MILLISECONDS.toMinutes(diff);
System.out.println(min);

With Joda Time
DurationFormatUtils.formatDuration(date2.getTime() - date1.getTime(), "m");

Related

SimpleDateFormat returning the wrong date in Java

I'm having an issue with SimpleDateFormat in Java. My code is returning the wrong date. Help please.
String date_str = "Tue Mar 08 09:44:55 EST 2022";
Date date = new SimpleDateFormat("EEE MMM D HH:mm:ss z yyyy").parse(date_str);
// Output: Sat Jan 08 14:44:55 GMT 2022
d, not D for the day of the month. D is the day in the year, so 8 is the 8 of January.
String date_str = "Tue Mar 08 09:44:55 EST 2022";
Date date = new SimpleDateFormat("EEE MMM d HH:mm:ss z yyyy").parse(date_str);
// ^---Here
// Output: Tue Mar 08 15:44:55 CET 2022
Check the full list of patterns here.

Missing Month and Day In Java SimpleDateFormat parse

Using SimpleDateFormat:
DateFormat df = new SimpleDateFormat("dd-MMMM-YYYY kk:mm:ss.SSS");
Date extractedDate = df.parse(possibleDate);
Input given:
11-May-2017 21:45:33.614
Output data object:
Sun Jan 01 21:45:33 MST 2017
I have tried lots of iterations but it won't pull the month and day.
Use dd-MMM-yyyy kk:mm:ss.SSS as pattern
Example :
DateFormat df = new SimpleDateFormat("dd-MMM-yyyy kk:mm:ss.SSS");
Date extractedDate = df.parse("11-May-2017 21:45:33.614");
System.out.println(extractedDate);
Output :
Thu May 11 21:45:33 BDT 2017
Another thing If you use kk for hour, hour should be represent between 1 to 24. If the hour between 0 to 23 use HH instead of kk

Difference in hours of two Calendar objects

I have two Calendar objects, and I want to check what is the difference between them, in hours.
Here is the first Calendar
Calendar c1 = Calendar.getInstance();
And the second Calendar
Calendar c2 = Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy", Locale.ENGLISH);
c2.setTime(sdf.parse("Sun Feb 22 20:00:00 CET 2015"));
Now lets say that c1.getTime() is: Fri Feb 20 20:00:00 CET 2015 and c2.getTime() is Sun Feb 22 20:00:00 CET 2015.
So is there any code that would return the difference between first and second Calendar in hours? In my case it should return 48.
You can try the following:
long seconds = (c2.getTimeInMillis() - c1.getTimeInMillis()) / 1000;
int hours = (int) (seconds / 3600);
Or using the Joda-Time API's Period class, you can use the constructor public Period(long startInstant, long endInstant) and retrieve the hours field:
Period period = new Period(c1.getTimeInMillis(), c2.getTimeInMillis());
int hours = period.getHours();
In Java 8 you could do
long hours = ChronoUnit.HOURS.between(c1.toInstant(), c2.toInstant());

Using Date in Java

I have various Date instances in my Java program. Working with the is a pain but it is required.
Date today = new Date(); // Wed Dec 10 14:10:29 EST 2014
Date a = new GregorianCalendar(2014, 11, 10).getTime();
Date b = new GregorianCalendar(2015, 01, 10).getTime();
Date c = new GregorianCalendar(2015, 02, 10).getTime();
Date d = new GregorianCalendar(2015, 03, 10).getTime(); //Fri April 10 00:00:00 EDT 2015
Date e = new GregorianCalendar(2015, 11 ,10).getTime();
I need to figure out how to shave off the time (14:10:29) from each as well as convert them to GMT time.
I know today.getTime(); will the number of milliseconds since January 1, 1970, 00:00:00 GMT, but I'm not sure how to represent that with out the times.
This would be for easier comparisons between dates. Thanks.
try using date formater
SimpleDateFormat df= new SimpleDateFormat("HH:MM:ss");
df.format(date)
All you have to do is modulus the long value by the long value of 1 day, and subtract that off the long value of your date.
If you have Wed Dec 10 14:10:29 EST 2014, then if you do
Date today = new Date(); // Wed Dec 10 14:10:29 EST 2014
long timeDiff = today.getTime() % 24 * 60 * 60 * 1000;
today = new Date(today.getTime - timeDiff);
The new today object will be created with the time of the day removed.
To convert them to GMT you can similarly create new dates for the longs. This is obviously "Date" way to do it. The best way would be to use Calendar.

Difference of two dates in minutes

I have 2 dates in String with format (MMM dd, yyyy hh:mm:ss a).
How to convert two Strings into date and find the difference in minutes ?
DateFormat df = new SimpleDateFormat("MMM dd, yyyy hh:mm:ss a", Locale.ENGLISH);
Date start = df.parse(startstring);
Date end = df.parse(endstring);
After I want to take the difference in minutes and I am using this code:
long result = ((end.getTime()/60000) - (start.getTime()/60000));
But the result is 0. How can I solve this problem ?
My Strings are :
start: Fri Mar 07 23:45:43 GMT+04:00 2014
end: Fri Mar 07 23:46:01 GMT+04:00 2014
You could use this approach (first calculate the minutes since epoch, then subtract them) -
private static long getTimeInMinutesFromEpoch(Date d) {
if (d == null) {
return 0;
}
return d.getTime() / (60 * 1000);
}
public static long getMinuteDifference(Date a, Date b) {
return Math.abs(getTimeInMinutesFromEpoch(b)
- getTimeInMinutesFromEpoch(a));
}
public static void main(String[] args) throws ParseException {
String startstring = "Mar 07, 2014 23:45:43 PM";
String endstring = "Mar 07, 2014 23:46:01 PM";
DateFormat df = new SimpleDateFormat("MMM dd, yyyy hh:mm:ss a",
Locale.ENGLISH);
Date start = df.parse(endstring);
Date end = df.parse(startstring);
System.out.println(getMinuteDifference(start, end));
}
Output is
1
From the looks of it you're creating the start date immediately before the end date (unless there is non-included relevant information).
Date start = df.parse(startstring);
Date end = df.parse(endstring);
These are going to be created in exactly the same minute and therefore give you 0 when you try to find the difference in minutes.
EDIT
Your times:
start: Fri Mar 07 23:45:43 GMT+04:00 2014
end: Fri Mar 07 23:46:01 GMT+04:00 2014
are 18 seconds apart. You're going to get 0 for the difference in minutes.
You can make Calendar object instead of Date and then you can get the minutes using Calendar.get(Calendar.MINUTE). Note that by using this logic, the difference between 22:45:43 GMT+04:00 2014 and 23:45:43 GMT+04:00 2014 will be zero minutes.

Categories