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

4

3 回答 3

2

您快到了,您已经正确解析了日期,但是您依赖的是默认值toString()而不是您已经创建的格式。

最后一行试试这个:

lastUpdate.setText(curFormater.format(date));
于 2012-08-14T07:58:41.567 回答
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);
}
于 2012-08-14T08:08:25.730 回答
0

date -d 为此在 Unix 中工作。

于 2012-12-31T19:07:48.480 回答