java - Get date by utc/gmt in Android -
i want display time in country, , timezone gmt+4.
private void loadweather(){ timezone tz = timezone.gettimezone("gmt+0400"); calendar cal = calendar.getinstance(tz); date date = cal.gettime(); dateformat df = dateformat.getdatetimeinstance(dateformat.short,dateformat.short, locale.getdefault()); string mydate = df.format(date); tv_time.settext(mydate); }
i've tried this, gives me time, , not other one
the problem you're specifying time zone on calendar
- only used current instant in time, doesn't depend on time zone. need specify on format instead, it's applied when creating appropriate text representation of instant:
private void loadweather() { date date = new date(); // enough; uses current instant. dateformat df = dateformat.getdatetimeinstance( dateformat.short, dateformat.short, locale.getdefault()); df.settimezone(timezone.gettimezone("gmt+0400")); string mydate = df.format(date); tv_time.settext(mydate); }
or inline more:
private void loadweather() { dateformat df = dateformat.getdatetimeinstance( dateformat.short, dateformat.short, locale.getdefault()); df.settimezone(timezone.gettimezone("gmt+0400")); tv_time.settext(df.format(new date())); }
(this assuming want short date/time format using current locale.)
Comments
Post a Comment