我正在使用DOM API阅读rss feed。 作为pubDate iamreciving日期的一部分,这种格式
Sun, 01 Nov 2015 06:27:09 +0000
我需要将日期转换为此格式
2015-11-01 06:27
这是我试过的
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class Test {
public static void main(String[] args) throws ParseException {
String today = convertdate("Sun, 01 Nov 2015 06:27:09 +0000");
System.out.println(today);
}
public static String convertdate(String recivieddate) throws ParseException {
SimpleDateFormat in = new SimpleDateFormat("E,dd MM yy HH:mm:ss");
Date date = in.parse(recivieddate);
SimpleDateFormat out = new SimpleDateFormat("yyyy-MM-dd HH:mm");
String newdate = out.format(date);
return newdate.toString();
}
}
但我得到的例外是
Exception in thread "main" java.text.ParseException: Unparseable date: "Sun, 01 Nov 2015 06:27:09 +0000"
at java.text.DateFormat.parse(Unknown Source)
at Test.convertdate(Test.java:13)
at Test.main(Test.java:8)
答案 0 :(得分:2)
您应该使用E,dd MMM yy HH:mm:ss
来解析
试试这个
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class test {
public static void main(String[] args) throws ParseException {
String today = convertdate("Sun, 01 Nov 2015 06:27:09 +0000");
System.out.println(today);
}
public static String convertdate(String recivieddate) throws ParseException {
SimpleDateFormat in = new SimpleDateFormat("E,dd MMM yy HH:mm:ss");
Date date = in.parse(recivieddate);
SimpleDateFormat out = new SimpleDateFormat("yyyy-MM-dd HH:mm");
String newdate = out.format(date);
return newdate.toString();
}
}
输出 2015-11-01 06:27
答案 1 :(得分:0)
试试这个...
package com.paytm.pg.common;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class Test {
public static void main(String[] args) throws ParseException {
String today = convertdate("Sun, 01 Nov 2015 06:27:09 +0000");
System.out.println(today);
}
public static String convertdate(String recivieddate) throws ParseException {
SimpleDateFormat in = new SimpleDateFormat("E, dd MMM yyyy HH:mm:ss");
Date date = in.parse(recivieddate);
SimpleDateFormat out = new SimpleDateFormat("yyyy-MM-dd HH:mm");
String newdate = out.format(date);
return newdate.toString();
}
}
答案 2 :(得分:0)
如果您不介意使用库,则可以在不指定格式的情况下解析日期。
import org.pojava.datetime.DateTime;
import java.util.TimeZone;
public static void main(String[] args) {
TimeZone utc=TimeZone.getTimeZone("UTC");
String formatted=DateTime.parse("Sun, 01 Nov 2015 06:27:09 +0000")
.toString("yyyy-MM-dd HH:mm", utc));
// 2015-11-01 06:27
}