Java Date get days between two date values

Description

Java Date get days between two date values

import java.time.Instant;
import java.util.Calendar;
import java.util.Date;

public class Main {
  public static void main(String[] argv) {
    Date start = new Date();
    Date end = Date.from(Instant.EPOCH);
    System.out.println(dayDiff(start, end));
  }/*  ww w  . j a v a  2  s .  c  o  m*/

  public static long dayDiff(Date start, Date end) {
    long day = compareDate(start, end);
    if (day == 0) {
      return 0;
    }
    if (day > 0) {
      return (day - 1) / 1000 / 60 / 60 / 24 + 1;
    } else {
      return (day + 1) / 1000 / 60 / 60 / 24 - 1;
    }

  }

  public static long compareDate(Date start, Date end) {
    long temp = 0;
    Calendar starts = Calendar.getInstance();
    Calendar ends = Calendar.getInstance();
    starts.setTime(start);
    ends.setTime(end);
    temp = ends.getTime().getTime() - starts.getTime().getTime();
    return temp;
  }
}
import java.time.Instant;
import java.util.Calendar;
import java.util.Date;

public class Main {
  public static void main(String[] argv) {
    Date start = Date.from(Instant.EPOCH);
    Date end = new Date();
    System.out.println(compareDay(start, end));
  }//from  w w w . java 2 s . com

  public static long compareDay(Date start, Date end) {
    long day = compareDate(start, end);
    return day / 1000 / 60 / 60 / 24;
  }

  public static long compareDate(Date start, Date end) {
    long temp = 0;
    Calendar starts = Calendar.getInstance();
    Calendar ends = Calendar.getInstance();
    starts.setTime(start);
    ends.setTime(end);
    temp = ends.getTime().getTime() - starts.getTime().getTime();
    return temp;
  }
}



PreviousNext

Related