-1

I have a date like this Tue Jun 21 14:47:37 GMT+05:30 2016 , which I create myself. I create it using calendar. Where user selects a date and I save it as milliseconds from calendar. When I send it , send it, I again create a calendar instance put the saved milliseconds into it and get the date like this :

Calendar calendar = Calendar.getInstance();
    calendar.setTimeZone(TimeZone.getDefault());
    calendar.setTimeInMillis(SSPreferences.getDate());

Now while getting date from calendar i do this :

calendar.getTime()//This is what I send to server.

I send it to server, but when server sends me the date, it is always 5.5 hours before my time.

I know my time is GMT+5:50. So what server is doing on its side ? How do I send the date , such that I get back the same date which I sent to the server.

Any help is appreciated. Thanks

intellignt_idiot
  • 1,962
  • 2
  • 16
  • 23
  • Get time UTC and keep in server or milliseconds – Sreekanth Jun 21 '16 at 09:30
  • "This is what I send to server." - *How* are you sending it to the server? – Jon Skeet Jun 21 '16 at 09:31
  • How are you sending the date to the server? Are you sending it to a php script using httpurlconnection? Something else? – Menma Jun 21 '16 at 09:33
  • @JonSkeet -- calendar.getTime() – intellignt_idiot Jun 21 '16 at 09:34
  • Just calling `calendar.getTime()` doesn't send anything to the server. *How are you sending it to the server* - and what is the server doing with it? How are you receiving the value from the server? What millisecond values are you getting? There's a *lot* of information missing here. – Jon Skeet Jun 21 '16 at 09:41
  • @JonSkeet -- Sorry, I meant, that the date which I send in the date parameter is fetched from the calendar i.e calendar.getTime() after doing this : Calendar calendar = Calendar.getInstance(); calendar.setTimeZone(TimeZone.getDefault()); calendar.setTimeInMillis(SSPreferences.getDate()); The server accepts date : Like this : reservation.put("checkinDate",calendar.getTime()); reservation.put("checkinTime", calendar.getTime()); – intellignt_idiot Jun 21 '16 at 09:43
  • That still doesn't tell us anything about how it's being transmitted. We don't know what the server is, or anything to do with how the data is being transmitted, or whether you have control over it. – Jon Skeet Jun 21 '16 at 09:46
  • It is shown on the website as : Arrival date and Time: 20-06-2016 08:17 am The date I sent had a time 1:47 PM – intellignt_idiot Jun 21 '16 at 09:49
  • In, iOS it is sent after being converted to local time, so that the time remains local to user. In ios it is send like this : func toLocalTime() -> NSDate { let timeZone = NSTimeZone.localTimeZone() let seconds : NSTimeInterval = Double(timeZone.secondsFromGMTForDate(self)) let localDate = NSDate(timeInterval: seconds, sinceDate: self) return localDate } – intellignt_idiot Jun 21 '16 at 09:54

2 Answers2

0

check this

static final String DATEFORMAT = "yyyy-MM-dd HH:mm:ss"

public static Date GetUTCdatetimeAsDate()
{
    //note: doesn't check for null
    return StringDateToDate(GetUTCdatetimeAsString());
}

public static String GetUTCdatetimeAsString()
{
    final SimpleDateFormat sdf = new SimpleDateFormat(DATEFORMAT);
    sdf.setTimeZone(TimeZone.getTimeZone("UTC"));
    final String utcTime = sdf.format(new Date());

    return utcTime;
}

public static Date StringDateToDate(String StrDate)
{
    Date dateToReturn = null;
    SimpleDateFormat dateFormat = new SimpleDateFormat(DATEFORMAT);

    try
    {
        dateToReturn = (Date)dateFormat.parse(StrDate);
    }
    catch (ParseException e)
    {
        e.printStackTrace();
    }

    return dateToReturn;
}
Sreekanth
  • 407
  • 4
  • 8
  • 20
0

Finally I solved the issue by adding/subtracting the rawOffSet from the local time zone.

    TimeZone timeZone = TimeZone.getDefault();
    int offset = timeZone.getOffset(SSPreferences.getDate());
    WriteLog.Print("offset is "+offset);
    long newtime = SSPreferences.getDate() + offset;
    calendar.setTimeInMillis(newtime);
intellignt_idiot
  • 1,962
  • 2
  • 16
  • 23