Skip to content

Instantly share code, notes, and snippets.

@n1ckfg
Created October 3, 2021 07:16
Show Gist options
  • Select an option

  • Save n1ckfg/5e0c446d22a5d65d4728a5f58ede914c to your computer and use it in GitHub Desktop.

Select an option

Save n1ckfg/5e0c446d22a5d65d4728a5f58ede914c to your computer and use it in GitHub Desktop.
// https://stackoverflow.com/questions/20165564/calculating-days-between-two-dates-with-java/29812532
import java.util.Calendar;
import java.util.Date;
import java.util.Scanner;
void setup() {
println(getDateDifference("04 03 2009", "12 06 2021", " "));
println(getDateDifference("04 11 1999", "05 11 1999", " "));
}
void draw() {
//
}
int getDateDifference(String date1, String date2, String separator) {
String[] insert1 = date1.split(separator);
String[] insert2 = date2.split(separator);
Calendar cal = Calendar.getInstance();
cal.set(Calendar.DAY_OF_MONTH, Integer.parseInt(insert1[0]));
cal.set(Calendar.MONTH, Integer.parseInt(insert1[1]));
cal.set(Calendar.YEAR, Integer.parseInt(insert1[2]));
Date firstDate = cal.getTime();
cal.set(Calendar.DAY_OF_MONTH, Integer.parseInt(insert2[0]));
cal.set(Calendar.MONTH, Integer.parseInt(insert2[1]));
cal.set(Calendar.YEAR, Integer.parseInt(insert2[2]));
Date secondDate = cal.getTime();
long diff = secondDate.getTime() - firstDate.getTime();
return int(diff / 1000 / 60 / 60 / 24);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment