【问题标题】:How split date in this line? [duplicate]在这条线上如何分割日期? [复制]
【发布时间】:2016-03-01 11:10:46
【问题描述】:

我在解析字符串行中的一些信息时遇到了一些问题。在此示例中,我想使用 2011-08-28 19:02:30 行。 这个模板有很多行。如何解析这个日期?谢谢

 [47.611910999999999, -122.335178]  6   2011-08-28 19:02:30 I'm at Saigon Vietnamese Restaurant II (1529 6th Ave., at Pine St., Seattle) http://example.com

谢谢大家。这是我的解决方案。

 private Date parseDate(String line) {
    line = line.replaceAll("\\s+", " ").trim();
    String[] masWords = line.split(" ");
    DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    Date date = null;
   try {
        date = format.parse(masWords[3] + " " + masWords[4]);
    } catch (ParseException e) {
        e.printStackTrace();
    }
    return date;
}

【问题讨论】:

  • @sferencik 在我的示例中,有必要剪切字符串日期
  • @ЕгорУрбанович 搜索字符串拆分。

标签: java string parsing split simpledateformat


【解决方案1】:

我会这样做:

   String[] s1 = "[47.611910999999999, -122.335178]  6   2011-08-28 19:02:30 I'm at Saigon Vietnamese Restaurant II (1529 6th Ave., at Pine St., Seattle) http://t.co/8s86hNX".split(" ");
        DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        for(int i = 1; i< s1.length ; i++  ){
            try {
                Date date = format.parse(s1[i-1]+" "+s1[i]);
                System.out.println("Date found: "+s1[i-1]+" "+s1[i]);
                break;
            } catch (ParseException e) {
                continue;
            }
        }
  1. 拆分字符串
  2. 遍历字符串数组
  3. 直到找到有效日期为止。

如果你知道日期总是在字符串中的同一个位置,你可以通过强制转换来更容易地做到这一点:

String[] s1 = "[47.611910999999999, -122.335178]  6   2011-08-28 19:02:30 I'm at Saigon Vietnamese Restaurant II (1529 6th Ave., at Pine St., Seattle) http://t.co/8s86hNX".split(" ");
DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
format.parse(s1[6]+" "+s1[7]);

【讨论】:

    【解决方案2】:

    您需要通过删除多余的空格来规范化您的字符串...()Regex 可以快速轻松地做到这一点),然后拆分字符串将构建日期所需的字符串元素连接起来解析这些元素到 Date 对象 Woala...

    一个sn-p:

    String chain = "[47.611910999999999, -122.335178]  6   2011-08-28 19:02:30 I'm at Saigon Vietnamese Restaurant II (1529 6th Ave., at Pine St., Seattle) http://t.co/8s86hNX";
            chain = chain.replaceAll("\\s+", " ").trim();
            System.out.println(chain);
            String[] var = chain.split(" ");
            for (String string : var) {
                System.out.println(string);
            }
    
            String string = var[3] + " " + var[4];
            DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Date date = format.parse(string);
            System.out.println(date);
    

    【讨论】:

    • 好例子。谢谢)
    【解决方案3】:
    1. 以空格为分隔符分割String[] parts = str.split(" ");
    2. 循环通过parts[] 查找与您的日期和时间匹配的项目。
    3. 您可以使用以下正则表达式:
      • 日期:\d{4}-\d{2}-\d{2}
      • 时间:\d{2}:\d{2}:\d{2}
    4. 您可以使用java.util.Date 处理检测到的包含您的时间和日期的字符串

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-08-23
      • 1970-01-01
      • 2018-08-15
      • 1970-01-01
      相关资源
      最近更新 更多