2

I'm creating an application where I have calculated the start date and end date but I'm getting the output of days.

For example my start date is 2019-05-16 and I write 3 days of leave so it should give me the output for the end date 2019-05-19

Here is my source code:

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

But what if I want to calculate the no of days and get the end date result.

enter image description here Here I'm getting some errors when I add button.

Community
  • 1
  • 1
  • Please confirm, Do you want difference of days (from start and end) – BlackBlind May 16 '19 at 09:07
  • I want to calculate the days and to get end date result –  May 16 '19 at 09:09
  • write your example in question , what you are passing and what is your expected result – Nikunj Paradva May 16 '19 at 09:11
  • 1
    I wrote the example and my expected output @NikunjParadva –  May 16 '19 at 09:19
  • Possible duplicate of [How can I increment a date by one day in Java?](https://stackoverflow.com/questions/428918/how-can-i-increment-a-date-by-one-day-in-java) – Ole V.V. May 17 '19 at 09:44
  • Also duplicate of [Adding days to a date in Java [duplicate\]](https://stackoverflow.com/questions/12087419/adding-days-to-a-date-in-java) and other questions. Please search. – Ole V.V. May 17 '19 at 09:45
  • As an aside consider throwing away the long outmoded and notoriously troublesome `SimpleDateFormat` and friends, and adding [ThreeTenABP](https://github.com/JakeWharton/ThreeTenABP) to your Android project in order to use `java.time`, the modern Java date and time API. It is so much nicer to work with and has much better support for your task. – Ole V.V. May 17 '19 at 09:47

3 Answers3

3

Use this function for your expected result, in this function you need to pass Start Date and number of days you want to add. and you will get your result

For Date object

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

For String Date

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;
}
Nikunj Paradva
  • 15,332
  • 5
  • 54
  • 65
  • And what about system.out.println which is there in my code –  May 16 '19 at 09:26
  • One more thing let's say if I don't want system.out.println and I want see the result in edit text box so how should I do that. –  May 16 '19 at 09:28
  • I've not added any button to calculate so how do I see the result when I select start date dialog picker. –  May 16 '19 at 09:47
  • do this code on that, or print `outputDate` in LogCat – Nikunj Paradva May 16 '19 at 09:50
  • It's giving me some errors when I put this code in Button onclickListener. –  May 16 '19 at 09:57
  • Ok I'll put a screen shot –  May 16 '19 at 10:15
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/193454/discussion-between-huzaifa-yusuf-and-nikunj-paradva). –  May 16 '19 at 10:22
0

Here it is:

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);
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));
Arwy Shelke
  • 333
  • 1
  • 2
  • 12