-3

private void startDate(String selected_date) {

    if (selected_date != null && selected_date != "0") {

        Calendar mcurrentDate = Calendar.getInstance();

        Calendar calendar = Calendar.getInstance();

        int selectDate = Integer.parseInt(selected_date);

        mcurrentDate.add(Calendar.MONTH, 1);

        mcurrentDate.set(Calendar.DAY_OF_MONTH, selectDate);

        long diff = mcurrentDate.getTimeInMillis() - calendar.getTimeInMillis();

        long days = TimeUnit.DAYS.convert(diff, TimeUnit.MILLISECONDS);

        if (days < 60) {

            mcurrentDate.add(Calendar.MONTH, 1);
            long yourmilliseconds = mcurrentDate.getTimeInMillis();
            SimpleDateFormat sdf = new SimpleDateFormat("MMM dd,yyyy HH:mm");
            Date resultdate = new Date(yourmilliseconds);
            Log.e("day60", sdf.format(resultdate));

        } else {
            long yourmilliseconds = mcurrentDate.getTimeInMillis();
            SimpleDateFormat sdf = new SimpleDateFormat("MMM dd,yyyy HH:mm");
            Date resultdate = new Date(yourmilliseconds);

            Log.e("elseday60", String.valueOf(mcurrentDate.getTimeInMillis()));

        }

    }

}

I NEED IF USER PUT 9TH in date THEN IT SHOULD REVERT BACK date BETWEEN 60 TO 90Days date not in between 0 to 30days date

  • If user input `9th`, i.e. if `selected_date = "9th"`, then `Integer.parseInt(selected_date)` will throw an exception. Try again. – Andreas Sep 12 '20 at 12:53
  • 1
    `selected_date != "0"` --- See: [How do I compare strings in Java?](https://stackoverflow.com/q/513832/5221149) – Andreas Sep 12 '20 at 12:54
  • No no its not throwing execption i need solution i dont want to print the date between 0 to 30 days only between 30 to 60days – Nitesh Kukreja Sep 12 '20 at 13:27

1 Answers1

-1

Getting a date for a particular day-of-month that is at least e.g. 60 days into the future can be done like this:

private static Date startDate(int day) {
    if (day < 1 || day > 31)
        throw new IllegalArgumentException("Invalid day: " + day);
    Calendar cal = Calendar.getInstance();
    cal.add(Calendar.DAY_OF_MONTH, 60);
    long minMillis = cal.getTimeInMillis();
    if (day > cal.getActualMaximum(Calendar.DAY_OF_MONTH)) // if day would overflow:
        cal.add(Calendar.MONTH, 1); // skip to next month which will be 31 days long
    cal.set(Calendar.DAY_OF_MONTH, day);
    if (cal.getTimeInMillis() < minMillis) {
        cal.set(Calendar.DAY_OF_MONTH, 1); // prevent day-overflow when adding 1 month
        cal.add(Calendar.MONTH, 1);
        if (day > cal.getActualMaximum(Calendar.DAY_OF_MONTH)) // if day would overflow:
            cal.add(Calendar.MONTH, 1); // skip to next month which will be 31 days long
        cal.set(Calendar.DAY_OF_MONTH, day);
    }
    return cal.getTime();
}

Test

System.out.println(new SimpleDateFormat("MMM dd,yyyy HH:mm").format(startDate(9)));
System.out.println(new SimpleDateFormat("MMM dd,yyyy HH:mm").format(startDate(30)));
System.out.println(new SimpleDateFormat("MMM dd,yyyy HH:mm").format(startDate(31)));

Sample Output (today is Sep 12, 2020)

Dec 09,2020 09:53
Nov 30,2020 09:53
Dec 31,2020 09:53

Notice how asking for the 31st needs to skip all the way to Dec 31, because Oct 31 is only 49 days and Nov doesn't have a 31st, so the next 31st is Dec 31, a whopping 110 days into the future.

Andreas
  • 154,647
  • 11
  • 152
  • 247
  • Thanks for your Answer but when i put 90 days then it show only one date i.e "DEC 12,2020" but i need both NOV AND DEC 12 date in month how to achieve that – Nitesh Kukreja Sep 12 '20 at 15:34
  • @NiteshKukreja `DEC 12,2020`? Huh? With 90 days instead of 60, that test code prints `Jan 09,2021 14:39`, `Dec 30,2020 14:39`. and `Dec 31,2020 14:39`. Where do you get `Dec 12` from? – Andreas Sep 12 '20 at 18:40
  • System.out.println(new SimpleDateFormat("MMM dd,yyyy HH:mm").format(startDate(13)));. See when i put this date in start date ok and my days was 90 days instead of 30 so i want the code to be print Nov 13,2020 11:34, Dec 13,2020 11:34 because i dont want to print the date between 0 to 30 days if my input date comes between this 0 to 30 and specially – Nitesh Kukreja Sep 13 '20 at 06:07
  • Can You Please Help Me OUT – Nitesh Kukreja Sep 13 '20 at 09:50
  • @NiteshKukreja I'm still not following you. This code will give you the first date with the given day-of-month that is at least 60 days in the future. If you then also want another date that is another month after that, i.e. you want **two** dates (although the question makes no mention of this), then just add 1 month to the date determined by the code in this answer. I don't see the problem with that. – Andreas Sep 13 '20 at 12:20
  • @NiteshKukreja *FYI:* Your question reads like you want a single date that is between 60 and 90 days in the future, which makes sense, since you'd generally have to consider a 30-day interval to find a date with a particular day-of-month, even though you might have to look further into the future if looking for the 31st, as mentioned in the answer. The code in the question reinforces that interpretation, since it only looks for a single start date. If you meant otherwise, you should edit the question to make it a whole lot clearer that you're looking for 2 dates. – Andreas Sep 13 '20 at 12:27