Search code examples
androiddatetimesharepointutcdst

DayLightSavingMode issue in Android


I know this Questions Asked before so many time but i didn't get solution from it.

I am trying to convert Datetime to UTC-0.

I have backend as microsoft sharepoint O365.

I am adding record in sharepoint List.

I am store user timezone when it register. so now whenever user login they can see all data with dateandtime in their selected timezone which store at time of registration.

Now my point is user registered timezone is Asia/Kolkata.

now if he login using other country device like London. then he can see all data date time in Asia/Kolkata which is selected at register time.

For eg. User registered timezone at time of registration like GMT+5:30 then if user mobile timezone is whatever they can see in GMT+5:30 DateTime

And if he add any data from app then it also store as Asia/Kolkata timezone which Datetime not as current mobile timezone if he using Mobile in london.

how to Resolve it?

my code like below:

    public  String getDateAndTimeToUserFormat()
{

    String date;
    TimeZone timeZone = TimeZone.getTimeZone("Indian Standard Time");

    Calendar calendar = Calendar.getInstance(timeZone);


    SimpleDateFormat simpleDateFormat =
            new SimpleDateFormat(serverdateFormat, Locale.US);
   simpleDateFormat.setTimeZone(timeZone);

date= simpleDateFormat.format(calendar.getTime());

Log.i("TAG","UserTimezone=>"+this.timeZone);
Log.i("TAG","Time zone: " + timeZone.getID());

Log.i("TAG","default time zone: " + TimeZone.getDefault());
Log.i("TAG","default time : " + Calendar.getInstance().getTime());
Log.i("TAG","UTC:     " + date);
Log.i("TAG","Default: " + calendar.getTime());



    return date;

}

Output: UserTimezone=>India Standard Time Time zone: GMT

default time zone: Africa/Casablanca

default time : Fri Jun 17 12:53:55 GMT+00:00 2016

UTC: 2016-06-17T12:53:55Z

Default: Fri Jun 17 12:53:55 GMT+00:00 2016


Solution

  • finally i have make code for my requirement.

     public String getDateTimeToUTC() {
    
            String result=convertToUTCDateTime(convertToUserDateTime(this.timeZone));
    
        return result;
    
    }
    
    /**
     *
     * @param userTimeZone : is contain String Of User Selected TimeZone
     * @return Date: it contain DateTime of UserSelected TimeZone
     */
    
    
     public Date convertToUserDateTime(String userTimeZone)
    { Date parsed = null;
    TimeZone timeZone = TimeZone.getTimeZone(userTimeZone);
    
    Calendar calendar = Calendar.getInstance(timeZone);
    
    SimpleDateFormat simpleDateFormat =
            new SimpleDateFormat(serverdateFormat, Locale.US);
    simpleDateFormat.setTimeZone(timeZone);
    String date = simpleDateFormat.format(calendar.getTime());
    try {
    parsed=simpleDateFormat.parse(date);
    
        Log.i("TAG", "default time zone: " + TimeZone.getDefault().getID());
        Log.i("TAG", "User Selected Timezone=>" + timeZone.getID());
        Log.i("TAG", "default Device DateTime : " + Calendar.getInstance().getTime());
        Log.i("TAG","User selected  DateTime=>"+date);
    
    
    } catch (ParseException e) {
        e.printStackTrace();
    }
    return parsed ;
    }    /**
     * @param userZoneDateTime : is contain User Selected TimeZone Date
     * @return String : Return UTC-0 DateTime String From User Selected DateTimeZone
     */
    public  String convertToUTCDateTime(Date userZoneDateTime)
    {
        String result;
        TimeZone timeZone1 = TimeZone.getTimeZone("UTC");
    
        SimpleDateFormat simpleDateFormat1 =
                new SimpleDateFormat(serverdateFormat, Locale.US);
        simpleDateFormat1.setTimeZone(timeZone1);
    
    
            result = simpleDateFormat1.format(userZoneDateTime);
            Log.i("TAG", "UTC Time zone: " + timeZone1.getID());
            Log.i("TAG", "UTC:     " + result);
    
        return result;
    
    }