[英]Converting String to Date error
我有这样的日期字符串值:星期五,2013年9月27日08: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我错在哪里? 谢谢您的帮助
试试这个完美
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");
格式应为EEE, dd-MMM-yyyy hh:mm:ss ZZZ
尝试这个
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);
这应该是你的格式。 EEE, dd-MMM-yyyy hh:mm:ss ZZZ
这是工作输出 。
尝试这个....
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;
}
这很有效。 只需根据GMT设定时间,它就会给你当地时间。
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");
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.