如何在android中设置特定日期和时间的时区?

kot*_*oti 6 timezone datetime android calendar

我开发了一个应用程序,因为我需要保存事件日期和时间。默认情况下,时间和日期采用“美国/芝加哥”时区格式。现在,我需要将它们转换为用户设备的时区格式。但我的格式错误。

我做了以下事情。

SimpleDateFormat curFormater1=new SimpleDateFormat("MM-dd-yyyy-hh:mm a");//10-23-2012-08:30 am

curFormater1.setTimeZone(TimeZone.getTimeZone("America/Chicago")); 

curFormater1.setTimeZone(TimeZone.getDefault());
Run Code Online (Sandbox Code Playgroud)

当前输出: TimeZone.getTimeZone("America/Chicago") 是 10-22-2012-10:00 PM TimeZone.getDefault() 是 10-23-2012-08:30 AM

所需输出 TimeZone.getTimeZone("America/Chicago") 是 10-23-2012-08:30 AM TimeZone.getDefault() 是 10-23-2012-07:00 PM

oik*_*opo 1

一些例子

在时区之间转换时间

转换时区之间的时间

      import java.util.Calendar;
       import java.util.GregorianCalendar;
       import java.util.TimeZone;

  public class TimeZoneExample {
      public static void main(String[] args) {

    // Create a calendar object and set it time based on the local
    // time zone

    Calendar localTime = Calendar.getInstance();
    localTime.set(Calendar.HOUR, 17);
    localTime.set(Calendar.MINUTE, 15);
    localTime.set(Calendar.SECOND, 20);

    int hour = localTime.get(Calendar.HOUR);
    int minute = localTime.get(Calendar.MINUTE);
    int second = localTime.get(Calendar.SECOND);


    // Print the local time

    System.out.printf("Local time  : %02d:%02d:%02d\n", hour, minute, second);


    // Create a calendar object for representing a Germany time zone. Then we
    // wet the time of the calendar with the value of the local time

    Calendar germanyTime = new GregorianCalendar(TimeZone.getTimeZone("Germany"));
    germanyTime.setTimeInMillis(localTime.getTimeInMillis());
    hour = germanyTime.get(Calendar.HOUR);
    minute = germanyTime.get(Calendar.MINUTE);
    second = germanyTime.get(Calendar.SECOND);


    // Print the local time in Germany time zone

    System.out.printf("Germany time: %02d:%02d:%02d\n", hour, minute, second);
}
}
Run Code Online (Sandbox Code Playgroud)