Span in days between two dates misunderstanding - java

I have a misundertood managing dates in Java when I want to calculate the span in number of days between two dates.
Say we have two different dates:
Date 1: 1986-01-24
Date 2: 2017-04-20
Case 1: I have this snippet of code using Dates:
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
Date dt1 = format.parse("1986-01-24");
Date dt2 = format.parse("2017-04-20");
int intSpanInDays= (int) ((dt2.getTime() - dt1.getTime()) / (1000 * 60 * 60 * 24));
System.out.println("Days between: " + intSpanInDays);
Output 1:
Days between: 11408
Case 2: Snippet of code using Calendar:
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
GregorianCalendar cal1 = new GregorianCalendar();
cal1.setTime(format.parse("1986-01-24"));
cal1.set(Calendar.HOUR, 0);
cal1.set(Calendar.MINUTE, 0);
cal1.set(Calendar.SECOND, 0);
GregorianCalendar cal2 = new GregorianCalendar();
cal2.setTime(format.parse("2017-04-20"));
cal2.set(Calendar.HOUR, 0);
cal2.set(Calendar.MINUTE, 0);
cal2.set(Calendar.SECOND, 0);
long spanInMillis = cal2.getTimeInMillis() - cal1.getTimeInMillis();
GregorianCalendar cal3 = new GregorianCalendar();
cal3.setTimeInMillis(spanInMillis);
long millisInADay = 1000 * 60 * 60 * 24;
System.out.println("Days between: " + (cal3.getTimeInMillis() / millisInADay));
Output 2:
Days between: 11408
Case 3: Example using a spreadsheet in Excel:
When I use MS Excel to get this span just introducing the given dates and simply substracting, the output is this:
QUESTION
Why is Java calculation code of date missing one day? What is missing or wrong in either case 1 and 2 that does not match the result in case 3?

The spreadsheet is taking Daylight Savings into account, and your calculations are naively truncating, and given that there's one more 23-hour day in the interval than 25-hour days, the 23-hour remainder is truncated, yielding a result one day less than the correct answer.

JDK 8 largely simplifies these calculations with its new date time API. The same can be done accurately and simply using the below code :
LocalDate date1 = LocalDate.of(1986, 01, 24);
LocalDate date2 = LocalDate.of(2017, 04, 20);
System.out.println(date1.until(date2, ChronoUnit.DAYS));
This automatically takes care of any/all the DST changes, leap years etc. which is mostly missed when trying to do the calculations manually.

Related

how to calculate and split a float value into two separate integer values ?? e.g 18.5f hours equals int h=18 int m=30

