2009-09-18 47 views
5

什麼是解析可在下列格式Java日期格式允許 - /或。如日期

"dd-MM-yyyy HH:mm" 
"dd/MM/yyyy HH:mm" 
"dd.MM.yyyy HH:mm" 

一個,而無需創建3個SimpleDateFormats和分析對每一個日期的最好方式中的分隔符。

感謝

回答

4

這可能比較容易「調整」的源字符串轉換爲規範的格式:

if (text.length() == 16) 
{ 
    if ((text.charAt(2) == '/' && text.charAt(5) == '/') || 
     (text.charAt(2) == '.' && text.charAt(5) == '.')) 
    { 
     text = text.substring(0, 2) + "-" + text.substring(3, 5) 
      + "-" + text.substring(6); 
    } 
} 

然後用使用格式字符串「 - 」。

注意,這是非常具體的,只有更換正是你感興趣的人物,以避免不必要的副作用。

+0

謝謝,我會開始對通用的日期解析器爲我們的代碼庫,這將是一個良好的開端 – Tarski 2009-09-18 09:51:52

5

你可以運行兩個第一替換操作,讓你減少這三種格式的單呢?

+0

啊..我因此未看到這一個發佈我的回答+1之前。大! – vpram86 2009-09-18 10:04:55

-3

ParseExact可以採取的形式的陣列。您仍然需要指定所有格式,但這只是一項操作。

+2

這是一個Java的問題,還沒有一個parseExact方法 – Tarski 2009-09-18 09:35:16

+0

這不是SimpleDateFormat的的方法,或標準用於Java方法的情況。你是否引用C#或類似? – 2009-09-18 09:36:45

+0

對不起,我的錯。是的,這就是C#。 – 2009-09-19 23:28:45

4

您可以使用Apache commons lang DateUtils.parseDate

import java.text.ParseException; 
import org.apache.commons.lang.time.DateUtils; 

public class Test { 

public static void main(String[] args) throws ParseException { 

    String[] values = new String[]{"31-12-2009 12:00", "31/12/2009 12:00", "31.12.2009 12:00"}; 
    String[] parsePatterns = new String[]{"dd-MM-yyyy HH:mm", "dd/MM/yyyy HH:mm", "dd.MM.yyyy HH:mm"}; 

    for (String value : values) { 
     System.out.println(DateUtils.parseDate(value, parsePatterns)); 
    } 
} 
} 

那麼,在內部它創建SimpleDateFormats,但什麼不好呢?

1

如何對正則表達式:

"\\d\\d[./-]\\d\\d[./-]\\d\\d\\d\\d \\d\\d:\\d\\d" 

在代碼中,這將意味着是這樣的:

Pattern pattern = 
Pattern.compile("(\\d\\d)([./-])(\\d\\d)([./-])(\\d\\d\\d\\d) (\\d\\d):(\\d\\d)"); 

Matcher matcher = 
pattern.matcher("31-07-1983 15:30"); 

if (matcher.find() && matcher.group(2).equals(matcher.group(4))) { 
    int day = Integer.parseInt(matcher.group(1)); 
    int month = Integer.parseInt(matcher.group(3)); 
    int year = Integer.parseInt(matcher.group(5)); 
    int hour = Integer.parseInt(matcher.group(6)); 
    int minute = Integer.parseInt(matcher.group(7)); 
} 
+0

因爲\是Java字符串文字的轉義字符,你必須加倍每個\:「\\ d \\ d [\\/\\ - ] \\ \\ d d [\\。\ \/\\ - ] \\ \\ d \\ d \\ d \\ d \\ d d:\\ \\ d d」 – Jesper 2009-09-18 09:49:47

+0

你錯了 - 你仍然需要寫 「在\\ d」 你Java源代碼,無論這是否是正則表達式。 – Jesper 2009-09-18 10:06:41

+0

現在我不得不去檢查一下,你是完全正確的先生。這真是醜陋,他們可以很容易地使用另一個逃生角色。 – NomeN 2009-09-18 10:23:12

0

自己用正則表達式做它:

public class SpecialDateFormat 
{ 
    private final static Pattern PATTERN = Pattern.compile("(\\d{2})[\\.\\/\\-](\\d{2})[\\.\\/\\-](\\d{4}) (\\d{2}):(\\d{2})"); 

    public static Date parse(String text) throws ParseException { 
     Matcher m = PATTERN.matcher(text); 
     if (m.matches()) { 
      int dd = Integer.parseInt(m.group(1)); 
      int mm = Integer.parseInt(m.group(2)); 
      int yy = Integer.parseInt(m.group(3)); 
      int hh = Integer.parseInt(m.group(4)); 
      int mi = Integer.parseInt(m.group(5)); 

      // NOTE: Checking if values are in valid ranges omitted 

      Calendar cal = Calendar.getInstance(); 
      cal.set(yy, mm - 1, dd, hh, mi, 0); 

      return cal.getTime(); 
     } 
     else { 
      throw new ParseException("Unparseable date: " + text, 0); 
     } 
    } 
} 

然而要注意這確實允許混合不同的分隔符,例如「17-09/2009 12:00」將被允許。

+0

我稱之爲公然竊取的概念;-),你會看到我已經在不同的分隔符檢查中工作了很多功夫。 – NomeN 2009-09-18 10:29:49

+0

而且顯然你不需要轉義[]之間的任何字符,甚至不需要這段時間。 – NomeN 2009-09-18 10:32:26