convert irregular dates
我得到了这样的不规则日期列表:
Thu, 29 Sep 2005 17:52:45 GMT
Wed, 17 Aug 2005 21:21:08 +0200
Wed, 17 Aug 2005 20:08:22 +0200
Mon, 15 Aug 2005 21:44:07 +0200
Sun, 24 Jul 2005 21:47:09 +0200
Sun, 24 Jul 2005 12:37:46 -0700 (PDT)
Sun, 24 Jul 2005 21:37:51 +0200
Mon, 11 Jul 2005 21:19:38 +0200
Mon, 11 Jul 2005 21:19:02 +0200
Mon, 11 Jul 2005 20:43:08 +0200 (CEST)
13 Nov 2006 14:06:20 +0000
如何以及我可以将它们转换为DateTime或只使用JodaTime或默认的Java日期类时间? (joda时间优先)。
Pangea当我阅读你的消息时我很累,我想发布解决方案并且我把它读回来,现在我意识到我可以通过尝试和无效返回或者尝试不同的模式来完成它。
无论如何,这是难以解决的问题:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 | String[] days = { "Mon","Tue","Wed","Thu","Fri","Sat","Sun" }; String[] months = { "Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec" }; String[] years = { "2004","2005","2006","2007","2008","2009","2010","2011" }; // ORDER IS VERRY IMPORTANT!! ( MEST must be before EST for example) String[] timesZones = { "BST","CET","CEST","CST","CDT","EDT","GMT+00:00","GMT","IST","MEST","EST","MET","MDT","PST","PDT","SAST","UTC","UT","W. Europe Standard Time","West-Europa (zomertijd)" }; DateTime handleDate(String date) { String origDate = date; String timeZone =""; String year =""; String month =""; String day =""; int hours = 0; int minutes = 0; int seconds = 0; // is it valid? date = trim(date); if (date.equals("")) { return null; } // first delete the comma that comes mostly after the day date = date.replaceAll(",",""); // remove the day for (int i = 0; i < days.length; i++) { if (date.contains(days[i])) { date = date.replace(days[i],""); break; } } // if(date.contains("23:27:17")) println(date); for (int i = 0; i < timesZones.length; i++) { // first check with '(' and ')' String target ="("+timesZones[i]+")"; if (date.contains(target)) { timeZone = timesZones[i]; date = date.replace(target,""); break; } // if not found check without '(' and ')' if (date.contains(timesZones[i])) { timeZone = timesZones[i]; date = date.replace(timesZones[i],""); break; } } // get the month for (int i = 0; i < months.length; i++) { if (date.contains(months[i])) { month = months[i].toLowerCase(); // !must be lowercase // must be dutch on my pc if(month.equals("oct")) month ="okt"; if(month.equals("may")) month ="mei"; if(month.equals("mar")) month ="mrt"; date = date.replace(months[i],""); break; } } // get the year for (int i = 0; i < years.length; i++) { if (date.contains(years[i])) { year = years[i]; date = date.replace(years[i],""); break; } } // get the time Pattern p = Pattern.compile("(\\d\\d):(\\d\\d):(\\d\\d)"); Matcher m = p.matcher(date); if (m.find()) { // also fix the time, 00 is not allowed hours = int(m.group(1)); minutes = int(m.group(2)); seconds = int(m.group(3)); date = date.replaceAll("(\\d\\d:\\d\\d:\\d\\d)",""); } // get the time difference date = date.replace("+-","+0"); // bug fix where data is incorrect ( 16 Sep 2007 23:27:17 +-200) p = Pattern.compile("[+|-]*(\\d\\d)\\d\\d"); m = p.matcher(date); if (m.find()) { int timeDifferenceH = int(m.group(1)); date = date.replaceAll("([+|-]*\\d\\d\\d\\d)",""); } date =""+date; // bug fix // get the day for (int i = 31; i >= 1; i--) { // first check for the ones that contains 2 digits (like 07) String d = nf(i, 2); if (date.contains(d)) { day = nf(i, 2); date = date.replace(d,""); break; } // check for 1 digit d =""+i; if (date.contains(d)) { day = nf(i, 2); date = date.replace(d,""); break; } } // there should be nothing left except white space date = date.replace("",""); if (date.equals("") == false) { println("handleDate: problem with input "+date); println(origDate+" "); println(year); println(month); println(day); } String cleanDate = day+"/"+month+"/"+year+""+nf(hours, 2)+":"+nf(minutes, 2)+":"+nf(seconds, 2); DateTimeFormatter formatter = DateTimeFormat.forPattern("dd/MMM/yyyy HH:mm:ss"); try { DateTime dt = formatter.parseDateTime(cleanDate); return dt; } catch (IllegalArgumentException iae) { println("handleDate: Problem with formatting:"+cleanDate); } return null; } |
我不知道伙伴如何用jodatime做,但你可以做的是使用
String pattern ="MM / dd / yyyy";
SimpleDateFormat format = new SimpleDateFormat(pattern);
其中每个都遵循某个DateFormat。 准备预定义的DateFormat处理程序的链(责任链模式),并将日期字符串传递给链。 让适当的处理程序解析日期并为您返回日期。
注意:这是假设您已发布的日期以字符串格式提供给您。 如果你有一个java.util.Date对象,那么我相信你所看到的是一种显示时间格式。