实现一个简单的中文时间解析器

参考源代码如下:

final static String[] ft1 = { "前年", "去年", "今年", "明年", "后年", "上个月", "上一月",
   "上月", "本月", "下月", "下个月", "今晚", "明早", "明晚", "前天", "昨天", "今天", "明天",
   "后天", "凌晨", "早上", "早晨", "上午", "中午", "下午", "傍晚", "晚上", "白天", "周一",
   "周二", "周三", "周四", "周五", "周六", "周日", "周末", "星期一", "星期二", "星期三",
   "星期四", "星期五", "星期六", "星期日", "星期天", "五一", "十一" };

 final static String[] ft2 = { "年", "月", "日", "号", "时", "点", "分", "秒", "-",
   "/", ":" };

 final static String[] ft3 = { "左右", "前后", "以前", "以后", "之后", "之前" };

 final static String[] ft4 = { "到", "至" };

 

/**
  * 是否是日期或时间格式的字符串
  *
  * @param date
  */
 public static boolean isDateTime(String date) {
  if (date != null) {
   int index = -1;
   if (date == null)
    return false;
   for (String s3 : ft3) {
    index = date.indexOf(s3);
    if (index > 1)
     break;
   }

   if (index > 0)
    date = date.substring(0, index);

   // 类似明天/后天/明天下午3点以后等的时间
   String day1 = null;
   for (int i = 0; i < date.length(); i++) {
    for (String s : ft1) {
     if (date.indexOf(s) == 0) {
      if (date.length() > s.length()) {
       day1 = date.substring(0, s.length());
       date = date.substring(s.length());
       i = 0;
       break;
      } else {
       if (day1 == null
         || date.equals("白天")
         || day1.indexOf("天") > 0
         && ((date.indexOf("天") == -1)
           && date.indexOf("月") == -1 && date
           .indexOf("年") == -1))
        return true;
      }
     }
    }
   }
   // 明天下午4点左右

   date = GFString.treatChineseNumber(date, ft2);
   String t = null;
   boolean flag = false;
   for (int i = 0; i < date.length(); i++) {
    if (i < date.length() - 1)
     t = date.substring(i, i + 1);
    else
     t = date.substring(i);
    if (GFString.isNumeric(t))
     continue;
    if ("年".equals(t)) {
     flag = true;
     for (int j = 1; j < ft2.length; j++) {
      if (date.indexOf(ft2[j]) != -1
        && date.indexOf(ft2[j]) < i)
       return false;
     }
    } else if ("月".equals(t)) {
     flag = true;
     for (int j = 2; j < ft2.length; j++) {
      if (date.indexOf(ft2[j]) != -1
        && date.indexOf(ft2[j]) < i)
       return false;
     }
    } else if ("日".equals(t)) {
     flag = true;
     for (int j = 4; j < ft2.length; j++) {
      if (date.indexOf(ft2[j]) != -1
        && date.indexOf(ft2[j]) < i)
       return false;
     }
    } else if ("号".equals(t)) {
     flag = true;
     for (int j = 4; j < ft2.length; j++) {
      if (date.indexOf(ft2[j]) != -1
        && date.indexOf(ft2[j]) < i)
       return false;
     }
    } else if ("时".equals(t)) {
     flag = true;
     for (int j = 6; j < ft2.length; j++) {
      if (date.indexOf(ft2[j]) != -1
        && date.indexOf(ft2[j]) < i)
       return false;
     }
    } else if ("点".equals(t)) {
     flag = true;
     for (int j = 6; j < ft2.length; j++) {
      if (date.indexOf(ft2[j]) != -1
        && date.indexOf(ft2[j]) < i)
       return false;
     }
    } else if ("分".equals(t)) {
     flag = true;
     for (int j = 7; j < ft2.length; j++) {
      if (date.indexOf(ft2[j]) != -1
        && date.indexOf(ft2[j]) < i)
       return false;
     }
    } else if ("秒".equals(t)) {
     flag = true;
     for (int j = 8; j < ft2.length; j++) {
      if (date.indexOf(ft2[j]) != -1
        && date.indexOf(ft2[j]) < i)
       return false;
     }
    } else if (GFString.isAlphanumeric(t))
     return false;
   }

   if (!flag) {
    try {
     DateFormat dateformat = DateFormat.getDateTimeInstance();
     Date d = dateformat.parse(date);
     System.out.println(dateformat.format(d));
     if (d != null)
      return true;
    } catch (ParseException e) {
     // e.printStackTrace();
    }
    try {

     DateFormat dateformat2 = DateFormat.getDateInstance();
     Date d2 = dateformat2.parse(date);
     System.out.println(dateformat2.format(d2));
     if (d2 != null)
      return true;
    } catch (ParseException e) {
     // e.printStackTrace();
    }

    try {

     DateFormat dateformat3 = DateFormat.getTimeInstance();
     Date d3 = dateformat3.parse(date);
     System.out.println(dateformat3.format(d3));
     if (d3 != null)
      return true;
    } catch (ParseException e) {
     // e.printStackTrace();
    }

   } else
    return true;

  }
  return false;
 }

