如何计算天数以获取android中的结束日期?

时间:2019-05-16 08:57:53

标签: android date calendar

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

例如,我的开始日期是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);
 }

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

enter image description here在添加按钮时出现一些错误。

3 个答案:

答案 0 :(得分:0)

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

对于日期对象

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

答案 1 :(得分:0)

这里是:

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

答案 2 :(得分:0)

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