1 17 package com.sun.syndication.io.impl; 18 19 import java.text.DateFormat ; 20 import java.text.SimpleDateFormat ; 21 import java.util.Date ; 22 import java.util.TimeZone ; 23 24 36 public class DateParser { 37 38 private static final String [] RFC822_MASKS = { 42 "EEE, dd MMM yyyy HH:mm:ss z", 43 "EEE, dd MMM yyyy HH:mm z", 44 "EEE, dd MMM yy HH:mm:ss z", 45 "EEE, dd MMM yy HH:mm z", 46 "dd MMM yyyy HH:mm:ss z", 47 "dd MMM yyyy HH:mm z", 48 "dd MMM yy HH:mm:ss z", 49 "dd MMM yy HH:mm z" 50 }; 51 52 private static final String [] W3CDATETIME_MASKS = { 56 "yyyy-MM-dd'T'HH:mm:ssz", 57 "yyyy-MM-dd'T'HH:mmz", 58 "yyyy-MM-dd", 59 "yyyy-MM", 60 "yyyy" 61 }; 62 63 66 private DateParser() { 67 } 68 69 81 private static Date parseUsingMask(String [] masks,String sDate) { 82 Date d = null; 83 for (int i=0;d==null && i<masks.length;i++) { 84 DateFormat df = new SimpleDateFormat (masks[i]); 85 df.setLenient(true); 86 try { 87 d = df.parse(sDate); 88 } 90 catch (Exception ex1) { 91 } 93 } 94 return d; 95 } 96 97 119 public static Date parseRFC822(String sDate) { 120 return parseUsingMask(RFC822_MASKS,sDate); 121 } 122 123 124 143 public static Date parseW3CDateTime(String sDate) { 144 int tIndex = sDate.indexOf("T"); 147 if (tIndex>-1) { 148 if (sDate.endsWith("Z")) { 149 sDate = sDate.substring(0,sDate.length()-1)+"+00:00"; 150 } 151 int tzdIndex = sDate.indexOf("+",tIndex); 152 if (tzdIndex==-1) { 153 tzdIndex = sDate.indexOf("-",tIndex); 154 } 155 if (tzdIndex>-1) { 156 String pre = sDate.substring(0,tzdIndex); 157 int secFraction = pre.indexOf(","); 158 if (secFraction>-1) { 159 pre = pre.substring(0,secFraction); 160 } 161 String post = sDate.substring(tzdIndex); 162 sDate = pre + "GMT" + post; 163 } 164 } 165 return parseUsingMask(W3CDATETIME_MASKS,sDate); 166 } 167 168 178 public static String formatRFC822(Date date) { 179 SimpleDateFormat dateFormater = new SimpleDateFormat ("EEE, dd MMM yyyy HH:mm:ss 'GMT'"); 180 dateFormater.setTimeZone(TimeZone.getTimeZone("GMT")); 181 return dateFormater.format(date); 182 } 183 184 194 public static String formatW3CDateTime(Date date) { 195 SimpleDateFormat dateFormater = new SimpleDateFormat ("yyyy-MM-dd'T'HH:mm:ss'Z'"); 196 dateFormater.setTimeZone(TimeZone.getTimeZone("GMT")); 197 return dateFormater.format(date); 198 } 199 200 } 201 | Popular Tags |