/**
  * <pre>
  *    解析字符串当中的时间段
  *                                              
  *   1.大前年/前年/去年/今年/明年/后年/大后年
  *   2.上个月/上一月/上月/本月/下月/下一月/下个月
  *   3.大前天/前天/昨天/今天/明天/后天/大后天
  *   4.凌晨/早上/早晨/上午/中午/下午/傍晚/晚上/半夜/
  *   5.2006年/二零零六年/2006年7月/2006年7月8号/2006年7月8日/二零零六年七月八号
  *   6.7点54/7点54分/七点五十分/7点40分8秒
  *   7.12:20/20:30:42
  *   8.2006-4-5/2006/4/5/
  *   9.8点左右/以前/以后
  * </pre>
  *
  * @param str
  * @return
  */
 public static Calendar[] parseTimeSeg(String msg) {
  Calendar[] cals = null;

  if (msg != null && msg.length() > 1) {
   String stime = null;
   String etime = null;
   msg = GFString.treatChineseNumber(msg, ft2);

   // 进行原子分词,把字符串分成一个个字
   String[] atoms = new String[msg.length()];
   for (int i = 0; i < msg.length(); i++) {
    if (i < msg.length() - 1)
     atoms[i] = msg.substring(i, i + 1);
    else
     atoms[i] = msg.substring(i);
   }

   String tk = "";
   String tk2 = null;
   String tk3 = null;
   for (int i = 0; i < atoms.length; i++) {
    for (int j = i; j < atoms.length; j++) {
     if (isTimeKey(atoms[j])) {
      tk += atoms[j];
      if (j < atoms.length - 1)
       tk2 = atoms[j + 1];
      else
       tk2 = null;

      if (!isTimeKey(tk2) && isDateTime(tk)) {
       tk3 = splitTime(tk);
       if (tk3 != null) {
        if (stime == null)
         stime = tk3;
        else if (etime == null
          && stime.indexOf("/t3") == stime
            .lastIndexOf("/t3"))
         etime = tk3;

       }
       i = j + 1;
       tk = "";
      } else if (!isTimeKey(tk2))
       tk = "";
     }
    }

   }

   System.out.println("stime:" + stime);
   System.out.println("etime:" + etime);
   cals = new Calendar[2];
   Calendar cal1 = Calendar.getInstance();
   Calendar cal2 = Calendar.getInstance();
   cal2.set(2100, 0, 0, 0, 0, 0);
   int[][] rs1 = null;
   int[][] rs2 = null;

   if (stime != null) {
    int index1 = stime.indexOf("/t3");
    int index2 = stime.lastIndexOf("/t3");
    int index3 = stime.indexOf("/t4");

    if (index1 > 1 && index1 == index2) {
     if ("左右".equals(stime.substring(index1 - 2, index1))
       || "前后".equals(stime.substring(index1 - 2, index1))) {
      rs1 = parseTime(stime);
      if (rs1[0][0] == 0)
       rs1[0][0] = cal1.get(Calendar.YEAR);
      if (rs1[0][1] == 0)
       rs1[0][1] = cal1.get(Calendar.MONTH);
      if (rs1[0][3] == 0 && rs1[0][2] == 0)
       rs1[0][3] = cal1.get(Calendar.HOUR_OF_DAY);
      if (rs1[0][4] == 0 && rs1[0][2] == 0 && rs1[0][3] == 0)
       rs1[0][4] = cal1.get(Calendar.MINUTE);
      if (rs1[0][2] == 0)
       rs1[0][2] = cal1.get(Calendar.DAY_OF_MONTH);
      if (rs1[1][0] == 0)
       rs1[1][0] = rs1[0][0];
      if (rs1[1][1] == 0)
       rs1[1][1] = rs1[0][1];
      if (rs1[1][2] == 0)
       rs1[1][2] = rs1[0][2];
      if (rs1[1][3] == 0)
       rs1[1][3] = rs1[0][3];
      if (rs1[1][4] == 0)
       rs1[1][4] = rs1[0][4];
      cal1.set(rs1[0][0], rs1[0][1], rs1[0][2], rs1[0][3],
        rs1[0][4], rs1[0][5]);
      cal2.set(rs1[1][0], rs1[1][1], rs1[1][2], rs1[1][3],
        rs1[1][4], rs1[1][5]);
     } else if ("以前".equals(stime.substring(index1 - 2, index1))
       || "之前".equals(stime.substring(index1 - 2, index1))) {
      rs1 = parseTime(stime.substring(0, index1 - 3));
      if (rs1[0][0] == 0)
       rs1[0][0] = cal1.get(Calendar.YEAR);
      if (rs1[0][1] == 0)
       rs1[0][1] = cal1.get(Calendar.MONTH);
      if (rs1[0][3] == 0 && rs1[0][2] == 0)
       rs1[0][3] = cal1.get(Calendar.HOUR_OF_DAY);
      if (rs1[0][4] == 0 && rs1[0][2] == 0 && rs1[0][3] == 0)
       rs1[0][4] = cal1.get(Calendar.MINUTE);
      if (rs1[0][2] == 0)
       rs1[0][2] = cal1.get(Calendar.DAY_OF_MONTH);
      cal1.set(cal1.get(Calendar.YEAR), cal1
        .get(Calendar.MONTH), cal1
        .get(Calendar.DAY_OF_MONTH), cal1
        .get(Calendar.HOUR_OF_DAY), cal1
        .get(Calendar.MINUTE), cal1
        .get(Calendar.SECOND));
      cal2.set(rs1[0][0], rs1[0][1], rs1[0][2], rs1[0][3],
        rs1[0][4], rs1[0][5]);
     } else if ("以后".equals(stime.substring(index1 - 2, index1))
       || "之后".equals(stime.substring(index1 - 2, index1))) {
      rs1 = parseTime(stime.substring(0, index1 - 3));
      if (rs1[0][0] == 0)
       rs1[0][0] = cal1.get(Calendar.YEAR);
      if (rs1[0][1] == 0)
       rs1[0][1] = cal1.get(Calendar.MONTH);
      if (rs1[0][3] == 0 && rs1[0][2] == 0)
       rs1[0][3] = cal1.get(Calendar.HOUR_OF_DAY);
      if (rs1[0][4] == 0 && rs1[0][2] == 0 && rs1[0][3] == 0)
       rs1[0][4] = cal1.get(Calendar.MINUTE);
      if (rs1[0][2] == 0)
       rs1[0][2] = cal1.get(Calendar.DAY_OF_MONTH);
      cal1.set(rs1[0][0], rs1[0][1], rs1[0][2], rs1[0][3],
        rs1[0][4], rs1[0][5]);
      cal2.set(rs1[0][0], rs1[0][1], 31, 23, 59, 0);
     }
    } else if (index1 > 1 && index2 > index1) {
     rs1 = parseTime(stime.substring(0, index1 - 3));
     rs2 = parseTime(stime.substring(index1 + 4, index2 - 3));
     if (rs1[0][0] == 0)
      rs1[0][0] = cal1.get(Calendar.YEAR);
     if (rs1[0][1] == 0)
      rs1[0][1] = cal1.get(Calendar.MONTH);
     if (rs2[0][0] == 0)
      rs2[0][0] = rs1[0][0];
     if (rs2[0][1] == 0)
      rs2[0][1] = rs1[0][1];
     if (rs2[0][2] == 0)
      rs2[0][2] = rs1[0][2];
     if (rs2[0][3] == 0)
      rs2[0][3] = rs1[0][3];
     if (rs1[0][3] > 12 && rs2[0][3] <= 12)
      rs2[0][3] += 12;
     cal1.set(rs1[0][0], rs1[0][1], rs1[0][2], rs1[0][3],
       rs1[0][4], rs1[0][5]);
     cal2.set(rs2[0][0], rs2[0][1], rs2[0][2], rs2[0][3],
       rs2[0][4], rs2[0][5]);

    } else if (index3 > 1) {
     rs1 = parseTime(stime.substring(0, index3 - 2));
     rs2 = parseTime(stime.substring(index3 + 3));
     rs1 = parseTime(stime.substring(0, index3 - 2));
     rs2 = parseTime(stime.substring(index3 + 3));
     if (rs1[0][0] == 0)
      rs1[0][0] = cal1.get(Calendar.YEAR);
     if (rs1[0][1] == 0)
      rs1[0][1] = cal1.get(Calendar.MONTH);
     if (rs1[0][3] == 0 && rs1[0][2] == 0)
      rs1[0][3] = cal1.get(Calendar.HOUR_OF_DAY);
     if (rs1[0][4] == 0 && rs1[0][2] == 0 && rs1[0][3] == 0)
      rs1[0][4] = cal1.get(Calendar.MINUTE);
     if (rs1[0][2] == 0)
      rs1[0][2] = cal1.get(Calendar.DAY_OF_MONTH);
     if (rs2[0][0] == 0)
      rs2[0][0] = rs1[0][0];
     if (rs2[0][1] == 0)
      rs2[0][1] = rs1[0][1];
     if (rs2[0][2] == 0)
      rs2[0][2] = rs1[0][2];
     if (rs2[0][3] == 0)
      rs2[0][3] = rs1[0][3];
     cal1.set(rs1[0][0], rs1[0][1], rs1[0][2], rs1[0][3],
       rs1[0][4], rs1[0][5]);
     cal2.set(rs2[0][0], rs2[0][1], rs2[0][2], rs2[0][3],
       rs2[0][4], rs2[0][5]);

    } else {
     rs1 = parseTime(stime);
     if (rs1[0][0] == 0)
      rs1[0][0] = cal1.get(Calendar.YEAR);
     if (rs1[0][1] == 0)
      rs1[0][1] = cal1.get(Calendar.MONTH);
     if (rs1[0][3] == 0 && rs1[0][2] == 0)
      rs1[0][3] = cal1.get(Calendar.HOUR_OF_DAY);
     if (rs1[0][4] == 0 && rs1[0][2] == 0 && rs1[0][3] == 0)
      rs1[0][4] = cal1.get(Calendar.MINUTE);
     if (rs1[0][2] == 0)
      rs1[0][2] = cal1.get(Calendar.DAY_OF_MONTH);

     if (etime != null) {
      rs2 = parseTime(etime);
      if (rs2[0][0] == 0)
       rs2[0][0] = rs1[0][0];
      if (rs2[0][1] == 0)
       rs2[0][1] = rs1[0][1];
      if (rs2[0][2] == 0)
       rs2[0][2] = rs1[0][2];
      if (rs2[0][3] == 0)
       rs2[0][3] = rs1[0][3];
      cal1.set(rs1[0][0], rs1[0][1], rs1[0][2], rs1[0][3],
        rs1[0][4], rs1[0][5]);
      cal2.set(rs2[0][0], rs2[0][1], rs2[0][2], rs2[0][3],
        rs2[0][4], rs2[0][5]);
     } else {
      if (rs1[1][0] == 0)
       rs1[1][0] = rs1[0][0];
      if (rs1[1][1] == 0)
       rs1[1][1] = rs1[0][1];
      if (rs1[1][2] == 0)
       rs1[1][2] = rs1[0][2];
      if (rs1[1][3] == 0)
       rs1[1][3] = rs1[0][3];
      if (rs1[1][4] == 0)
       rs1[1][4] = rs1[0][4];
      cal1.set(rs1[0][0], rs1[0][1], rs1[0][2], rs1[0][3],
        rs1[0][4], rs1[0][5]);
      cal2.set(rs1[1][0], rs1[1][1], rs1[1][2], rs1[1][3],
        rs1[1][4], rs1[1][5]);
     }
    }
   }
   cals[0] = cal1;
   cals[1] = cal2;
   System.out.println("cal1:" + cdate(cal1, "yyyy-mm-dd hh24:mi:ss"));
   System.out.println("cal2:" + cdate(cal2, "yyyy-mm-dd hh24:mi:ss"));
  }

  return cals;
 }

 private static int[][] parseTime(String stime) {
  int[][] rs = null;
  int year1 = 0;
  int month1 = 0;
  int day1 = 0;
  int hour1 = 0;
  int minute1 = 0;
  int year2 = 0;
  int month2 = 0;
  int day2 = 0;
  int hour2 = 0;
  int minute2 = 0;
  String t = null;
  Calendar cal1 = Calendar.getInstance();
  if (stime != null) {
   rs = new int[2][6];
   String[] ts = stime.split(" ");
   for (int k = 0; k < ts.length; k++) {
    t = ts[k];
    if (t.indexOf("/t1") > 1) {

     if (t.indexOf("今天") == 0) {
      day1 = cal1.get(Calendar.DAY_OF_MONTH);
      hour2 = 23;
      minute2 = 59;

     } else if (t.indexOf("明天") == 0) {
      day1 = cal1.get(Calendar.DAY_OF_MONTH) + 1;
      hour2 = 23;
      minute2 = 59;

     } else if (t.indexOf("后天") == 0) {
      day1 = cal1.get(Calendar.DAY_OF_MONTH) + 2;
      hour2 = 23;
      minute2 = 59;

     } else if (t.indexOf("大后天") == 0) {
      month1 = cal1.get(Calendar.MONTH);
      day1 = cal1.get(Calendar.DAY_OF_MONTH) + 3;
      hour2 = 23;
      minute2 = 59;

     } else if (t.indexOf("周一") == 0 || t.indexOf("星期一") == 0) {

      day1 = getThisWeekDay(1);
      hour2 = 23;
      minute2 = 59;

     } else if (t.indexOf("周二") == 0 || t.indexOf("星期二") == 0) {

      day1 = getThisWeekDay(2);
      hour2 = 23;
      minute2 = 59;

     } else if (t.indexOf("周三") == 0 || t.indexOf("星期三") == 0) {
      day1 = getThisWeekDay(3);
      hour2 = 23;
      minute2 = 59;

     } else if (t.indexOf("周四") == 0 || t.indexOf("星期四") == 0) {
      day1 = getThisWeekDay(4);
      hour2 = 23;
      minute2 = 59;

     } else if (t.indexOf("周五") == 0 || t.indexOf("星期五") == 0) {
      day1 = getThisWeekDay(5);
      hour2 = 23;
      minute2 = 59;

     } else if (t.indexOf("周六") == 0 || t.indexOf("星期六") == 0) {
      day1 = getThisWeekDay(6);
      hour2 = 23;
      minute2 = 59;

     } else if (t.indexOf("周日") == 0 || t.indexOf("星期天") == 0
       || t.indexOf("星期日") == 0) {
      day1 = getThisWeekDay(0);
      hour2 = 23;
      minute2 = 59;
     } else if (t.indexOf("周末") == 0) {
      day1 = getThisWeekDay(6);
      day2 = day1 + 1;
      hour2 = 23;
      minute2 = 59;
     } else if (t.indexOf("五一") == 0) {
      month1 = 5;
      day1 = 1;
      day2 = day1 + 6;
      hour2 = 23;
      minute2 = 59;
     } else if (t.indexOf("十一") == 0) {
      month1 = 10;
      day1 = 1;
      day2 = day1 + 6;
      hour2 = 23;
      minute2 = 59;
     } else if (t.indexOf("早上") == 0 || t.indexOf("早晨") == 0) {
      hour1 = 4;
      hour2 = 8;
     } else if (t.indexOf("早上") == 0 || t.indexOf("早晨") == 0) {
      hour1 = 4;
      hour2 = 8;
     } else if (t.indexOf("上午") == 0) {
      hour1 = 8;
      hour2 = 12;
     } else if (t.indexOf("中午") == 0) {
      hour1 = 11;
      hour2 = 13;
     } else if (t.indexOf("下午") == 0) {
      hour1 = 13;
      hour2 = 18;
     } else if (t.indexOf("晚上") == 0) {
      hour1 = 18;
      hour2 = 23;
      minute2 = 59;
     } else if (t.indexOf("傍晚") == 0) {
      hour1 = 18;
      hour2 = 20;
     } else if (t.indexOf("白天") == 0) {
      hour1 = 6;
      hour2 = 23;
      minute2 = 59;
     } else if (t.indexOf("今晚") == 0) {
      year1 = cal1.get(Calendar.YEAR);
      month1 = cal1.get(Calendar.MONTH);
      day1 = cal1.get(Calendar.DAY_OF_MONTH);
      day2 = cal1.get(Calendar.DAY_OF_MONTH);
      hour1 = 18;
      hour2 = 20;
     } else if (t.indexOf("明晚") == 0) {
      year1 = cal1.get(Calendar.YEAR);
      month1 = cal1.get(Calendar.MONTH);
      day1 = cal1.get(Calendar.DAY_OF_MONTH) + 1;
      day2 = cal1.get(Calendar.DAY_OF_MONTH) + 1;
      hour1 = 18;
      hour2 = 24;
     }

    } else if (t.indexOf("/t3") > 1) {
     if (k > 0) {
      if (t.indexOf("左右") == 0 || t.indexOf("前后") == 0) {
       if (minute1 != 0) {
        minute2 = minute1 + 30;
        minute1 -= 30;
       } else if (hour1 != 0) {
        hour2 = hour1 + 1;
        hour1 -= 1;
       } else if (day1 != 0) {
        day2 = day1 + 1;
        day1 -= 1;
       }
      }
     }
    } else {
     for (int i = 0, j = i; i < t.length(); i++) {
      String s = null;
      if (i != t.length() - 1)
       s = t.substring(i, i + 1);
      else
       s = t.substring(i);
      if (GFString.isGeneralNumber(s))
       continue;
      else {
       if (s.equals("年") && i > j + 1)
        year1 = GFString.chinaNum2arebNum(t.substring(
          j, i));
       else if (s.equals("月") && i > j)
        month1 = GFString.chinaNum2arebNum(t.substring(
          j, i));
       else if ((s.equals("日") || s.equals("号")) && i > j)
        day1 = GFString.chinaNum2arebNum(t.substring(j,
          i));
       else if ((s.equals("时") || s.equals("点")) && i > j) {

        int th = GFString.chinaNum2arebNum(t.substring(
          j, i));
        int tm = 0;
        int m = i + 1;
        if (m < t.length()) {
         for (; m < t.length(); m++) {
          String s1 = null;
          if (m < t.length() - 1)
           s1 = t.substring(m, m + 1);
          else
           s1 = t.substring(m);
          if (!GFString.isGeneralNumber(s1))
           break;
         }

         if (m == t.length())
          tm = GFString.chinaNum2arebNum(t
            .substring(i + 1));

         else
          tm = GFString.chinaNum2arebNum(t
            .substring(i + 1, m));
        }

        hour1 = th;
        minute1 = tm;

        if (k > 0
          && (ts[k - 1].indexOf("下午") == 0 || ts[k - 1]
            .indexOf("晚上") == 0)) {
         if (hour1 != 0)
          hour1 += 12;
        }

       }

       else if (s.equals("分") && i > j)
        minute1 = GFString.chinaNum2arebNum(t
          .substring(j, i));

       j = i;
       j++;
      }
     }
    }
   }
   if (month1 > 0)
    month1 -= 1;
   if (month2 > 0)
    month2 -= 1;
   rs[0][0] = year1;
   rs[0][1] = month1;
   rs[0][2] = day1;
   rs[0][3] = hour1;
   rs[0][4] = minute1;
   rs[0][5] = 0;
   rs[1][0] = year2;
   rs[1][1] = month2;
   rs[1][2] = day2;
   rs[1][3] = hour2;
   rs[1][4] = minute2;
   rs[1][5] = 0;
  }

  return rs;
 }

 private static boolean isTimeKey(String str) {
  if (str != null) {
   if (GFString.isGeneralNumber(str))
    return true;
   for (String s : ft1) {
    if (s.indexOf(str) != -1)
     return true;
   }

   for (String s : ft2) {
    if (s.indexOf(str) != -1)
     return true;
   }

   for (String s : ft3) {
    if (s.indexOf(str) != -1)
     return true;
   }
   for (String s : ft4) {
    if (s.indexOf(str) != -1)
     return true;
   }
  }

  return false;
 }

 /**
  * <pre>
  *                                把时间字符进行分隔.
  *                               
  *                                比如:明天下午3点以后8点之前
  *                                分隔后:明天/t1 下午/t1 3点 以后/t3 8点 之前/t3
  * </pre>
  *
  * @param st
  * @return
  */
 private static String splitTime(String st) {
  if (st != null) {
   int index = -1;
   for (String s : ft1) {
    for (int i = 0; i < st.length(); i++) {
     index = st.indexOf(s, i);
     if (index != -1) {
      st = st.substring(0, index) + " " + s + "/t1 "
        + st.substring(index + s.length());
      i = index + s.length() - 1;
     }
    }
   }

   for (String s : ft3) {
    index = st.indexOf(s);
    if (index != -1) {
     st = st.substring(0, index) + " " + s + "/t3 "
       + st.substring(index + s.length());
    }
   }

   for (String s : ft4) {
    index = st.indexOf(s);
    if (index != -1) {
     st = st.substring(0, index) + " " + s + "/t4 "
       + st.substring(index + s.length());
    }
   }

  }
  return GFString.formatSpace(st);
 }

 /**
  * 取得星期X的日期
  *
  * @param week
  *            0:星期天 1:星期一 2:星期二 3:星期三 4:星期四 5:星期五 6:星期六
  *
  * @return 日期
  */
 public static int getThisWeekDay(int week) {

  int w = 0;
  int t = 0;
  Calendar cal = Calendar.getInstance();
  t = cal.get(Calendar.DAY_OF_WEEK);

  switch (week) {
  case 0:
   w = cal.get(Calendar.DAY_OF_MONTH) + (8 - t);
   break;
  case 1:
   w = cal.get(Calendar.DAY_OF_MONTH) + (2 - t);
   break;
  case 2:
   w = cal.get(Calendar.DAY_OF_MONTH) + (3 - t);
   break;
  case 3:
   w = cal.get(Calendar.DAY_OF_MONTH) + (4 - t);
   break;
  case 4:
   w = cal.get(Calendar.DAY_OF_MONTH) + (5 - t);
   break;
  case 5:
   w = cal.get(Calendar.DAY_OF_MONTH) + (6 - t);
   break;
  case 6:
   w = cal.get(Calendar.DAY_OF_MONTH) + (7 - t);
   break;
  }

  return w;
 }

