Android - 将 GMT 时间转换为本地时间

Android - Convert GMT time to Locale time

我正在尝试将 UTC 时间转换为用户的区域设置时间。但是,我得到了相同的 UTC 时间。 显然,将时区设置为 locale/default 不起作用。

另一种方法似乎可以使用 Instant,但需要 API 级别 26。

这是我在字符串中输入的日期:“2020-01-16T19:44:48.303+0000”。 我希望日期和时间采用以下格式:"M/dd/yy - h:mm aa"

private String toLocaleTime(String timeStr){
    // Date date;
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'+0000'");
        simpleDateFormat.setTimeZone(TimeZone.getDefault());

        String localeTime = "";
        try {
            //date = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss", Locale.US).parse(timeStr);
            localeTime = simpleDateFormat.format(simpleDateFormat.parse(timeStr));

        } catch (ParseException e) {
            e.printStackTrace();
        }
        //String str = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss", Locale.US).format(date);
        Log.d("LocaleTime", ""+ localeTime);
        return localeTime;
}

您告诉 parsing 日期格式化程序使用 UTC 时区进行解析,告诉 formatting 日期格式化程序使用默认时间进行格式化区域(不告诉它任何东西):

String input = "2020-01-16T19:44:48.303+0000";

SimpleDateFormat inputFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'+0000'");
inputFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
Date date = inputFormat.parse(input);

SimpleDateFormat outputFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS");
// Not needed:  outputFormat.setTimeZone(TimeZone.getDefault());
String output = outputFormat.format(date);

System.out.println("input:  " + input);
System.out.println("output: " + output);
System.out.println(new SimpleDateFormat("zzzz  XX").format(date));

输出

input:  2020-01-16T19:44:48.303+0000
output: 2020-01-16T14:44:48.303
Eastern Standard Time  -0500

java.time

A Locale 与日期时间对象的内容无关。区域设置仅在生成文本以表示日期时间对象的值时出现。我怀疑您的意图是从时-分-秒的 UTC 偏移量调整为特定地区(时区)人们使用的挂钟时间。

切勿使用 SimoleDateFormatDateCalendar。这些可怕的日期时间 类 已在 java.time 年前被取代。对于 26 之前的 Android,ThreeTenABP 添加到您的项目中。

This is my input date in string: "2020-01-16T19:44:48.303+0000"

提示:如果在偏移量中的小时和秒之间包含冒号字符,java.time 类 和其他日期时间框架工作得更好:+00:00

如果您的所有输入都具有相同的偏移量,请替换字符串的那部分。

String input = "2020-01-16T19:44:48.303+0000".replace( "+0000" , "+00:00" ) ;

解析为 Instant,UTC 时间。

Instant instant = Instant.parse( input ) ;

生成所需格式的字符串。你真的想要一位数的月份和两位数的日子吗?一位数小时和两位数分钟?

DateTimeFormatter f = DateTimeFormatter.ofPattern( "M/dd/uu - h:mm a" ).withLocale( locale ) ;
ZoneId z = ZoneId.of( "Africa/Tunis" ) ;
String output = instant.atZone( z ).format( z ) ;

我建议您改为让 java.time 使用 DateTimeFormatter.ofLocalizedDateTime.

自动为您本地化

关于java.time

java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as java.util.Date, Calendar, & SimpleDateFormat.

要了解更多信息,请参阅 Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310

Joda-Time project, now in maintenance mode, advises migration to the java.time 类.

您可以直接与数据库交换 java.time 对象。使用 JDBC driver compliant with JDBC 4.2 或更高版本。不需要字符串,不需要 java.sql.* 类.

在哪里获取java.time类?