如何计算在 android 中获得结束日期的天数?

how to calculate days to get end date in android?

我正在创建一个应用程序,我在其中计算了开始日期和结束日期,但我得到的是天数的输出。

例如,我的开始日期是 2019-05-16,我写了 3 天的假期,所以它应该给我结束日期 2019-05-19 的输出

这是我的源代码:

public static final String DATE_FORMAT = "d/M/yyyy";

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Here i can use Start date as a 01/01/2018 and End Date is 01/01/2019
System.out.println ("Days: " + getDaysBetweenDates("01/02/2018","01    /01/2019"));
}
public static long getDaysBetweenDates(String start, String end) {
SimpleDateFormat dateFormat = new SimpleDateFormat(DATE_FORMAT,      Locale.ENGLISH);
   Date startDate, endDate;
   long numberOfDays = 0;
    try {
    startDate = dateFormat.parse(start);
    endDate = dateFormat.parse(end);
    numberOfDays = getUnitBetweenDates(startDate, endDate, TimeUnit.DAYS);
    } catch (ParseException e) {
    e.printStackTrace();
    }
    return numberOfDays;
 }


private static long getUnitBetweenDates(Date startDate, Date endDate,   TimeUnit unit) {
    long timeDiff = endDate.getTime() - startDate.getTime();
    return unit.convert(timeDiff, TimeUnit.MILLISECONDS);
 }

但是如果我想计算天数并获得结束日期结果怎么办。

我在添加按钮时遇到了一些错误。

使用此函数获得预期结果,在此函数中您需要传递开始日期和要添加的天数。你会得到你的结果

对于日期对象

String outputDate = addDays(new Date(),5); //for current date

 public static String addDays(Date startDate,int numberOfDays) {
    Calendar c = Calendar.getInstance();
    c.setTime(startDate);
    c.add(Calendar.DAY_OF_WEEK,numberOfDays);
    SimpleDateFormat dateFormat=new SimpleDateFormat("dd-MM-yyyy",  Locale.ENGLISH);
    String resultDate=dateFormat.format(c.getTime());
    return resultDate;
}

对于字符串日期

String outputDate = addDays("01/02/2018",5);


public static String addDays(String startDate,int numberOfDays) {
    SimpleDateFormat dateFormat=new SimpleDateFormat("dd-MM-yyyy",  Locale.ENGLISH);
    Calendar c = Calendar.getInstance();
    try {
        c.setTime(dateFormat.parse(startDate));
    } catch (ParseException e) {
        e.printStackTrace();
    }
    c.add(Calendar.DAY_OF_WEEK,numberOfDays);
    String resultDate=dateFormat.format(c.getTime());
    return resultDate;
}

这里是:

Calendar calendar = Calendar.getInstance();
    calendar.set(Calendar.YEAR, 2019);
    calendar.set(Calendar.MONTH, 5);
    calendar.set(Calendar.DAY_OF_MONTH, 16);

    int NUMBER_OF_DAY_TO_ADD = 3;

    calendar.add(Calendar.DATE, NUMBER_OF_DAY_TO_ADD);

    String nextDate = String.valueOf( DateFormat.format("dd-MM-yyyy", calendar.getTimeInMillis()));
    System.out.println("Next date will:-> " + nextDate);
    public static final String DATE_FORMAT = "d/M/yyyy";    

    //Date startDate = new Date(DATE_FORMAT.format(sourceDate));

    // convert date to calendar
    Calendar c = Calendar.getInstance();
    c.setTime(startDate);

    // manipulate date
    //c.add(Calendar.YEAR, 1);
    //c.add(Calendar.MONTH, 1);
    c.add(Calendar.DATE, 1);    //add here the number of days 
    //c.add(Calendar.HOUR, 1);
    //c.add(Calendar.MINUTE, 1);
    //c.add(Calendar.SECOND, 1);

    // convert calendar to date
    Date targetDate = c.getTime();

    System.out.println(DATE_FORMAT.format(targetDate));