https://micromyaw.blogspot.com/ , https://thefairlyoddgirl.blogspot.com/ , https://maxyaquos.blogspot.com/ , https://almourasiloun2.blogspot.com/ , https://rudythecassol.blogspot.com/ , https://innaz2.blogspot.com/ , https://stephaniefulke.blogspot.com/ , https://syafiqzulkarnain2.blogspot.com/ , https://lifeandeating.blogspot.com/ , https://nandncomputers.blogspot.com/ , https://profsmythe.blogspot.com/ , https://shwesetharrking.blogspot.com/ , https://sihanandi.blogspot.com/ , https://ikkemunandar.blogspot.com/ , https://youshaoken.blogspot.com/ , https://bhrepublicadominicana.blogspot.com/ , https://austinangck2809.blogspot.com/ , https://indiafunworld.blogspot.com/ , https://everythingnbeyondblog.blogspot.com/ , https://19miracles.blogspot.com/ , https://bpcyclingteam.blogspot.com/ , https://katieandmaxtron.blogspot.com/ , https://southernmatron.blogspot.com/ , https://redzuanifaliyana.blogspot.com/ , https://waeltnx.blogspot.com/ , https://superpixels.blogspot.com/ , https://nikephang.blogspot.com/ , https://belialslut.blogspot.com/ , https://testallforone.blogspot.com/ , https://buyandroidsmartphone.blogspot.com/ , https://darellsfinancialcorner.blogspot.com/ , https://ferraricars77.blogspot.com/ , https://memesmemosos.blogspot.com/ , https://faultyaspirations.blogspot.com/ , https://puriagatsari.blogspot.com/ , https://ghchjgv.blogspot.com/ , https://smurugesaninfo.blogspot.com/ , https://diandrakesling.blogspot.com/ , https://eshikamien.blogspot.com/ , https://oxgila.blogspot.com/ , https://ramblingsofker.blogspot.com/ , https://laladarwis.blogspot.com/ , https://fullcomicfrenzy.blogspot.com/ , https://aziin5teens.blogspot.com/ , https://fcomax.blogspot.com/

New Java - calculate the difference between two dates By Gamzeozgesaroglu

New Java - calculate the difference between two dates By Gamzeozgesaroglu - welcome to the blog Specs Price Techno, On this occasion we will discuss article entitled New Java - calculate the difference between two dates By Gamzeozgesaroglu, we have collected a lot of data from various sources to make this article so that you no longer need to seek elsewhere, in addition to discussing this information we also have to provide a lot of information about the latest gaget, please look for it this blog, please let continue reading, hopefully you can easily understand that add to your knowledge.

you're looking for: New Java - calculate the difference between two dates By Gamzeozgesaroglu
complete information: New Java - calculate the difference between two dates By Gamzeozgesaroglu
Artikel java, Artikel sample code, Artikel tips and tricks,

You can also see our article on:


New Java - calculate the difference between two dates By Gamzeozgesaroglu

Finding the difference between two dates isn't as straightforward as subtracting the two dates and dividing the result by (24 * 60 * 60 * 1000). Infact, its erroneous! 

Going the 'milliseconds way' will lead to rounding off errors and they become most evident once you have a little thing like "Daylight Savings Time" come into the picture.

The Correct Way:

/** Using Calendar - THE CORRECT WAY**/
//assert: startDate must be before endDate
public static long daysBetween(Calendar startDate, Calendar endDate) {
Calendar date = (Calendar) startDate.clone();
long daysBetween = 0;
while (date.before(endDate)) {
date.add(Calendar.DAY_OF_MONTH, 1);
daysBetween++;
}
return daysBetween;
}
}

