大学IT网 - 最懂大学生的IT学习网站! QQ资料交流群:367606806
当前位置:大学IT网 > Java技巧 > java正则匹配解析出时间

java正则匹配解析出时间


[摘要]本文主要是对java正则匹配解析出时间的讲解,希望对大家学习java正则匹配解析出时间有所帮助。

  java正则匹配解析出时间

  package zhengze;

  import java.text.ParseException;

  import java.text.SimpleDateFormat;

  import java.util.regex.*;

  import java.util.Date;

  public class zhengze {

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

  // String path =

  // "/user/sunflower/flume/msp/0/1970-01-01/08/vortex-pro.hadoop0038.bj.voicecloud.cn";

  // String reg =

  // "^\\d{4}-0[1-9]1[1-2]-0[1-9]|[1-2]\\d|3[0-1] [0-2][0-4]:[0-6]\\d:[0-6]\\d";

  // String checkValue = "2009-01-01/12";

  // String reg = "^\\d{4}-[0-1]\\d-[0-3]\\d/[0-2][0-4]$";

  //

  // String reg =

  // "^\\d{4}-[0-1][1-9]-[0-3][1-9] [0-2][0-4]:[0-6]\\d:[0-6]\\d$";

  //

  // Pattern p = Pattern.compile(reg);

  // Matcher m = p.matcher(checkValue);

  // boolean b = m.matches();

  // if(b)

  // {

  //

  // System.out.println("格式正确");

  // }

  // else

  // {

  // System.out.println("格式错误");

  // }

  String path = "/string/string/string/2013-09-11/11/string/string";

  //    Pattern pattern = Pattern.compile("\\d{4}-[0-1]\\d-[0-3]\\d/[0-2]\\d");

  //    Matcher matcher = pattern.matcher(path);

  //

  //    if (matcher.find()) {

  //      String src = matcher.group();

  //      System.out.println(src);

  //      SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd/hh");

  //      Date date = sdf.parse(src);

  //      System.out.println(date);

  //

  //      long t = date.getTime() + 4200*1000;

  //

  //      System.out.println(t);

  //

  //    }

  Pattern pattern = Pattern.compile("\\d{4}-[0-1]\\d-[0-3]\\d/[0-2]\\d");

  Matcher matcher = pattern.matcher(path);

  Date datefile = null;

  if (matcher.find()) {

  String src = matcher.group();

  System.out.println(src);

  SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd/hh");

  try {

  datefile = df.parse(src);

  } catch (ParseException e) {

  // TODO Auto-generated catch block

  e.printStackTrace();

  }

  }

  long readyTime = datefile.getTime() + 4200*1000;

  System.out.println(datefile);

  System.out.println(datefile.getTime());

  System.out.println(readyTime);

  Date d = new Date(readyTime);

  System.out.println(d.toString());

  }

  }



相关评论