【问题标题】:reset time zone offset android重置时区偏移android
【发布时间】:2014-11-11 17:11:41
【问题描述】:

我有个约会,像

datewithGMT >>

Tue Oct 28 07:06:54 GMT+02:00 2014 

我想将其重置为

Tue Oct 28 07:06:54 2014

使用以下

   SimpleDateFormat df =    new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss");
   df.setTimeZone(TimeZone.getDefault());

    Date airDate = null;
    try {  
        airDate = df.parse(datewithGMT);   
    } catch (ParseException e) {  
        // TODO Auto-generated catch block  
        e.printStackTrace();  
    }

我再次获得 +2 GMT 的日期,如何删除这个偏移量

【问题讨论】:

  • 您能再解释一下吗?在您的示例中,您似乎刚刚从结尾删除了“2014”。
  • 使用 SimpleDateFormat

标签: android date utc


【解决方案1】:

您的模式与输入字符串不对应。您需要将df 更改为SimpleDateFormat df = new SimpleDateFormat("EE MMM dd HH:mm:ss z yyyy");

您还需要第二个 SimpleDateFormat。完成的实现如下:

        SimpleDateFormat df = new SimpleDateFormat("EE MMM dd HH:mm:ss z yyyy");
        df.setTimeZone(TimeZone.getDefault());
        SimpleDateFormat df2 = new SimpleDateFormat("EEE dd MMM HH:mm:ss yyyy");
        df2.setTimeZone(TimeZone.getDefault());

        String datewithGMT = "Tue Oct 28 07:06:54 GMT+02:00 2014";

        Date airDate = null;
        try {  
            airDate = df.parse(datewithGMT);   
            Log.v(df2.format(airDate)); //it will print Tue Oct 28 07:06:54 2014
        } catch (ParseException e) {  
            // TODO Auto-generated catch block  
            e.printStackTrace();  
        }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-08
    • 2014-03-27
    • 2018-03-01
    • 1970-01-01
    相关资源
    最近更新 更多