时区无法正常工作



我正在获取印度,加拿大,美国的日期和时间。我正确获取时间和日期,但是我得到三个国家的时区相同(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

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

提前谢谢。

您可以通过以下方式打印时区

final SimpleDateFormat

格式化程序 = new SimpleDateFormat("MMM dd yyyy hh:mm:ss z ");

相关内容

  • 没有找到相关文章

最新更新