【问题标题】:how to convert UTC date to locale GMT time on android如何在android上将UTC日期转换为区域设置GMT时间
【发布时间】:2016-04-26 09:14:56
【问题描述】:

我有这个日期字符串: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,我想这是不正确的。

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

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

【问题讨论】:

标签: android kotlin


【解决方案1】:

试试这个

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

【讨论】:

    【解决方案2】:

    问题是在 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'"吗?
    【解决方案3】:

    试试这个。

    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);
    

    【讨论】:

      【解决方案4】:
          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));
      

      【讨论】:

        【解决方案5】:

        试试这个,替换格式

        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;
        }
        

        【讨论】:

          猜你喜欢
          • 2011-08-29
          • 2020-06-11
          • 2018-02-04
          • 2021-05-02
          • 2022-12-17
          • 2018-07-28
          • 1970-01-01
          • 1970-01-01
          • 2019-03-24
          相关资源
          最近更新 更多