Note that there are some explanatory texts on larger screens.

plurals
  1. POAdd specific number of days to current date conditionally?
    text
    copied!<p>So the problem is I have got a date from server and I want to add specific days based on the difference between the current date and the date that I have got from the server.</p> <blockquote> <p>serverDate - currentDate </p> </blockquote> <p><strong>Working:</strong> This is how I calculate the difference between days,</p> <pre><code>String s=formater.format(currentDate); long d1 = formater.parse(s).getTime(); long d2=formater.parse(serverDate).getTime(); int totaldays=(int)Math.abs((d1-d2)/(1000*60*60*24)); </code></pre> <p><strong>Working: To make a date added with 5 days from the current date,</strong></p> <pre><code> if(totaldays==5) { Calendar c=Calendar.getInstance(); c.add(Calendar.DATE, 0); System.out.println("value of new server date "+c.getTime()); } else if(totaldays==4) { Calendar c=Calendar.getInstance(); c.add(Calendar.DATE, 1); System.out.println("value of new server date "+c.getTime()); } else if(totaldays==3) { Calendar c=Calendar.getInstance(); c.add(Calendar.DATE, 2); System.out.println("value of date "+c.getTime()); } else if(totaldays==2) { Calendar c=Calendar.getInstance(); c.add(Calendar.DATE, 3); System.out.println("value of new server date "+c.getTime()); } else if(totaldays==1) { Calendar c=Calendar.getInstance(); c.add(Calendar.DATE, 4); System.out.println("value of new server date "+c.getTime()); } else if(totaldays==0) { Calendar c=Calendar.getInstance(); c.add(Calendar.DATE, 0); System.out.println("value of new server date "+c.getTime()); } </code></pre> <p><strong>Eventually:</strong> I might need to modify this use case with a <strong>difference of total days to a large number</strong> so, in that case would I need to increase the number of comparisons or is there any trick that I can use to reach a specific date.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload