parse this type of date format in java?
在java中解析这个日期格式的最简单方法是什么?:
1 | 2010-09-18T10:00:00.000+01:00 |
我在java中读取了DateFormat api,找不到一个方法,它接受一个字符串甚至这种类型的日期格式作为解析的参数? 当我的意思是通过解析我的意思是我想将"日期(日月和年)","时间"和"时区"提取到单独的字符串对象中。
提前致谢
另一个答案,因为你似乎专注于简单地撕开
他们提出的验证8601替代品的正则表达式是:
1 2 3 4 | ^([\+-]?\d{4}(?!\d{2}\b))((-?)((0[1-9]|1[0-2])(\3([12]\d|0[1-9]|3[01]))?|W([0-4]\d|5[0-2]) (-?[1-7])?|(00[1-9]|0[1-9]\d|[12]\d{2}|3([0-5]\d|6[1-6])))([T\s]((([01]\d|2[0-3]) ((:?)[0-5]\d)?|24\:?00)([\.,]\d+(?!:))?)?(\17[0-5]\d([\.,]\d+)?)? ([zZ]|([\+-])([01]\d|2[0-3]):?([0-5]\d)?)?)?)?$ |
弄清楚如何梳理出正确的捕捉群体会让我感到恍惚。不过,以下内容适用于您的具体情况:
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 | import java.util.regex.Matcher; import java.util.regex.Pattern; public class Regex8601 { static final Pattern r8601 = Pattern.compile("(\\d{4})-(\\d{2})-(\\d{2})T((\\d{2}):"+ "(\\d{2}):(\\d{2})\\.(\\d{3}))((\\+|-)(\\d{2}):(\\d{2}))"); //2010-09-18T10:00:00.000+01:00 public static void main(String[] args) { String thisdate ="2010-09-18T10:00:00.000+01:00"; Matcher m = r8601.matcher(thisdate); if (m.lookingAt()) { System.out.println("Year:"+m.group(1)); System.out.println("Month:"+m.group(2)); System.out.println("Day:"+m.group(3)); System.out.println("Time:"+m.group(4)); System.out.println("Timezone:"+m.group(9)); } else { System.out.println("no match"); } } } |
如果您正在做日期和时间非常重要的事情,建议使用JodaTime。请参阅此广泛的SO讨论,包括ISO8601。另请参阅"我应该使用本机数据/时间......"。
这是一个示例代码片段,取自此示例,如果您要使用JDK
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 | // 2004-06-14T19:GMT20:30Z // 2004-06-20T06:GMT22:01Z // http://www.cl.cam.ac.uk/~mgk25/iso-time.html // // http://www.intertwingly.net/wiki/pie/DateTime // // http://www.w3.org/TR/NOTE-datetime // // Different standards may need different levels of granularity in the date and // time, so this profile defines six levels. Standards that reference this // profile should specify one or more of these granularities. If a given // standard allows more than one granularity, it should specify the meaning of // the dates and times with reduced precision, for example, the result of // comparing two dates with different precisions. // The formats are as follows. Exactly the components shown here must be // present, with exactly this punctuation. Note that the"T" appears literally // in the string, to indicate the beginning of the time element, as specified in // ISO 8601. // Year: // YYYY (eg 1997) // Year and month: // YYYY-MM (eg 1997-07) // Complete date: // YYYY-MM-DD (eg 1997-07-16) // Complete date plus hours and minutes: // YYYY-MM-DDThh:mmTZD (eg 1997-07-16T19:20+01:00) // Complete date plus hours, minutes and seconds: // YYYY-MM-DDThh:mm:ssTZD (eg 1997-07-16T19:20:30+01:00) // Complete date plus hours, minutes, seconds and a decimal fraction of a // second // YYYY-MM-DDThh:mm:ss.sTZD (eg 1997-07-16T19:20:30.45+01:00) // where: // YYYY = four-digit year // MM = two-digit month (01=January, etc.) // DD = two-digit day of month (01 through 31) // hh = two digits of hour (00 through 23) (am/pm NOT allowed) // mm = two digits of minute (00 through 59) // ss = two digits of second (00 through 59) // s = one or more digits representing a decimal fraction of a second // TZD = time zone designator (Z or +hh:mm or -hh:mm) public static Date parse( String input ) throws java.text.ParseException { //NOTE: SimpleDateFormat uses GMT[-+]hh:mm for the TZ which breaks //things a bit. Before we go on we have to repair this. SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssz" ); //this is zero time so we need to add that TZ indicator for if ( input.endsWith("Z" ) ) { input = input.substring( 0, input.length() - 1) +"GMT-00:00"; } else { int inset = 6; String s0 = input.substring( 0, input.length() - inset ); String s1 = input.substring( input.length() - inset, input.length() ); input = s0 +"GMT" + s1; } return df.parse( input ); } |
此日期采用ISO 8601格式。这是一个特定于此格式的解析器的链接,该解析器在内部使用Java SimpleDateFormat解析API。
你应该使用SimpleDateFormat,
在此处和此处查找示例以开始使用。
如果您使用的是Java 7或更早版本,可以参考这篇文章。
如果您使用的是Java 8,则可以:
1 2 3 4 5 |
最简单的解决方案是:
1 2 3 4 5 6 7 8 | Date date = null; SimpleDateFormat isoFormatWithMillis = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSXXX"); try { date = isoFormatWithMillis.parse("2010-09-18T10:00:00.000+01:00"); } catch (ParseException e) { e.printStackTrace(); } System.out.println(date); |
这将打印
您可以使用
使用SimpleDateFormat,模式为
更新SimpleDateFormat不适用于ISO 8601日期格式。而是使用JodaTime代替。它提供符合ISO 8601的ISOChronology。
可以在SO上找到简要示例。