Java 减去日期,得到天数差。如何减去它们?

Java 减去日期,得到天数差。如何减去它们?,java,date,date-difference,Java,Date,Date 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

谁能告诉我如何从“今天”中减去“之后”的字符串以得到日差

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);

}
}

使用java8会更容易,因为您不需要减去表示日期的长值并将其更改回天、小时和分钟

Date today= LocalDate.now();
Date futureDate = LocalDate.now().plusDays(1);
long days = Period.between(today, futureDate).getDays();
&class在#java8中提供

LocalDate
docs

LocalDate是表示日期的不可变日期时间对象, 通常被视为年-月-日。其他日期字段,例如 也可以访问年中的某一天、某一周的某一天和某一年的某一周。对于 例如,值“2007年10月2日”可以存储在LocalDate中


如果您不使用java8,请使用joda时间库的实用程序来计算此值

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

使用java8会更容易,因为您不需要减去表示日期的长值并将其更改回天、小时和分钟

Date today= LocalDate.now();
Date futureDate = LocalDate.now().plusDays(1);
long days = Period.between(today, futureDate).getDays();
&class在#java8中提供

LocalDate
docs

LocalDate是表示日期的不可变日期时间对象, 通常被视为年-月-日。其他日期字段,例如 也可以访问年中的某一天、某一周的某一天和某一年的某一周。对于 例如,值“2007年10月2日”可以存储在LocalDate中


如果您不使用java8,请使用joda时间库的实用程序来计算此值

Days day = Days.daysBetween(startDate, endDate);
int days = d.getDays();
这可能对你有帮助

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");
这可能对你有帮助

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");
如果没有Java8,请使用

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());
哪个输出

2014-11-11; 2014-12-01
Days = 6
Weeks = 2
Months = 0
如果没有Java8,请使用

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());
哪个输出

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

愿它能帮助你

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.");
  }
}
试试这个

愿它能帮助你

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.");
  }
}

似乎与此相关。@amitbhardwaj我同意,但这只是问题的一半…@amitbhardwaj在JodaTime中有一个
期间
持续时间
类。。。似乎与此相关。@amitbhardwaj我同意,但这只是问题的一半…@amitbhardwaj在JodaTime中有一个
Period
Duration
类…不,永远不要这样做。日期/时间的工作方式有很多差异(闰年、世纪和千年边界),只需减去两个日期的毫秒偏移量不,永远不要这样做。日期/时间的工作方式(闰年、世纪和千年边界)有很多差异,只需减去两个日期的毫秒偏移量。如果您达到了要求,并且想要使用JodaTime添加一个示例,我将删除我的答案+1,如果您达到了要求,并且想要使用JodaTime添加一个示例,不过我会删除我的答案+1