最近在做一个关于RSS解析器的项目,在解析rss源的时候遇到时间转换挡的问题。

FEED源通常有两种,一种是RSS源,另一种是ATOM源。
RSS源采用的时间格式是RFC822, ATOM源采用的是RFC3339

RFC8222格式:
Fri, 30 Mar 2012 05:14:40 +0000
后面的 +0000 代表的是时区

RFC3339格式:
2007-05-01T15:43:26.3452-07:00

代码如下:
import java.text.DateFormat; import java.text.SimpleDateFormat; import java.util.Date; import java.util.Locale; /** * @author Administrator -- Yinpeng * @connect: einpare@gmail.com
*
* 分别可以获取Date Time Week */ public class SaxDemo { public static Date parseRFC3339Date(String datestring) throws java.text.ParseException, IndexOutOfBoundsException { Date d = new Date(); // if there is no time zone, we don't need to do any special parsing. if (datestring.endsWith("Z")) { try { SimpleDateFormat s = new SimpleDateFormat( "yyyy-MM-dd'T'HH:mm:ss'Z'");// spec for RFC3339 d = s.parse(datestring); } catch (java.text.ParseException pe) {// try again with optional // decimals SimpleDateFormat s = new SimpleDateFormat( "yyyy-MM-dd'T'HH:mm:ss.SSSSSS'Z'");// spec for RFC3339 // (with fractional // seconds) s.setLenient(true); d = s.parse(datestring); } return d; } // step one, split off the timezone. String firstpart = datestring.substring(0, datestring.lastIndexOf('-')); String secondpart = datestring.substring(datestring.lastIndexOf('-')); // step two, remove the colon from the timezone offset secondpart = secondpart.substring(0, secondpart.indexOf(':')) + secondpart.substring(secondpart.indexOf(':') + 1); datestring = firstpart + secondpart; SimpleDateFormat s = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ");// spec // for // RFC3339 try { d = s.parse(datestring); } catch (java.text.ParseException pe) {// try again with optional // decimals s = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSSSSZ");// spec // for // RFC3339 // (with // fractional // seconds) s.setLenient(true); d = s.parse(datestring); } return d; } public static String getSimpleDateFormat(Date d) { SimpleDateFormat format = new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss EEE", Locale.CHINA); String result = format.format(d); return result; } public static String getDate(Date d) { SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd", Locale.CHINA); String result = format.format(d); return result; } public static String getTime(Date d) { SimpleDateFormat format = new SimpleDateFormat("HH:mm:ss", Locale.CHINA); String result = format.format(d); return result; } public static String getWeek(Date d) { SimpleDateFormat format = new SimpleDateFormat("EEE", Locale.CHINA); String result = format.format(d); return result; } public static String getSimpleDateFormat(String date) { String result = null; Date d = new Date(date); SimpleDateFormat dateFormat = new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss EEE", Locale.CHINA); result = dateFormat.format(d); return result; } public static String getDate(String date) { String result = null; Date d = new Date(date); SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd", Locale.CHINA); result = dateFormat.format(d); return result; } public static String getTime(String date) { String result = null; Date d = new Date(date); SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm:ss", Locale.CHINA); result = dateFormat.format(d); return result; } public static String getWeek(String date) { String result = null; Date d = new Date(date); SimpleDateFormat dateFormat = new SimpleDateFormat("EEE", Locale.CHINA); result = dateFormat.format(d); return result; } // some testing stuff in main() public static void main(String[] args) throws java.text.ParseException { String atom = "2012-03-15T10:30:41Z"; String rss = "Thu, 19 Apr 2012 09:59:23 +0000"; String current = atom; Date d = parseRFC3339Date(atom); System.out.println(getSimpleDateFormat(d)); System.out.println(getDate(d)); System.out.println(getTime(d)); System.out.println(getWeek(d)); System.out.println("<<<<<<<<<<<<<<<<<<<<<<<<"); System.out.println(getSimpleDateFormat(rss)); System.out.println(getDate(rss)); System.out.println(getTime(rss)); System.out.println(getWeek(rss)); } }

注: parseRFC3339Date 这个方法 来源于 http://cokere.com/RFC3339Date.txt 并非本人所写