测试用例如下:

public void testParseTimeSeg(){
  GFDate.parseTimeSeg("18号之前杭州发往上海的火车有哪些?");
  GFDate.parseTimeSeg("明天下午3点四十以后杭州发往上海的火车有哪些?");
  GFDate.parseTimeSeg("明天下午3点以后后天下午8点以前杭州发往上海的火车有哪些?或者4月10号左右到北京的飞机.后天晚上到苏州的汽车也行");
  GFDate.parseTimeSeg("大后天晚上9点四十以后杭州发往上海的火车有哪些?");
  GFDate.parseTimeSeg("16号晚上9点四十以后十二点以前杭州发往上海的火车有哪些?");
  GFDate.parseTimeSeg("二十三点以前杭州发往上海的火车有哪些?");
  GFDate.parseTimeSeg("10点40左右");
  GFDate.parseTimeSeg("9月8号左右");
  GFDate.parseTimeSeg("9月8号到10号");
  GFDate.parseTimeSeg("明天下午开始到后天中午江二银泰有什么打折");
  GFDate.parseTimeSeg("江二有什么打折");
  GFDate.parseTimeSeg("周六有什么打折");
  GFDate.parseTimeSeg("周末有什么打折");
  GFDate.parseTimeSeg("星期一有什么打折");
  GFDate.parseTimeSeg("五一有什么打折");
  GFDate.parseTimeSeg("十一有什么打折");
 }

