【问题标题】:convert custom locale date time to gmt in android在android中将自定义语言环境日期时间转换为gmt
【发布时间】:2016-01-28 10:30:32
【问题描述】:

我正在尝试将在 android 设备上的语言环境时间创建的日期时间格式转换为 GMT 。我无法获得正确的值。此外,生成的 dateTime 是 android 上的本地时间。如何将其转换为精确的 GMT unixtime?

         String dateTime="02-02-2016 8:28 PM";
  SimpleDateFormat format = new SimpleDateFormat("dd-MM-yyyy hh:mm a");
            format.setTimeZone(TimeZone.getTimeZone("GMT"));
            Date date = null;
            try {
                date = format.parse(dateTime);
                System.out.println("Date ->" + date);
            } catch (Exception e) {
                e.printStackTrace();
            }
            long unixtime = date.getTime() / 1000;

【问题讨论】:

    标签: android


    【解决方案1】:
    try{
                        SimpleDateFormat sourceFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                        sourceFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
                        Log.v("Current time", ""+ sourceFormat.format(new Date()));
    
                        Date parsed = sourceFormat.parse(sourceFormat.format(new Date())); // => Date is in UTC now
    
    
                        TimeZone tz = TimeZone.getTimeZone("America/Los_Angeles");
                        SimpleDateFormat destFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                        destFormat.setTimeZone(tz);
    
    //                  String result = destFormat.format(parsed);
                        String result = destFormat.format(sourceFormat.parse(sourceFormat.format(new Date())));
    
                        Log.e("result", ""+result);
    
    
    //                  2015-11-26 02:07:59                 
                        SimpleDateFormat UTCFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                        SimpleDateFormat severFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                        SimpleDateFormat localFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    
                        UTCFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
    
                        TimeZone tz1 = TimeZone.getTimeZone("America/Los_Angeles");
                        severFormat.setTimeZone(tz1);
    
                        localFormat.setTimeZone(TimeZone.getDefault());
    
                        Date serverDate = severFormat.parse(result);
                        Date utcDate =  UTCFormat.parse(UTCFormat.format(serverDate));
    
                        String localDate = localFormat.format(utcDate);
                        Log.e("localDate", ""+localDate);
                    }catch(ParseException e){
                        e.printStackTrace();
                    } catch (java.text.ParseException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
    

    【讨论】:

    • 请转换我创建的字符串。我没有采用当前日期和时间,而且每个设备都有自己的时区。
    • 我认为你可以做到。有什么问题可以问我。
    • 以上解决方案已转换为特定时区和特定时区为本地日期
    • 我正在使用 int gmtOffset = TimeZone.getDefault().getRawOffset() + TimeZone.getDefault().getDSTSavings();和 long unixtime = (date.getTime() -(gmtOffset))/1000; Log.w("unixtime",unixtime+"");它现在有效。
    猜你喜欢
    • 1970-01-01
    • 2014-03-09
    • 1970-01-01
    • 2021-01-27
    • 2016-02-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多