2013-09-27 119 views
2

我有日期字符串值是这样的:周五,27月 - 2013 8时29分59秒GMT ,我需要,将其转换日期格式 我想是这样的:转换字符串到日期错误

private Date modifyDateLayout(String inputDate) { 
     DateFormat format = new SimpleDateFormat ("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"); 
     format.setTimeZone (TimeZone.getTimeZone ("GMT")); 
     Date d = null; 
     try { 
     d = format.parse(inputDate); 
     } catch (ParseException e) { 
      e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates. 
     } 
     return d; 
    } 

但它没有工作,和e = null 我在哪里错了? 感谢您的帮助

+0

1)尝试DateFormat format = new SimpleDateFormat(“yyyy-MM-dd HH:mm:ss.SSS'Z'”);没有'T'并在设置日期模板之前设置时区。 2)也许传入的字符串是无效的 可能是这个http://java67.blogspot.com/2013/01/how-to-format-date-in-java-simpledateformat-example.html帮助你 – 2013-09-27 08:34:32

+0

第一个解决方案不工作太...字符串是有效的,我从调试复制字符串问题 – Vitaliy

回答

1

格式应该是EEE, dd-MMM-yyyy hh:mm:ss ZZZ

+0

此解决方案不起作用:( – Vitaliy

1

试试这个

DateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ"); 
System.out.println(format.format(now)); 
String s = format.format(now); 
String result = s.substring(0, 26) + ":" + s.substring(27); 
System.out.println("Result: "+result); 
1

这应该是您的格式。 EEE, dd-MMM-yyyy hh:mm:ss ZZZ

这是working output

+0

我不明白...我复制了你所有的代码(即使与字符串常量)和钢不工作! – Vitaliy

+0

你得到的错误是什么?! – SudoRahul

+0

in catch e = null!:o – Vitaliy

1

试试这个....

private Date modifyDateLayout(String inputDate) { 
    DateFormat format = new SimpleDateFormat("EEE, dd-MMM-yyyy HH:mm:ss z"); 
    format.setTimeZone(TimeZone.getTimeZone("GMT")); 
    Date d = null; 
    try { 
     d = format.parse(inputDate); 
    } catch (ParseException e) { 
     e.printStackTrace(); // To change body of catch statement use File | 
           // Settings | File Templates. 
    } 
    return d; 
} 
+0

@Vitaliy如果它有帮助,然后接受为答案 – sandy

2

试试这个完美的作品

private Date modifyDateLayout(String inputDate) { 
     SimpleDateFormat format = new SimpleDateFormat("EEE, dd-MMM-yyyy hh:mm:ss"); 
     format.setTimeZone(TimeZone.getDefault()); 
     Date d = null; 
     try { 
      d = format.parse(inputDate); 
     } catch (ParseException e) { 
      e.printStackTrace(); 
     } 
     return d; 
    } 

呼叫

modifyDateLayout("Fri, 27-Sep-2013 08:29:59 GMT"); 
+0

谢谢!我不知道原因,但钢返回null。即使是你的答案完整副本,我无法理解这一点......感谢您的帮助! – Vitaliy

1

这是工作。根据格林尼治标准时间设置你的时间,它会给你你当地的时间。

import java.text.DateFormat; 
import java.text.ParseException; 
import java.text.SimpleDateFormat; 
import java.util.Date; 
import java.util.TimeZone; 


public class Time { 
    private Date modifyDateLayout(String inputDate) { 
     DateFormat format = new SimpleDateFormat ("EEE, dd-MMM-yyyy hh:mm:ss zzz"); 
     format.setTimeZone (TimeZone.getTimeZone ("GMT")); 

     Date d = null; 
     try { 
     d = format.parse(inputDate); 
     System.out.println(d); 
     } catch (ParseException e) { 
      e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates. 
     } 
     return d; 
    } 
    public static void main(String[] args) { 
     //new Time().modifyDateLayout("Fri, 27-Sep-2013 08:29:59 GMT"); 
     new Time().modifyDateLayout("Fri, 17-Apr-2015 15:45:59 GMT"); 
    } 
} 
+1

@Vitality您在第二行中犯了错误DateFormat fo rmat = new SimpleDateFormat(“yyyy-MM-dd'T'HH:mm:ss.SSS'Z'”); –