2012-08-14 42 views
0

日期格式"EEE, dd MMM yyyy HH:mm:ss Z",并且日期是更改时区没有改变的日期格式

String date = "Tue, 14 Aug 2012 07:26:33 +0000"; 

如何改变+0700时区没有改变的日期格式?

这是我尝试

String sDate = null; 
Date date = null; 
try { 
    sDate = "Tue, 14 Aug 2012 07:26:33 +0000"; 
    SimpleDateFormat curFormater = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss Z"); 
    curFormater.setTimeZone(TimeZone.getTimeZone("GMT")); 
    try { 
     date = curFormater.parse(sDate); 
    } catch (ParseException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 
} catch (DropboxException e) { 
    // TODO Auto-generated catch block 
    e.printStackTrace(); 
} 
lastUpdate.setText("Last Update : " + date.toString()); 

但结果是Tue Aug 14 14:26:33 ICT 2012

+2

解析日期,然后将它输出该格式 – 2012-08-14 07:37:36

+1

@therefromhere我的意思更改时区 – Sieryuu 2012-08-14 07:44:23

+0

是啊,这就是我的意思 - 解析日期,更改时区和输出。 – 2012-08-14 07:46:31

回答

2

你就要成功了,你已经正确地解析日期,但你依赖于默认toString()而不是你已经创建的格式。

试试这个最后一行:

lastUpdate.setText(curFormater.format(date)); 
+0

感谢您的帮助,'curFormater.setTimeZone(TimeZone.getDefault());',在我将时区设置为默认值后,它的工作原理 – Sieryuu 2012-08-14 08:05:06

+0

啊是的,我在看格式而不是TZ的东西。所以这将输出在你的环境设置使用的任何TZ中。 – 2012-08-14 08:07:22

0

另外,如果你想添加的小时手册号,你可以做这样的事情:

static final Long oneHourInMilliseconds = new Long(3600000); 
static final DateFormat d = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss Z"); 

public static Long addHours(Date time, int hours) 
{ 
    return time.getTime() + (hours * oneHourInMilliseconds); 
} 

public static void main(String[] args) throws ParseException 
{ 
    String dateAsString = "Tue, 14 Aug 2012 07:26:33 +0000"; 
    Date before = d.parse(dateAsString); 
    Date after = new Date(addHours(before, 7)); 
    System.out.println("Before: " + before + ". After: " + after); 
} 
+0

感谢您的建议 – Sieryuu 2012-08-14 08:13:41

0

日期-d在Unix上为此工作。