2012-01-25 10 views
0

私のような非常に不規則な日付があります。変換文字列(複雑なタイムゾーン)

月、2012年1月9日夜二時18分32秒-0800(GMT)

2006年10月18日夜12時32を:03 -0000

土、2006年11月18日午後7時52分23秒UT

私はそれをすべて、それが動作する、成し遂げることは本当に複雑な関数を作ったがために学習の目的私はtryとcatchを使ってもう一度やりたい 私の質問は、タイムゾーンをどうすれば処理できますか?あなたは私の関数内のすべてのタイムゾーンを見ることができますが、私はいつか(例えばタイムゾーンとして...例えば文字列配列にすべてのケースを格納したくないのですが)

また、私はJoda-Time、 (それは私にはそれほど重要ではありません、LOCALDATEまたはdateTimeのことができます)。

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 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)" }; 

     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 = 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)", ""); 
     } 

     // 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 = Integer.parseInt(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\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"); 

     try { 

      // DateTime dt = formatter.parseDateTime(cleanDate); 
      // LocalDate dt = new LocalDate(cleanDate); 
      // return dt.toLocalDate(); 
      // return dt; 
      // return new LocalDate().parse(cleanDate, formatter); 
      return formatter.parseLocalDate(cleanDate); 
     } catch (IllegalArgumentException iae) { 
      println("handleDate: Problem with formatting: " + cleanDate); 
     } 

     return null; 
    } 
+0

Freaky Formattersはあなたに知っています試してみてくださいSimpleDateFormat.parse()や[Apache Commons Validators](http://commons.apache.org/validator/apidocs/org/apache/commons)のようなものです。 /validator/routines/package-summary.html#date)? –

+0

さまざまな入力形式の問題に対する魔法の答えはありません。あなたは、例外をスローしないものが見つかるまで、さまざまなフォーマッタを使って解析しなければなりません。あなたは間違いなくJoda-TimeやJava 8の新しいjava.timeでそれをしたいと思っています。古いjava.util.Dateと.CalendarとSimpleDateFormatは非常に面倒ですので避けるべきです。日や月の名前のために様々な人間の言語を扱うには、Joda-Timeでjava.util.Localeを使用することに注意してください。そのような単語をハードコードする必要はありません。 –

答えて

2

JodaTimeでてDateTimeFormatterクラスは、この中には非常に強く、柔軟である、あなたはCustom Formattersまたは