java - Convert String to date (complex timezone) -



java - Convert String to date (complex timezone) -

i have irregular dates like:

mon, 9 jan 2012 14:18:32 -0800 (gmt)

18 oct 2006 00:32:03 -0000

sat, 18 nov 2006 19:52:23 ut

i made complex function done, works purpose of learning want 1 time again using seek , catch. question is, how can deal timezone? can see timezone's in function, have sometime (... timezone illustration , don't want store cases in string array.

also want utilize joda-time, (it can localdate or datetime, doesn't matter much me).

public localdate handledate(string date) { 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", "2012" }; // order verry important!! ( mest must before est 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)" }; string origdate = date; string timezone = ""; string year = ""; string month = ""; string day = ""; int hours = 0; int minutes = 0; int seconds = 0; // valid? date = trim(date); if (date.equals("")) { homecoming null; } // first delete comma comes after day date = date.replaceall(",", ""); // remove day (int = 0; < days.length; i++) { if (date.contains(days[i])) { date = date.replace(days[i], ""); break; } } // if(date.contains("23:27:17")) println(date); (int = 0; < timeszones.length; i++) { // first check '(' , ')' string target = "(" + timeszones[i] + ")"; if (date.contains(target)) { timezone = timeszones[i]; date = date.replace(target, ""); break; } // if not found check without '(' , ')' if (date.contains(timeszones[i])) { timezone = timeszones[i]; date = date.replace(timeszones[i], ""); break; } } // month (int = 0; < months.length; i++) { if (date.contains(months[i])) { month = months[i].tolowercase(); // !must lowercase // must dutch on 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; } } // year (int = 0; < years.length; i++) { if (date.contains(years[i])) { year = years[i]; date = date.replace(years[i], ""); break; } } // time pattern p = pattern.compile("(\\d\\d):(\\d\\d):(\\d\\d)"); matcher m = p.matcher(date); if (m.find()) { // prepare time, 00 not allowed hours = integer.parseint(m.group(1)); minutes = integer.parseint(m.group(2)); seconds = integer.parseint(m.group(3)); date = date.replaceall("(\\d\\d:\\d\\d:\\d\\d)", ""); } // time difference date = date.replace("+-", "+0"); // bug prepare info wrong ( 16 // sep 2007 23:27:17 +-200) p = pattern.compile("[+|-]*(\\d\\d)\\d\\d"); m = p.matcher(date); if (m.find()) { int timedifferenceh = integer.parseint(m.group(1)); date = date.replaceall("([+|-]*\\d\\d\\d\\d)", ""); } date = " " + date; // bug prepare // day (int = 31; >= 1; i--) { // first check ones contains 2 digits (like 07) string d = nf(i, 2); if (date.contains(d)) { day = nf(i, 2); date = date.replace(d, ""); break; } // check 1 digit d = "" + i; if (date.contains(d)) { day = nf(i, 2); date = date.replace(d, ""); break; } } // there should nil left except white space date = date.replace(" ", ""); if (date.equals("") == false) { println("handledate: problem input\n" + date); println(origdate + "\n"); 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"); string cleandate = year + "-" + month + "-" + day; datetimeformatter formatter = datetimeformat.forpattern("yyyy-mmm-dd"); seek { // datetime dt = formatter.parsedatetime(cleandate); // localdate dt = new localdate(cleandate); // homecoming dt.tolocaldate(); // homecoming dt; // homecoming new localdate().parse(cleandate, formatter); homecoming formatter.parselocaldate(cleandate); } grab (illegalargumentexception iae) { println("handledate: problem formatting: " + cleandate); } homecoming null; }

the datetimeformatter class in jodatime quite strong , flexible in this, should seek custom formatters or freaky formatters

java string datetime jodatime

Comments

Popular posts from this blog

How do I check if an insert was successful with MySQLdb in Python? -

delphi - blogger via idHTTP : error 400 bad request -

postgresql - ERROR: operator is not unique: unknown + unknown -