Subtracting dates and get difference in days.How t

2019-02-21 03:09发布

问题:

This question already has an answer here:

  • Calculating the difference between two Java date instances 45 answers

can anyone tell how to subtract string "after" from "today" to get days difference.

import java.text.*;
import java.util.*;


public class Main {
public static void main(String args[]){


SimpleDateFormat sdf=new SimpleDateFormat("yyyy/MM/dd");
Calendar cal=Calendar.getInstance();
String today=sdf.format(cal.getTime());
System.out.println(today);
cal.add(Calendar.DATE, 20);
String After=sdf.format(cal.getTime());
System.out.println(After);

}
}

回答1:

It would be easier with java8 where you dont need to subtract long values represent of date and change back to days, hours and minutes.

Date today= LocalDate.now();
Date futureDate = LocalDate.now().plusDays(1);
long days = Period.between(today, futureDate).getDays();

Period & LocalDate class are available in #java8

LocalDate docs

LocalDate is an immutable date-time object that represents a date, often viewed as year-month-day. Other date fields, such as day-of-year, day-of-week and week-of-year, can also be accessed. For example, the value "2nd October 2007" can be stored in a LocalDate.


If you are not using java8, use joda-time library's org.joda.time.Days utility to calculate this

Days day = Days.daysBetween(startDate, endDate);
int days = d.getDays();


回答2:

Using JodaTime, in case you don't have Java 8

String timeValue = "2014/11/11";
DateTimeFormatter parseFormat = new DateTimeFormatterBuilder().appendPattern("yyyy/MM/dd").toFormatter();
LocalDate startDate = LocalDate.parse(timeValue, parseFormat);
LocalDate endDate = startDate.plusDays(20);

System.out.println(startDate + "; " + endDate);

Period p = new Period(startDate, endDate);
System.out.println("Days = " + p.getDays());
System.out.println("Weeks = " + p.getWeeks());
System.out.println("Months = " + p.getMonths());

Which outputs...

2014-11-11; 2014-12-01
Days = 6
Weeks = 2
Months = 0


回答3:

try this...

May it helps you.

import java.util.Date;
import java.util.GregorianCalendar;

// compute the difference between two dates.

public class DateDiff {
  public static void main(String[] av) {
    /** The date at the end of the last century */
    Date d1 = new GregorianCalendar(2010, 10, 10, 11, 59).getTime();

    /** Today's date */
    Date today = new Date();

    // Get msec from each, and subtract.
    long diff = today.getTime() - d1.getTime();

    System.out.println("The 21st century (up to " + today + ") is "
        + (diff / (1000 * 60 * 60 * 24)) + " days old.");
  }
}


回答4:

This may help You..

SimpleDateFormat sdf=new SimpleDateFormat("yyyy/MM/dd");
    Calendar cal=Calendar.getInstance();
    String today=sdf.format(cal.getTime());
    System.out.println(today);

    cal.add(Calendar.DATE, 20);
    String After=sdf.format(cal.getTime());
    System.out.println(After);

    Date d1 = null;
    Date d2 = null;

    SimpleDateFormat format = new SimpleDateFormat("yyyy/MM/dd");

    try {
        d1 = format.parse(today);
        d2 = format.parse(After);
    } catch (ParseException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    long diff = d2.getTime() - d1.getTime();

    long diffDays = diff / (24 * 60 * 60 * 1000);

    System.out.println("Difference is "+diffDays+" Days");