现在的时间格式是:
2011-11-25T13:42:00+08:00
我想把它转换为yyyy-MM-dd hh:mm:ss,该怎么处理呢? /**
 * String转换为时间
 * @param str
 * @return
 */
public static Date ParseDate(String str){
SimpleDateFormat dateFormat =new SimpleDateFormat("yyyy/MM/dd hh:mm:ss"); 
Date addTime = null;
try {
addTime = dateFormat.parse(str);
} catch (ParseException e) {
e.printStackTrace();
}
return addTime;
}
我现在的是上面的方法,但是会报错,得不到结果。

解决方案 »

  1.   

    格式是这样: 2011-11-25T13:42:00+08:00
    为何不直接使用date型呢?
      

  2.   

    会报不支持的转换错误。
    这是一种GMT时间格式,RSS格式都是用这种格式
    这种格式会在不同的国家显示成不同,比如在中国是2011-11-28T17:59:50+08:00
    在美国可能就是2011-11-28T23:59:50-08:00
    我想把它转换成本地时间。
      

  3.   


    public class Test {
    public static void main(String[] args){
    try {
    System.out.println(new Test().getString());
    System.out.println(new Test().getValue());
    } catch (Exception e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    }

    public String getString() throws ParseException{
    String time="2011-11-25T13:42:00+08:00";
    int plusIndex=time.indexOf("+");
    String date=time.substring(0, plusIndex);
    String timeZone=time.substring(plusIndex);
    int columnIndex=timeZone.indexOf(":");
    String hour=timeZone.substring(0,columnIndex);
    String minute=timeZone.substring(columnIndex+1);
    DateFormat format=new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
    Date temp=format.parse(date);
    long tempValue=temp.getTime();
    String timezone = "GMT" + hour + ":" + minute;        // 计算字符串中的时区与当前时区的时间差
            long current_milli = System.currentTimeMillis();
            TimeZone zone = TimeZone.getTimeZone(timezone);
            long date_gmt_offset = zone.getOffset(current_milli);
            long local_gmt_offset = TimeZone.getDefault().getOffset(current_milli);        long offset_to_local = local_gmt_offset - date_gmt_offset;
            long last=tempValue+offset_to_local;
            DateFormat secondFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    return secondFormat.format(last);
    }

    public String getValue() throws ParseException{
    String time="2011-11-25T13:42:00+08:00";
    int plusIndex=time.indexOf("+");
    String date=time.substring(0, plusIndex);
    String timeZone=time.substring(plusIndex);
    timeZone=timeZone.replace(":", "");
    time=date+timeZone;
    DateFormat first=new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ");
    Date temp=first.parse(time);
    DateFormat second=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    return second.format(temp);
    }
    }以上是两种方法,因为琢磨了好多正则表达式,结果试了好几个,没成功,所以只能退而求其次了,采用了分割字符串的方法,更大家更正
      

  4.   

    dateFormat.parse是将字符串按固定格式解析
    dateFormat.format是将时间按固定格式转为字符串输出
    所以SimpleDateFormat dateFormat =new SimpleDateFormat("yyyy/MM/dd hh:mm:ss"); 
    应该改为SimpleDateFormat dateFormat =new SimpleDateFormat("yyyy-MM-dd hh:mm:ss"); 
    以匹配你的时间字符串。
    不过("yyyy-MM-dd hh:mm:ss"); 我可能写错了,中间还有个T呢