/** * 获取最晚出票时间. * @param ssrADTK * @param ssrOTHS * @return */ private static String getLastTicketTime(String ssrADTK,String ssrOTHS) { // -------------------最晚出票时间---------------------------- // 混航司的,SSR中可能有多个ADTK项,应该取时间最早的那个。 // 生成PNR后,过一段时间才会有,比如10分钟,半小时之类的,属于航司发过来的通知 // 格式不固定 // BY HKG25JAN18/1411 OR CXL HX528 X26JAN // TO BR BY 27JAN 1500 HKG OTHERWISE WILL BE XLD // TO CI BY 24JAN 1400 HKG TIME ZONE OTHERWISE WILL BE XLD // TO GA BY 01FEB 1400 OTHERWISE WILL BE XLD // ADV TKT NBR TO CX/KA BY 06DEC 0800 GMT OR SUBJECT TO CANCEL // ADV TKT BY 26JAN18 1800 HKG OR SEG WILL BE CXLD // RITK/ADTKT BY 12FEB 0900 HKG LT //有的航司写在TKTL中~~,这好像有时区问题啊? //SS/ /RGN 1238/25JAN.PNR EXPIRATION //SS/HKT 1355/26JAN //SS/CAN 1510/27JAN //SS/SIN 1220/01FEB //有的是在OTHS中 //NN1. PLS TKT BY 1134 25JAN18 HKG //ADV TKT BY 25JAN18 1020HKGHK OR WL BE CXLD //ISSUE TKT BY 28JAN 1714 LT SHA OR PNR WILL BE CXLD //PLS TICKET BY 2359/26JAN2018 LCLT AT BOARD POINT OR QR WILL CXL //ADTK中的期限是航司写的,超时会导致座位被干掉,所以很重要。TKTL中是系统或代理写的,超时不会怎么样,故不管它了。 //OTHS中应该也是航司写的。 String totalSSR = ssrADTK + "\n" +ssrOTHS; String[] ssrArray = totalSSR.split("\n"); List<Date> dateList = new ArrayList<Date>(); for(String ssr:ssrArray) { if(ssr.length()==0) { continue; } //包含关键字 if(ssr.contains("OR CXL") || ssr.contains("OTHERWISE WILL BE XLD") || ssr.contains("OR SUBJECT TO CANCEL") || ssr.contains("SEG WILL BE CXLD") || ssr.contains("ADTKT BY") || ssr.contains("TKT BY") || ssr.contains("TICKET BY")) { //HKG25JAN18/1411 //06DEC 0800 GMT --这个特殊,是GMT时间 //27JAN 1500 HKG //26JAN18 1800 HKG //25JAN18 1020HKGHK //28JAN 1714 LT SHA---以上4个都算本地时间吧 //2359/26JAN2018 LCLT AT BOARD POINT //1134 25JAN18 HKG---这俩是时间在前的 //匹配各种格式,不是HKG的城市只能无视了,没有城市时区的对应表 //(1)HKG25JAN18/1411 String regex = "HKG(\\d{2}(?:JAN|FEB|MAR|APR|MAY|JUN|JUL|AUG|SEP|OCT|NOV|DEC)\\d{2}/\\d{4})"; SimpleDateFormat dateFormat = new SimpleDateFormat("ddMMMyy/HHmm", Locale.US); Matcher m = Pattern.compile(regex).matcher(ssr); if(m.find()) { String dateStr = m.group(1); try { Date d = dateFormat.parse(dateStr); dateList.add(d); } catch (ParseException e) { // TODO Auto-generated catch block e.printStackTrace(); } continue; } //(2)06DEC 0800 GMT --这个特殊,是GMT时间 regex = "(\\d{2}(?:JAN|FEB|MAR|APR|MAY|JUN|JUL|AUG|SEP|OCT|NOV|DEC) \\d{4}) GMT"; dateFormat = new SimpleDateFormat("ddMMM HHmm", Locale.US); m = Pattern.compile(regex).matcher(ssr); if(m.find()) { String dateStr = m.group(1); try { Date d = dateFormat.parse(dateStr); //GMT时间,需要+8小时,转为HKG时间 Calendar calendar = new GregorianCalendar(); calendar.setTime(d); calendar.add(Calendar.HOUR_OF_DAY,8); dateList.add(calendar.getTime()); } catch (ParseException e) { // TODO Auto-generated catch block e.printStackTrace(); } continue; } //(3)这几个算本地时间吧 //26JAN18 1800 HKG //27JAN 1500 HKG //25JAN18 1020HKGHK //28JAN 1714 LT SHA regex = "(\\d{2}(?:JAN|FEB|MAR|APR|MAY|JUN|JUL|AUG|SEP|OCT|NOV|DEC)(\\d{2})? \\d{4})"; m = Pattern.compile(regex).matcher(ssr); if(m.find()) { if(m.group(2)!=null) { dateFormat = new SimpleDateFormat("ddMMMyy HHmm", Locale.US); }else { dateFormat = new SimpleDateFormat("ddMMM HHmm", Locale.US); } String dateStr = m.group(1); try { Date d = dateFormat.parse(dateStr); dateList.add(d); } catch (ParseException e) { // TODO Auto-generated catch block e.printStackTrace(); } continue; } //2359/26JAN2018 LCLT AT BOARD POINT //1134 25JAN18 HKG---这俩是时间在前的 regex = "(\\d{4}( |/)\\d{2}(?:JAN|FEB|MAR|APR|MAY|JUN|JUL|AUG|SEP|OCT|NOV|DEC)(\\d{4}|\\d{2}))"; m = Pattern.compile(regex).matcher(ssr); if(m.find()) { if(m.group(3).length()==4) { dateFormat = new SimpleDateFormat("HHmm" + m.group(2) + "ddMMMyyyy", Locale.US); }else { dateFormat = new SimpleDateFormat("HHmm" + m.group(2) + "ddMMMyy", Locale.US); } String dateStr = m.group(1); try { Date d = dateFormat.parse(dateStr); dateList.add(d); } catch (ParseException e) { // TODO Auto-generated catch block e.printStackTrace(); } continue; } } }
if(dateList.size()==0) { return ""; } //转换日期 List<Date> newDateList = new ArrayList<Date>(); for(Date d:dateList) { //没指定年份的话,年份是默认的1970,需要设置为当前年份或下一年(看日期而定,出票期限一定是未来) if(d.toString().contains("1970")) { //现在年份 Calendar calendarNow = new GregorianCalendar(); calendarNow.setTime(new Date()); int yearNow = calendarNow.get(Calendar.YEAR); //设置为当前年份 Calendar calendar = new GregorianCalendar(); calendar.setTime(d); calendar.set(Calendar.YEAR, yearNow); //小于当前时间,说明不对,得加一年 //如现在是12月2号,转换的结果是1月3号,那么说明应该是明年的1月3号 if(calendar.compareTo(calendarNow)<0) { calendar.set(Calendar.YEAR, yearNow+1); } d = calendar.getTime(); } newDateList.add(d); } System.out.println("---------------"); for(Date d:newDateList) { System.out.println(d.toString()); } //只有一个日期,直接返回 DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm"); if(newDateList.size()==1) { return df.format(newDateList.get(0)); } //List排序 Collections.sort(newDateList,new Comparator<Date>() {
@Override public int compare(Date one, Date two) { return one.compareTo(two); } }); //取一个最小的日期 return df.format(newDateList.get(0)); }
public static void main(String[] args) { String adtk = "BY HKG25JAN18/1411 OR CXL HX528 X26JAN"; adtk += "\n" + "TO BR BY 27JAN 1500 HKG OTHERWISE WILL BE XLD"; adtk += "\n" + "TO CI BY 24JAN 1400 HKG TIME ZONE OTHERWISE WILL BE XLD"; adtk += "\n" + "TO GA BY 01FEB 1400 OTHERWISE WILL BE XLD"; adtk += "\n" + "ADV TKT NBR TO CX/KA BY 06DEC 0800 GMT OR SUBJECT TO CANCEL"; adtk += "\n" + "ADV TKT BY 26JAN18 1800 HKG OR SEG WILL BE CXLD"; adtk += "\n" + "RITK/ADTKT BY 12FEB 0900 HKG LT"; String oths = "NN1. PLS TKT BY 1134 25JAN18 HKG"; oths += "\n" + "ADV TKT BY 25JAN18 1020HKGHK OR WL BE CXLD"; oths += "\n" + "ISSUE TKT BY 03JAN 1714 LT SHA OR PNR WILL BE CXLD"; oths += "\n" + "PLS TICKET BY 2359/10JAN2018 LCLT AT BOARD POINT OR QR WILL CXL"; String minDate = getLastTicketTime(adtk,oths); System.out.println(minDate); }
|