I am creating a Workday calendar program that calculates a 'start' and 'end' date
the results should output:
"Starting date: 24-05-2004 07:03 with addition of 8.276628 working days is end date: 04-06-2004 10:12"
or
24-05-2004 18:03 with the addition of -6.7470217 working days is 13-05-2004 10:02
The mathematical solution is to multiply hours pr day with incrementInWokringdays
like 8.0f hours a day * 2.5f days = 18.4f hours and then the result of this should be added to the Date calendar this way
-date.add( Calendar.Hours_of_Day, 18.0f ) //but from float converted to integers
-date.add( Calendar.Minutes_of_Day, 0.4f )//
how do i split the value 18.4f hours in to
'int hours = 18;'
and
'int minutes = 40;'
????
public Date getWorkdayIncrement(Calendar date, float incrementInWorkdays) {
SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd HH:mm");
System.out.println("start day: " + f.format(date.getTime()));
// so if chosen incrementDays is 2.3 days and workinghours pr day is 7.5h you multiply them together to get total working hours
float totalHoursWorkedPrDay = getWorkdayStartAndStop() * incrementInWorkdays;
// needed to convert hours and minutes to integer values in order to increment calendar
int hoursToIncrement = (int) totalHoursWorkedPrDay; //gets only hours
// get the last to decimals 0.25 representing the minutes which means 25 percent of 60min
float lastTwoDecimalsOfTotalWorkingHours = ((totalHoursWorkedPrDay - (float) hoursToIncrement) * 100);
//calculate percent of minutes and convert to integer (25 / 100 * 60) = 15min
int minutesToIncrement = (int) ((lastTwoDecimalsOfTotalWorkingHours / 100) *60);
System.out.println("Hours to increment: " + hoursToIncrement);
System.out.println("Minutes to increment: " + minutesToIncrement);
//increment calendar
date.add(Calendar.HOUR_OF_DAY, hoursToIncrement);
date.add(Calendar.MINUTE, minutesToIncrement);
Date endDate = date.getTime();
System.out.println("End date excluding holidays: " + f.format(endDate));
}
I didn’t follow everything you were doing in the question. So for now I focus on calculating the working hours of a working day given start and end time. I am assuming that you get start and end as GregorianCalendar objects from some legacy API that you cannot afford to upgrade to java.time just now.
I recommend that you use java.time, the modern Java date and time API, for your date and time work. Even if you are getting your data as objects of the old-fashioned date-time types. java.time is so much nicer to work with.
static float getHoursBetween(Calendar start, Calendar end) {
ZonedDateTime startZdt = ((GregorianCalendar) start).toZonedDateTime();
ZonedDateTime endZdt = ((GregorianCalendar) end).toZonedDateTime();
long wholeDays = ChronoUnit.DAYS.between(startZdt, endZdt);
startZdt = startZdt.plusDays(wholeDays);
Duration workDay = Duration.between(startZdt, endZdt);
return (float) workDay.toMinutes() / (float) Duration.ofHours(1).toMinutes();
}
The method ignores the date difference between start and end by adding the number of full days to the start time. This means that the result we get is based only on the hours and minutes. To try the method out, let’s construct a couple of GregorianCalendar objects (of course I am using java.time for that too):
ZoneId zone = ZoneId.of("America/Araguaina");
ZonedDateTime workDayStart = ZonedDateTime.of(2020, 6, 14, 7, 30, 0, 0, zone);
Calendar workDayStartCal = GregorianCalendar.from(workDayStart);
ZonedDateTime workDayEnd = ZonedDateTime.of(2020, 6, 15, 15, 15, 0, 0, zone);
Calendar workDayEndCal = GregorianCalendar.from(workDayEnd);
float workingDay = getHoursBetween(workDayStartCal, workDayEndCal);
System.out.println(workingDay);
Output from the example is:
7.75
Edit: for the opposite conversion this works since Java 9:
float hours = 7.75f;
Duration dur = Duration.ofMinutes(Math.round(hours * Duration.ofHours(1).toMinutes()));
System.out.println("Hours: " + dur.toHours());
System.out.println("Minutes: " + dur.toMinutesPart());
Hours: 7
Minutes: 45
Link
Oracle tutorial: Date Time explaining how to use java.time.

Convert Excel timestamp to java.sql.date

I am extracting a timestamp out of Excel (2010):
It is displayed as "10.06.2015 14:24". The "internal representation" of excel is "42165.6". Last one is outputted from Excel.
So, for now, I want to parse this timestamp into a Java program like this:
double input = 42165.6;
// long myLong = ???
SimpleDateFormat sdf = new SimpleDateFormat("dd.MM.yyyy HH:mm");
System.out.println(sdf.format(new java.sql.Date(myLong)));
How can I do this in line 2?!
Many thanks for your help!!!
Kind regards
Excel stores dates as the number of days since January 1900. This makes it awkward to convert into a Java date (milliseconds since 1 Jan 1970). If you cannot export it in a readable format, you'll need to create a Java Calendar, set it to 1 Jan 1900, and add the number of days.
Here it is:
double excelDate = 42165.6;
int days = (int) excelDate; //number of days
int seconds = (int) ((excelDate-days) * 86400); //number of seconds in .6 days
//create calendar set to 01-Jan-1900
Calendar cal = Calendar.getInstance();
cal.set(Calendar.YEAR, 1900);
cal.set(Calendar.MONTH, 0);
cal.set(Calendar.DAY_OF_MONTH, 1);
//Add days and seconds to get required date/time
cal.add(Calendar.DATE, days-1);
cal.add(Calendar.SECOND, seconds);
//cal.getTime() returns a java.util.Date, print it out...
System.out.println(cal.getTime());
NOTE
A java.sql.Date can be created from a java.util.Date as follows:
java.util.Date utilDate = ....
java.sql.Date sqlDate = new java.sql.Date(utilDate.getTime());
The fastest way to get from the excel notation to a java.sql.Date is:
double excelInput = 42165.6;
SimpleDateFormat sdf = new SimpleDateFormat("dd.MM.yyyy HH:mm");
System.out.println("---> "
+ sdf.format(new java.sql.Date(
(long) ((excelInput - 25569) * 86400 * 1000))));
Excel stores a date since 01-01-1900, java.sql.date since 01-01-1970. There are exactly 25569 days difference between both dates. The constructor of java.sql.Date wants the milliseconds (!) since 01-01-1970, so with "* 86400" we get the seconds and then with (* 1000) the milliseconds.
That's it! ;)
Putting 42165.6 in excel and formatting to a date gives the correct date 6/10/15 14:24.
For me the answers by mrbela and NickJ both gave incorrect answers to the question of 42165.6, 06/10/2015 09:23 and 06/12/2015 03:25. This seems to be true of most of the examples I've tried.
The solution that worked for me was to use Apache POI, which is a java API for Microsoft Documents. This question is very similar and had the pointers that led me to poi.
Here is a working code example.
double input = 42165.6;
Date date = DateUtil.getJavaDate(input);
System.out.println(new SimpleDateFormat("dd.MM.yyyy HH:mm").format(javaDate));
which outputs the correct 06/10/2015 14:24

