3

I need to pick a future date from calendar, suppose the date I am selecting is 04/30/2013. Now what I want is to send the date to server. At server end I need to calculate the number of days between the future date and current date and send it to database.

The problem is that when I do the calculation locally (because my server and browser are in same timezone) it works fine, but when the server is in a different timezone than the browser the difference in days does not come as expected. Someone please help me how to solve the timezone issues.

Thor
  • 45,082
  • 11
  • 119
  • 130
yarra
  • 43
  • 1
  • 6

2 Answers2

0

To Offset for time differences

You should use a format to send the data that includes the timezone within it. You could:

  1. Use UNIX time which does not use timezones (milliseconds since epoch) with GMT 00, and also use this on java side

see: Get current date/time in seconds

  1. Use ISO-8601 which is a standard and can include the timezone as well of the browser, and then parse this server side:

see:

To Calculate the Difference (using MS and Calendar Object)

You need to use the calendar object. You create a calendar object and set with a future date: e.g. How to set Java.util.calendar to a specific time period in the future Then you can perform a calculation by subtracting the current date (as long) with the long returned by the calendar, and dividing by (1000*60*60*24).

Some code I used in my own application to find entries greater than a specific datetime (midnight):

long currentTimeStamp = System.currentTimeMillis();
Calendar cal;
cal = Calendar.getInstance();
cal.setTime(new Date(currentTimeStamp));

//GET MS OF MIDNIGHT FROM BEGINNING OF THE DAY
cal.set(Calendar.HOUR_OF_DAY, 0); cal.set(Calendar.MINUTE, 0); cal.set(Calendar.SECOND, 0); cal.set(Calendar.MILLISECOND,0);
long midnight = cal.getTimeInMillis();
if(currentTimeStamp - midnight > (30*24*60*60*1000))break;
Community
  • 1
  • 1
Menelaos
  • 23,508
  • 18
  • 90
  • 155
0

Convert both dates in one common timezone e.g. GMT and then calculate the difference.