如何将UTC日期转换为Android本地GMT时间

10

我有这个日期字符串:2016-04-26T09:14:10.477Z,它是在UTC时区的。我想将其转换为用户本地时区的时间,因此如果是GMT +02:00,我希望得到一个值为2016-04-26T11:14:10.477Z的日期(注意9变为了11)。

到目前为止,我一直在使用以下方法:

val sdf = SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS", Locale.getDefault())
val now = Date()

val limit = sdf.parse(coupon.usedAt)

这里的limit = Tue Apr 26 09:14:10 GMT+02:00 2016是错误的,我想。

那么该如何正确转换它呢?谢谢!

编辑:我的问题与这个问题不同,因为我需要使用SimpleDateFormat对象而不是Date对象。


您可以将时间戳除以1000来将UTC转换为GMT。 - Naitik
可能是如何在Java中获取当前的UTC或GMT日期和时间?的重复问题。 - T D Nguyen
试试这段代码。 点击此处查看所有日期和时间格式 - Amitsharma
5个回答

14

试一试

SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
simpleDateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
Date myDate = simpleDateFormat.parse(rawQuestion.getString("AskDateTime"));

8

这个问题涉及到Kotlin,但我发现所有的答案都是用Java写的,所以这里提供一个Kotlin解决方案

val formatter = SimpleDateFormat("yyyy-MM-dd HH:mm:ssZ", Locale.getDefault())
formatter.timeZone = TimeZone.getTimeZone("UTC")
val result = formatter.parse(dateAsString)

为了让这个正常工作,格式化字符串不应该是“yyyy-MM-dd'T'HH:mm:ss'Z'”吗? - Richard Le Mesurier

0

试试这个。

String dateString = "04/26/2016 10:22:00 AM";
    SimpleDateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy hh:mm:ss aa");
    Date convertedDate = new Date();
    try {
        convertedDate = dateFormat.parse(dateString);
    } catch (ParseException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    Calendar calendar = Calendar.getInstance();
    calendar.setTimeZone(TimeZone.getTimeZone("UTC"));
    calendar.setTime(convertedDate);
    Date time = calendar.getTime();
    SimpleDateFormat outputFmt = new SimpleDateFormat("MMM dd, yyy h:mm a zz");
    String utcTime = outputFmt.format(time);

0
    DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSXXX");
    Date date = sdf.parse("2016-04-26T09:14:10.477Z"); 
    sdf.setTimeZone(TimeZone.getTimeZone("IST"));
    System.out.println(sdf.format(date));

0
尝试一下,替换格式。
public static final String DATE_FORMAT       = "yyyy-MM-dd'T'HH:mm:ss'Z'";

public static String getFormattedLocalTimeFromUtc (String utcTimeStamp, String outputFormat) {

   String formattedTime = null;
   if (!TextUtils.isEmpty (utcTimeStamp)) {
     if (utcTimeStamp.contains ("T")) {

        String localTime = null;
        SimpleDateFormat sdf = new SimpleDateFormat (DATE_FORMAT, Locale.getDefault ());
        sdf.setTimeZone (TimeZone.getTimeZone ("UTC"));
        try {
            localTime = sdf.parse (utcTimeStamp).toString ();
        }
        catch (ParseException e) {
            e.printStackTrace ();
        }

        DateFormat inputDateFormat = new SimpleDateFormat ("EEE MMM dd HH:mm:ss z yyyy");
        inputDateFormat.setTimeZone (TimeZone.getTimeZone ("UTC"));
        Date date = null;
        try {
            date = inputDateFormat.parse (localTime);
        }
        catch (ParseException e) {
            e.printStackTrace ();
        }

        DateFormat outputDateFormat = new SimpleDateFormat (outputFormat);
        formattedTime = outputDateFormat.format (date);
     }
   }
   return formattedTime;
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接