Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Utc time to Local time conversion

    • 0
    • 1
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 165
    Comment on it

    If you are working on meeting schedule functionality in your android app and you are stuck with the timezone then this example can guide you.

    If you are getting the time, timezone from server and time is in UTC then you should not have to apply the current timezone offset. In other words if the server timestamp is in UTC then you can simply get the time difference but if its not UTC time then you can calculate on your side like this :

    public static long getUtcTime(String dates,String timeZone,String dst){
    
        float mUtcTime;
        Date date = null;
        int intUtcTime,intTimeZone,intTime,intDst;
    
        AppLogs.printLogs("orignal server date "," in String -->" + dates);
        AppLogs.printLogs("orignal server timezone "," in String -->" + timeZone);
        AppLogs.printLogs("orignal server dst "," in String -->" + dst);
    
    
        intTime = getMinuteFromDate(dates);
        intTimeZone = getMinuteFromTimeZone(timeZone);
        intDst = Integer.parseInt(dst);
    
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        try {
            date = formatter.parse(dates);
        } catch (ParseException e) {
            e.printStackTrace();
        }
    
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.setTimeZone(TimeZone.getTimeZone("UTC"));
    
        AppLogs.printLogs("server time ", " in float -->" + intTime);
        AppLogs.printLogs("server date ", " in cal -->" + cal.getTime());
        AppLogs.printLogs("server timezone "," in int -->" + intTimeZone);
        AppLogs.printLogs("server dst "," dst -->" + dst);
    
        if (intTimeZone == getMinuteFromTimeZone("5.5")){
    
            cal.setTimeZone(TimeZone.getDefault());
            SimpleDateFormat formatters = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            try {
                date = formatters.parse(dates);
            } catch (ParseException e) {
                e.printStackTrace();
            }
            cal.setTime(date);
    
        }else{
            intUtcTime = intTime - intTimeZone + intDst*60;
            if (intUtcTime > 24*60){
                cal.add(Calendar.DATE,1);
                intUtcTime = intUtcTime - 24*60;
            }else if (intUtcTime < 0){
                cal.add(Calendar.DATE,-1);
                intUtcTime = intUtcTime + 24*60;
            }
    
            AppLogs.printLogs("int utc ", " utc time -->" + intUtcTime);
            AppLogs.printLogs("float utc ", " utc time -->" + getTimeInFormat(intUtcTime));
    
            mUtcTime = getTimeInFormat(intUtcTime);
            AppLogs.printLogs("new utc time ", " in float -->" + mUtcTime);
    
            String utcTime = String.format("%.2f", mUtcTime);
            String timeArr = utcTime;
            String [] arr = timeArr.split("\\.");
    
            cal.set(Calendar.HOUR OF Day, Integer.parseInt(arr[0]));
            cal.set(Calendar.MINUTE, Integer.parseInt(arr[1]));
            AppLogs.printLogs("utc date ", " in cal -->" + cal.getTime());
    
            cal.setTimeZone(TimeZone.getDefault());
            AppLogs.printLogs("local date ", " in cal -->" + cal.getTime());
    
        }
    
        return cal.getTimeInMillis();
    
    }
    

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: