在java中解析这种类型的日期格式? [英] parse this type of date format in java?

查看:128
本文介绍了在java中解析这种类型的日期格式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  2010-09-18T10:00 

在java中解析此日期格式最简单的方法是什么? :00.000 + 01:00

我在java中读取DateFormat api,找不到需要的方法一个字符串甚至这种类型的日期格式作为pararemeter来解析?当我的意思是解析我的意思是我想提取日期(日月和年),时间和时区到单独的字符串对象。



谢谢提前

解决方案

另一个答案,因为你似乎专注于简单地撕裂 String 分开(不是一个好主意,IMHO。)让我们假设字符串是有效的ISO8601。你能假定它总是是你的引用形式,还是只是有效的8601?如果后者,你必须应付一堆场景,如这些人做了



他们想出验证8601替代品的正则表达式是:

  ^([\ +  - ]?\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 +(? 0-5] \d([\。,] \d +)?)?
([zZ] |([\ + - ])([01] \d | 2 [ 3]):?([0-5] \d)?)?)?)?$



<解决如何挑出正确的捕捉组,让我很惬意。然而,以下内容将适用于您的具体情况:

  import java.util.regex.Matcher; 
import java.util.regex.Pattern;


public class Regex8601
{
static final Pattern r8601 = Pattern.compile((\\\dd {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);
}
}
}


what would be the easiest way to parse this date format below in java?:

2010-09-18T10:00:00.000+01:00

i read the DateFormat api in java and could not find a method that takes a string or even this type of date format as a paremeter to parse? When i mean by parse i mean i want to extract the "date (day month and year)", "time" and "timezone" into seperate string objects.

Thanks in advance

解决方案

Another answer, since you seem to be focused on simply tearing the String apart (not a good idea, IMHO.) Let's assume the string is valid ISO8601. Can you assume it will always be in the form you cite, or is it just valid 8601? If the latter, you have to cope with a bunch of scenarios as these guys did.

The regex they came up with to validate 8601 alternatives is:

^([\+-]?\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)?)?)?)?$ 

Figuring out how to tease out the correct capture groups makes me woozy. Nevertheless, the following will work for your specific case:

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

这篇关于在java中解析这种类型的日期格式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