时区无法正常工作

时间:2016-03-19 08:01:45

标签: android timezone

我正在获取印度,加拿大,美国的日期和时间。我正确地得到时间和日期, 但我得到了三个国家的同一时区(GMT +5:30)。其实我应该得到不同的时区喜欢

 India  ------ Date IST.
 Canada ------ Date PST.
 USA    ------ Date PST.

这是我的逻辑

public void getDate(String timeZoneId) {
   String selectedId = timeZoneId;
    TimeZone timezone = TimeZone.getTimeZone(selectedId);
    TimeZoneName = timezone.getDisplayName();
    Log.v("TimeZoneName","TimeZoneName   "+TimeZoneName);
    int TimeZoneOffset = timezone.getRawOffset()
        / (60 * 1000);
    int hrs = TimeZoneOffset / 60;
    int mins = TimeZoneOffset % 60;
    miliSeconds = miliSeconds + timezone.getRawOffset();
    resultdate = new Date(miliSeconds);
    dateInCountry=sdf.format(resultdate);
    miliSeconds = 0;
}

private void getGMTTime() {
    current = Calendar.getInstance(Locale.ENGLISH);
     miliSeconds = current.getTimeInMillis();
     TimeZone tzCurrent = current.getTimeZone();
     int offset = tzCurrent.getRawOffset();
     if (tzCurrent.inDaylightTime(new Date())) {
         offset = offset + tzCurrent.getDSTSavings();
     }
     miliSeconds = miliSeconds - offset;
     resultdate = new Date(miliSeconds);
     System.out.println(sdf.format(resultdate));
}

 getGMTTime();
 getDate("Asia/Calcutta");
 final SimpleDateFormat formatter = new SimpleDateFormat("MMM dd yyyy hh:mm:ss a ");
 indTime.setText("Current Time " + String.valueOf(formatter.format(resultdate)) + TimeZoneName.substring(0,3));

 getGMTTime();
 getDate("Canada/Pacific");
 final SimpleDateFormat canadaFormatter = new SimpleDateFormat("MMM dd yyyy hh:mm:ss a ",Locale.CANADA);
 canadaTime.setText("Current Time " + String.valueOf(canadaFormatter.format(resultdate)) + TimeZoneName);
 canadaTime.setTypeface(titleType);

 getGMTTime();
 getDate("US/Pacific");
 final SimpleDateFormat usaFormatter = new SimpleDateFormat("MMM dd yyyy hh:mm:ss a ", Locale.US);
 usaTime.setText("Current Time " + String.valueOf(usaFormatter.format(resultdate)) + TimeZoneName);

我正确地获得了日期和时间,但没有获得时区

Ind     Mar 19 13:28:40 GMT+05:30 2016 

Canada  XXX XX XX:XX:XX GMT+05:30 2016

USA     XXX XX XX:XX:XX GMT+05:30 2016

你能告诉我我哪里错了吗?

提前致谢。

1 个答案:

答案 0 :(得分:0)

您可以按以下方式使用以打印时区

final SimpleDateFormat formatter = new SimpleDateFormat(“MMM dd yyyy hh:mm:ss z ”);