Java Date get seconds between two date values

Requirements

Write code to compare Date

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(compareDate(start, end));
  }/*  w w w.  ja  v  a  2  s  .c  o m*/

  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