Skip to content Skip to sidebar Skip to footer

Android:display Time After Adding Gmt Time Zone

I am working on a App in which i want to display notification time. I can display notification time but not able to add time zone in it. My current location is Pakistan and i want

Solution 1:

Update

You mark time with timezone in order to solve internationalization problem, I understand, right?

If so, I think it could be better to convert your date to UTC date. When you change to another timezone, just convert this UTC Date to local.

publicstaticDatelocalToUtc(Date localDate) {
    returnnewDate(localDate.getTime()-TimeZone.getDefault().getOffset(localDate.getTime()));
}
publicstaticDateutcToLocal(Date utcDate) {
    returnnewDate(utcDate.getTime()+TimeZone.getDefault().getOffset(utcDate.getTime()));
}

Old answer

If your notif.At is Dateobject, it's a same question actually:

TimeZone tz = TimeZone.getDefault();
Datedate=newDate(); 
final String format = "yyyy-MM-dd HH:mm:ss";

SimpleDateFormat sdf =new SimpleDateFormat(format, Locale.US);
String result= sdf.format(date);
Log.d("Date ", "date: " +result+ " " + tz.getDisplayName(false, TimeZone.SHORT));

print:

date: 2015-03-31 18:45:28 GMT+08:00

Solution 2:

You can try java.time api;

        Instant date = Instant.ofEpochMilli(1549362600000l);
        LocalDateTime utc = LocalDateTime.ofInstant(date, ZoneOffset.UTC);
        LocalDateTime pst = LocalDateTime.ofInstant(date, ZoneOffset.of("+05:00"));
        LocalDateTime is = LocalDateTime.ofInstant(date, ZoneOffset.of("+05:30"));

Post a Comment for "Android:display Time After Adding Gmt Time Zone"