测试结果如下:

stime:18号 之前/t3
etime:null
cal1:2006-04-11 17:18:52
cal2:2006-04-18 00:00:00
stime:明天/t1 下午/t1 3点四十 以后/t3
etime:null
cal1:2006-04-12 15:40:00
cal2:2006-05-01 23:59:00
stime:明天/t1 下午/t1 3点 以后/t3 后天/t1 下午/t1 8点 以前/t3
etime:null
cal1:2006-04-12 15:00:00
cal2:2006-04-13 20:00:00
stime:后天/t1 晚上/t1 9点四十 以后/t3
etime:null
cal1:2006-04-13 21:40:00
cal2:2006-05-01 23:59:00
stime:16号 晚上/t1 9点四十 以后/t3 12点 以前/t3
etime:null
cal1:2006-04-16 21:40:00
cal2:2006-04-17 00:00:00
stime:23点 以前/t3
etime:null
cal1:2006-04-11 17:18:52
cal2:2006-04-11 23:00:00
stime:10点40 左右/t3
etime:null
cal1:2006-04-11 10:10:00
cal2:2006-04-11 11:10:00
stime:9月8号 左右/t3
etime:null
cal1:2006-09-07 00:00:00
cal2:2006-09-09 00:00:00
stime:9月8号 到/t4 10号
etime:null
cal1:2006-09-08 00:00:00
cal2:2006-09-10 00:00:00
stime:明天/t1 下午/t1
etime:后天/t1 中午/t1
cal1:2006-04-12 13:00:00
cal2:2006-04-13 11:00:00
stime:null
etime:null
cal1:2006-04-11 17:18:52
cal2:2099-12-31 00:00:00
stime:周六/t1
etime:null
cal1:2006-04-15 00:00:00
cal2:2006-04-15 23:59:00
stime:周末/t1
etime:null
cal1:2006-04-15 00:00:00
cal2:2006-04-16 23:59:00
stime:星期一/t1
etime:null
cal1:2006-04-10 00:00:00
cal2:2006-04-10 23:59:00
stime:五一/t1
etime:null
cal1:2006-05-01 00:00:00
cal2:2006-05-07 23:59:00
stime:十一/t1
etime:null
cal1:2006-10-01 00:00:00
cal2:2006-10-07 23:59:00


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
Time-NLP 中文语句中的时间语义识别 author:shinyke 本工具是由复旦NLP中的时间分析功能修改而来,做了很多细节和功能的优化,具体如下: 泛指时间的支持,如:早上、晚上、中午、傍晚等。 时间未来倾向。 如:在周五输入“周一早上开会”,则识别到下周一早上的时间;在下午17点输入:“9点送牛奶给隔壁的汉子”则识别到第二天上午9点。 多个时间的识别,及多个时间之间上下文关系处理。如:"下月1号下午3点至5点到图书馆还书",识别到开始时间为下月1号下午三点。同时,结束时间也继承上文时间,识别到下月1号下午5点。 可自定义基准时间:指定基准时间为“2016-05-20-09-00-00-00”,则一切分析以此时间为基准。 修复了各种各样的BUG。 简而言之,这是一个输入一句话,能识别出话里的时间的工具。╮(╯▽╰)╭ 示例代码: /**  *   * 测试类  *   * @author kexm  * @version 1.0  * @since 2016年5月4日  *   */ public class TimeAnalyseTest {     @Test     public void test(){         String path = TimeNormalizer.class.getResource("").getPath();         String classPath = path.substring(0, path.indexOf("/com/time"));         System.out.println(classPath "/TimeExp.m");         TimeNormalizer normalizer = new TimeNormalizer(classPath "/TimeExp.m");         normalizer.parse("Hi,all.下周一下午三点开会");// 抽取时间         TimeUnit[] unit = normalizer.getTimeUnit();         System.out.println("Hi,all.下周一下午三点开会");         System.out.println(DateUtil.formatDateDefault(unit[0].getTime())   "-"   unit[0].getIsAllDayTime());          normalizer.parse("早上六点起床");// 注意此处识别到6天在今天已经过去,自动识别为明早六点(未来倾向,可通过开关关闭:new TimeNormalizer(classPath "/TimeExp.m", false))         unit = normalizer.getTimeUnit();         System.out.println("早上六点起床");         System.out.println(DateUtil.formatDateDefault(unit[0].getTime())   "-"   unit[0].getIsAllDayTime());         normalizer.parse("周一开会");// 如果本周已经是周二,识别为下周周一。同理处理各级时间。(未来倾向)         unit = normalizer.getTimeUnit();         System.out.println("周一开会");         System.out.println(DateUtil.formatDateDefault(unit[0].getTime())   "-"   unit[0].getIsAllDayTime());         normalizer.parse("下下周一开会");//对于上/下的识别         unit = normalizer.getTimeUnit();         System.out.println("下下周一开会");         System.out.println(DateUtil.formatDateDefault(unit[0].getTime())   "-"   unit[0].getIsAllDayTime());  
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值