or more efficiently, (thanks Mauro), if you're using the Gregorian Calendar:
/** Using Calendar - THE CORRECT (& Faster) WAY**/
/****Needs testing ...... Anyone?****/
//assert: startDate must be before endDate
public static long daysBetween(final Calendar startDate, final Calendar endDate) {
int MILLIS_IN_DAY = 1000 * 60 * 60 * 24;
long endInstant = endDate.getTimeInMillis();
int presumedDays = (int) ((endInstant - startDate.getTimeInMillis()) / MILLIS_IN_DAY);
Calendar cursor = (Calendar) startDate.clone();
cursor.add(Calendar.DAY_OF_YEAR, presumedDays);
long instant = cursor.getTimeInMillis();
if (instant == endInstant)
return presumedDays;
final int step = instant < endInstant ? 1 : -1;
do {
cursor.add(Calendar.DAY_OF_MONTH, step);
presumedDays += step;
} while (cursor.getTimeInMillis() != endInstant);
return presumedDays;
}



The Nuances:

Lets take, for example, the difference between the two dates
03/24/2007 and 03/25/2007 should be 1 day; 

However, using the millisecond route, you'll get 0 days, if you run this in the UK!


import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;

public class DateTest {

public class DateTest {

static SimpleDateFormat sdf = new SimpleDateFormat("dd-MMM-yyyy");

public static void main(String[] args) {

TimeZone.setDefault(TimeZone.getTimeZone("Europe/London"));

//diff between these 2 dates should be 1
Date d1 = new Date("01/01/2007 12:00:00");
Date d2 = new Date("01/02/2007 12:00:00");

//diff between these 2 dates should be 1
Date d3 = new Date("03/24/2007 12:00:00");
Date d4 = new Date("03/25/2007 12:00:00");

Calendar cal1 = Calendar.getInstance();cal1.setTime(d1);
Calendar cal2 = Calendar.getInstance();cal2.setTime(d2);
Calendar cal3 = Calendar.getInstance();cal3.setTime(d3);
Calendar cal4 = Calendar.getInstance();cal4.setTime(d4);

printOutput("Manual ", d1, d2, calculateDays(d1, d2));
printOutput("Calendar ", d1, d2, daysBetween(cal1, cal2));
System.out.println("---");
printOutput("Manual ", d3, d4, calculateDays(d3, d4));
printOutput("Calendar ", d3, d4, daysBetween(cal3, cal4));
}


private static void printOutput(String type, Date d1, Date d2, long result) {
System.out.println(type+ "- Days between: " + sdf.format(d1)
+ " and " + sdf.format(d2) + " is: " + result);
}

/** Manual Method - YIELDS INCORRECT RESULTS - DO NOT USE**/
/* This method is used to find the no of days between the given dates */
public static long calculateDays(Date dateEarly, Date dateLater) {
return (dateLater.getTime() - dateEarly.getTime()) / (24 * 60 * 60 * 1000);
}

/** Using Calendar - THE CORRECT WAY**/
public static long daysBetween(Calendar startDate, Calendar endDate) {
Calendar date = (Calendar) startDate.clone();
long daysBetween = 0;
while (date.before(endDate)) {
date.add(Calendar.DAY_OF_MONTH, 1);
daysBetween++;
}
return daysBetween;
}
}

OUTPUT:
Manual - Days between: 01-Jan-2007 and 02-Jan-2007 is: 1
Calendar - Days between: 01-Jan-2007 and 02-Jan-2007 is: 1
---
Manual - Days between: 24-Mar-2007 and 25-Mar-2007 is: 0
Calendar - Days between: 24-Mar-2007 and 25-Mar-2007 is: 1



Articles New Java - calculate the difference between two dates By Gamzeozgesaroglu we have presented

That's all the information about the New Java - calculate the difference between two dates By Gamzeozgesaroglu, hopefully can provide benefits to all of you in finding information latest gadgets, how to care for gadgets, tips and tricks mobile phone.

Thank you for reading the article New Java - calculate the difference between two dates By Gamzeozgesaroglu and its url of this article is https://gamzeozgesaroglu.blogspot.com/2007/07/new-java-calculate-difference-between.html o you to bookmark and you can go back if you need :), I hope the article this can be useful for you all.

Related News :

0 Response to "New Java - calculate the difference between two dates By Gamzeozgesaroglu"

Post a Comment