Calculating days between two dates wrong output

There are a lot of replicate questions on this matter, I've looked through most of them and got to make my code, but it didn't work.
SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");
Calendar date1 = Calendar.getInstance();
Calendar date2 = Calendar.getInstance();
String targetDate = "18/05/2015";
date2.setTime(formatter.parse(targetDate));
long diff = date2.getTimeInMillis() - date1.getTimeInMillis();
long daysPassed = TimeUnit.DAYS.convert(diff, TimeUnit.MILLISECONDS);
System.out.println("Today is : "+formatter.format(date1.getTime()));
System.out.println("Target date is : "+targetDate);
System.out.println("Difference of days between them is : "+daysPassed);
With the output being :
//Today is : 17/05/2015
//Target date is : 18/05/2015
//Difference of days between them is : 0
It works but with a day less, I could just add +1, but it gives 0 if is the same day too so there's that...
I should say It's for a uni project and I can't use external libraries or something that hasn't been taught yet.
It also gives an annoying parse exception error that keeps going all over the code
If you output the timeInMillis of date1 and date2, you'll get 2 long values, for example:
1431964800000
1431916207715
and the diff is 48592285
This value can be explained as:
48592285 milliseconds
48592285/1000 = 48592.285 seconds
48592285/1000/60 = 809.87 minutes
48592285/1000/60/60 = 13.50 hours
48592285/1000/60/60/24 = 0.56 days
That's the reason why you got 0 between 2 dates you specified.

How to find number of days between two unix timestamps in java

I am using unix timestamp to store the purchase date in my application.
sample data: 1371463066
I want to do some manipulation based on the difference in number of days and current day timestamp.
for example: If the number of days between the purchase date and current date is 5 days, then send an email regarding feedback again.
how to get the difference in days between two timestamps using java?
I have not tested it but you may try to do something like this:
Date purchasedDate = new Date ();
//multiply the timestampt with 1000 as java expects the time in milliseconds
purchasedDate.setTime((long)purchasedtime*1000);
Date currentDate = new Date ();
currentDate .setTime((long)currentTime*1000);
//To calculate the days difference between two dates
int diffInDays = (int)( (currentDate.getTime() - purchasedDate.getTime())
/ (1000 * 60 * 60 * 24) )
Unix timestamp is the number of seconds since 1.1.1970. If you have 2 unix timestamps then the difference in full days is
int diff = (ts1 - ts2) / 3600 / 24
You could try with Calendars (which will also allow you to use TimeZones):
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(1371427200l * 1000l);
Calendar newCalendar = Calendar.getInstance();
newCalendar.setTimeInMillis(1371527200l * 1000l);
// prints the difference in days between newCalendar and calendar
System.out.println(newCalendar.get(Calendar.DAY_OF_YEAR) - calendar.get(Calendar.DAY_OF_YEAR));
Output:
1

Java subtract two dates in this format [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Calculating difference in dates in Java
How do you subtract Dates in Java?
I am parsing two dates from a string that look like:
Oct 15, 2012 1:07:13 PM
Oct 23, 2012 03:43:34 PM
What I need to do is find the difference between these two dates, ex:
Oct 23, 2012 03:43:34 PM - Oct 15, 2012 1:07:13 PM
= 8 days 2 hours 36 minutes 21 seconds
^ This is what I need to get with the two date/times I have
I believe I need to parse the format and convert it to another format, then subtract the difference between and do the math to get the days/hours/minutes/seconds between
In contrary to what other answerers try to imply, calculating the difference between two dates isn't that trivial in standard Java SE.
Your first step is indeed to convert those strings to useable Date instances. You can do this using SimpleDateFormat. Here's a kickoff example:
String string1 = "Oct 15, 2012 1:07:13 PM";
String string2 = "Oct 23, 2012 03:43:34 PM";
SimpleDateFormat sdf = new SimpleDateFormat("MMM d, yyyy h:mm:ss a", Locale.ENGLISH);
Date date1 = sdf.parse(string1);
Date date2 = sdf.parse(string2);
(please note the importance of the optional Locale argument here, this is often overlooked in answers about converting strings to dates)
Your next step is calculating the difference between those 2 dates. This is a terrible job when you are restricted to the standard Java SE API. Best what you can get is the java.util.Calendar.
Note that you could of course substract the milliseconds and calculate the difference using the usual arithmetic operators.
long differenceInMillis = date2.getTime() - date1.getTime();
// ...
But this naive approach doesn't take leap years into account, let alone daylight saving time and local-specific changes in datetime.
As to the java.util.Calendar approach, you basically need to use Calendar#add() in a counter loop to get the elapsed value for years, months and days. This takes leap years, daylight saving time and local-specific disturbances in time properly into account.
First create this helper method to eliminate some boilerplate code:
public static int elapsed(Calendar before, Calendar after, int field) {
Calendar clone = (Calendar) before.clone(); // Otherwise changes are been reflected.
int elapsed = -1;
while (!clone.after(after)) {
clone.add(field, 1);
elapsed++;
}
return elapsed;
}
Now you can calculate the elapsed time as follows:
Calendar start = Calendar.getInstance();
start.setTime(date1);
Calendar end = Calendar.getInstance();
end.setTime(date2);
Integer[] elapsed = new Integer[6];
Calendar clone = (Calendar) start.clone(); // Otherwise changes are been reflected.
elapsed[0] = elapsed(clone, end, Calendar.YEAR);
clone.add(Calendar.YEAR, elapsed[0]);
elapsed[1] = elapsed(clone, end, Calendar.MONTH);
clone.add(Calendar.MONTH, elapsed[1]);
elapsed[2] = elapsed(clone, end, Calendar.DATE);
clone.add(Calendar.DATE, elapsed[2]);
elapsed[3] = (int) (end.getTimeInMillis() - clone.getTimeInMillis()) / 3600000;
clone.add(Calendar.HOUR, elapsed[3]);
elapsed[4] = (int) (end.getTimeInMillis() - clone.getTimeInMillis()) / 60000;
clone.add(Calendar.MINUTE, elapsed[4]);
elapsed[5] = (int) (end.getTimeInMillis() - clone.getTimeInMillis()) / 1000;
System.out.format("%d years, %d months, %d days, %d hours, %d minutes, %d seconds", elapsed);
Pretty ugly, yeah.
If you going to work with date and time in Java pretty often, then you may find Joda time the walhalla. Here's a concrete kickoff example of how you could do it all with pure Joda Time:
String string1 = "Oct 15, 2012 1:07:13 PM";
String string2 = "Oct 23, 2012 03:43:34 PM";
DateTimeFormatter dtf = DateTimeFormat.forPattern("MMM d, yyyy h:mm:ss a").withLocale(Locale.ENGLISH);
DateTime dateTime1 = dtf.parseDateTime(string1);
DateTime dateTime2 = dtf.parseDateTime(string2);
Period period = new Period(dateTime1, dateTime2);
PeriodFormatter formatter = new PeriodFormatterBuilder()
.appendYears().appendSuffix(" years ")
.appendMonths().appendSuffix(" months ")
.appendWeeks().appendSuffix(" weeks ")
.appendDays().appendSuffix(" days ")
.appendHours().appendSuffix(" hours ")
.appendMinutes().appendSuffix(" minutes ")
.appendSeconds().appendSuffix(" seconds ")
.printZeroNever()
.toFormatter();
String elapsed = formatter.print(period);
System.out.println(elapsed);
Much better, right? The plural "s" needs some work though, but that's beyond the question.
You need to use SimpleDateFormat to parse String and create Date
Then you can find the difference between dates.
Here is javadoc for SimpleDateFormat
try this:
Calendar ca1 = Calendar.getInstance();
ca1.set(2012,05,25);
// Addition of date in java
ca1.add(Calendar.DATE, 23); // Add 23 days in Dates in Calendar
ca1.add(Calendar.MONTH, 2); // Add 2 Month in Date in Calendar
ca1.add(Calendar.YEAR, 4); // add 4 Year in Date in Calendar
ca1.add(Calendar.DATE, -23); // sub 23 days in Dates in Calendar
ca1.add(Calendar.MONTH, -2); // sub 2 Month in Date in Calendar
ca1.add(Calendar.YEAR, -4); // sub 4 Year in Date in Calendar

Categories