convert custom locale date time to gmt in android -
i trying convert date time format created on locale time on android device gmt . unable right value .also datetime generated local time on android.how can convert exact gmt unixtime ?
string datetime="02-02-2016 8:28 pm"; simpledateformat format = new simpledateformat("dd-mm-yyyy hh:mm a"); format.settimezone(timezone.gettimezone("gmt")); date date = null; try { date = format.parse(datetime); system.out.println("date ->" + date); } catch (exception e) { e.printstacktrace(); } long unixtime = date.gettime() / 1000;
try{ simpledateformat sourceformat = new simpledateformat("yyyy-mm-dd hh:mm:ss"); sourceformat.settimezone(timezone.gettimezone("utc")); log.v("current time", ""+ sourceformat.format(new date())); date parsed = sourceformat.parse(sourceformat.format(new date())); // => date in utc timezone tz = timezone.gettimezone("america/los_angeles"); simpledateformat destformat = new simpledateformat("yyyy-mm-dd hh:mm:ss"); destformat.settimezone(tz); // string result = destformat.format(parsed); string result = destformat.format(sourceformat.parse(sourceformat.format(new date()))); log.e("result", ""+result); // 2015-11-26 02:07:59 simpledateformat utcformat = new simpledateformat("yyyy-mm-dd hh:mm:ss"); simpledateformat severformat = new simpledateformat("yyyy-mm-dd hh:mm:ss"); simpledateformat localformat = new simpledateformat("yyyy-mm-dd hh:mm:ss"); utcformat.settimezone(timezone.gettimezone("utc")); timezone tz1 = timezone.gettimezone("america/los_angeles"); severformat.settimezone(tz1); localformat.settimezone(timezone.getdefault()); date serverdate = severformat.parse(result); date utcdate = utcformat.parse(utcformat.format(serverdate)); string localdate = localformat.format(utcdate); log.e("localdate", ""+localdate); }catch(parseexception e){ e.printstacktrace(); } catch (java.text.parseexception e) { // todo auto-generated catch block e.printstacktrace(); }
Comments
Post a Comment