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.
serverDate – currentDate
Working: This is how I calculate the difference between days,
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));
Working: To make a date added with 5 days from the current date,
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());
}
Eventually: I might need to modify this use case with a difference of total days to a large number 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.
ok, from the comments of the question, I try to rephrase a bit the requirement:
totaldays(gap) is difference betweenserverDateandcurrentDateif the what was written above is true
totaldaysthen one line would solve your problem:
btw, you should clearly describe what you need in your question. Good luck!