在路上

 找回密码
 立即注册
在路上 站点首页 学习 查看内容

java 时间工具类

2016-12-20 13:17| 发布者: zhangjf| 查看: 443| 评论: 0

摘要: import java.io.File; import java.io.IOException; import java.net.URLEncoder; import java.text.DateFormat; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Cal ...
  1. import java.io.File;
  2. import java.io.IOException;
  3. import java.net.URLEncoder;
  4. import java.text.DateFormat;
  5. import java.text.ParseException;
  6. import java.text.SimpleDateFormat;
  7. import java.util.Calendar;
  8. import java.util.Date;
  9. import java.util.GregorianCalendar;
  10. import java.util.Locale;
  11. import java.util.regex.Matcher;
  12. import java.util.regex.Pattern;
  13. import javax.servlet.http.HttpServletRequest;
  14. import org.apache.commons.lang.StringUtils;
  15. import org.jsoup.Jsoup;
  16. import org.jsoup.nodes.Document;
  17. import org.jsoup.nodes.Element;
  18. import org.jsoup.select.Elements;
  19. import com.trs.gab.beans.AppPhrasseDataResult;
  20. import com.trs.gab.beans.DicDataModel;
  21. import sun.misc.UCEncoder;
  22. /**
  23. * 时间工具类
  24. *
  25. */
  26. public class DateUtil {
  27. /**
  28. * 返回数据库中以'YYYY-MM-DD HH24:MI:SS'格式表示当前时间的字符串
  29. * 用法:to_date(?,'YYYY-MM-DD HH24:MI:SS')
  30. * ?设置strNowtime
  31. * @return
  32. */
  33. public static String getNowTimeStr(){
  34. Calendar m_cal = Calendar.getInstance();
  35. String strNowtime = m_cal.get(Calendar.YEAR) + "-"
  36. + (m_cal.get(Calendar.MONTH) + 1) + "-"
  37. + m_cal.get(Calendar.DAY_OF_MONTH) + " "
  38. + m_cal.get(Calendar.HOUR_OF_DAY) + ":"
  39. + m_cal.get(Calendar.MINUTE) + ":" + m_cal.get(Calendar.SECOND);
  40. return strNowtime;
  41. }
  42. /**
  43. * 获得当前时间,格式yyyy-MM-dd hh:mm:ss
  44. *
  45. * @param format
  46. * @return
  47. */
  48. public static String getCurrentDateTime() {
  49. return getCurrentDate("yyyy-MM-dd HH:mm:ss");
  50. }
  51. /**
  52. * 获得当前时间,格式自定义
  53. *
  54. * @param format
  55. * @return
  56. */
  57. public static String getCurrentDate(String format) {
  58. Calendar day = Calendar.getInstance();
  59. day.add(Calendar.DATE, 0);
  60. SimpleDateFormat sdf = new SimpleDateFormat(format);// "yyyy-MM-dd"
  61. String date = sdf.format(day.getTime());
  62. return date;
  63. }
  64. /**
  65. * 获得昨天时间,格式自定义
  66. *
  67. * @param format
  68. * @return
  69. */
  70. public static String getYesterdayDate(String format) {
  71. Calendar day = Calendar.getInstance();
  72. day.add(Calendar.DATE, -1);
  73. SimpleDateFormat sdf = new SimpleDateFormat(format);// "yyyy-MM-dd"
  74. String date = sdf.format(day.getTime());
  75. return date;
  76. }
  77. /**
  78. * @param date1
  79. * 需要比较的时间 不能为空(null),需要正确的日期格式 ,如:2009-09-12
  80. * @param date2
  81. * 被比较的时间 为空(null)则为当前时间
  82. * @param stype
  83. * 返回值类型 0为多少天,1为多少个月,2为多少年
  84. * @return 举例: compareDate("2009-09-12", null, 0);//比较天
  85. * compareDate("2009-09-12", null, 1);//比较月
  86. * compareDate("2009-09-12", null, 2);//比较年
  87. */
  88. public static int compareDate(String startDay, String endDay, int stype) {
  89. int n = 0;
  90. String[] u = { "天", "月", "年" };
  91. String formatStyle = stype == 1 ? "yyyy-MM" : "yyyy-MM-dd";
  92. endDay = endDay == null ? getCurrentDate("yyyy-MM-dd") : endDay;
  93. DateFormat df = new SimpleDateFormat(formatStyle);
  94. Calendar c1 = Calendar.getInstance();
  95. Calendar c2 = Calendar.getInstance();
  96. try {
  97. c1.setTime(df.parse(startDay));
  98. c2.setTime(df.parse(endDay));
  99. } catch (Exception e3) {
  100. System.out.println("wrong occured");
  101. }
  102. // List list = new ArrayList();
  103. while (!c1.after(c2)) { // 循环对比,直到相等,n 就是所要的结果
  104. // list.add(df.format(c1.getTime())); // 这里可以把间隔的日期存到数组中 打印出来
  105. n++;
  106. if (stype == 1) {
  107. c1.add(Calendar.MONTH, 1); // 比较月份,月份+1
  108. } else {
  109. c1.add(Calendar.DATE, 1); // 比较天数,日期+1
  110. }
  111. }
  112. n = n - 1;
  113. if (stype == 2) {
  114. n = (int) n / 365;
  115. }
  116. // System.out.println(startDay+" -- "+endDay+" 相差多少"+u[stype]+":"+n);
  117. return n;
  118. }
  119. /**
  120. * 判断时间是否符合时间格式
  121. */
  122. public static boolean isLegalDateString(String date, String dateFormat) {
  123. if (date != null) {
  124. java.text.SimpleDateFormat format = new java.text.SimpleDateFormat(
  125. dateFormat);
  126. format.setLenient(false);
  127. try {
  128. format.format(format.parse(date));
  129. } catch (ParseException e) {
  130. return false;
  131. }
  132. return true;
  133. }
  134. return false;
  135. }
  136. /**
  137. * 实现给定某日期,判断是星期几 date:必须yyyy-MM-dd格式
  138. */
  139. public static String getWeekday(String date) {
  140. SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd");
  141. SimpleDateFormat sdw = new SimpleDateFormat("E");
  142. Date d = null;
  143. try {
  144. d = sd.parse(date);
  145. } catch (ParseException e) {
  146. e.printStackTrace();
  147. }
  148. return sdw.format(d);
  149. }
  150. /**
  151. * 用来全局控制 上一周,本周,下一周的周数变化
  152. */
  153. private static int weeks = 0;
  154. /**
  155. * 获得当前日期与本周一相差的天数
  156. */
  157. private static int getMondayPlus() {
  158. Calendar cd = Calendar.getInstance();
  159. // 获得今天是一周的第几天,星期日是第一天,星期二是第二天......
  160. int dayOfWeek = cd.get(Calendar.DAY_OF_WEEK);
  161. if (dayOfWeek == 1)
  162. return -6;
  163. else
  164. return 2 - dayOfWeek;
  165. }
  166. /**
  167. * 获得本周星期一的日期
  168. */
  169. public static String getCurrentMonday(String format) {
  170. weeks = 0;
  171. int mondayPlus = getMondayPlus();
  172. Calendar currentDate = Calendar.getInstance();
  173. currentDate.add(Calendar.DATE, mondayPlus);
  174. SimpleDateFormat sdf = new SimpleDateFormat(format);// "yyyy-MM-dd"
  175. String date = sdf.format(currentDate.getTime());
  176. return date;
  177. }
  178. /**
  179. * 获得上周星期一的日期
  180. */
  181. public static String getPreviousMonday(String format) {
  182. weeks--;
  183. int mondayPlus = getMondayPlus();
  184. Calendar currentDate = Calendar.getInstance();
  185. currentDate.add(GregorianCalendar.DATE, mondayPlus + 7 * weeks);
  186. SimpleDateFormat sdf = new SimpleDateFormat(format);// "yyyy-MM-dd"
  187. String date = sdf.format(currentDate.getTime());
  188. return date;
  189. }
  190. /**
  191. * 获得下周星期一的日期
  192. */
  193. public static String getNextMonday(String format) {
  194. weeks++;
  195. int mondayPlus = getMondayPlus();
  196. // GregorianCalendar currentDate = new GregorianCalendar();
  197. Calendar currentDate = Calendar.getInstance();
  198. currentDate.add(GregorianCalendar.DATE, mondayPlus + 7 * weeks);
  199. SimpleDateFormat sdf = new SimpleDateFormat(format);// "yyyy-MM-dd"
  200. String date = sdf.format(currentDate.getTime());
  201. return date;
  202. }
  203. /**
  204. * 获得相应周的周日的日期 此方法必须写在getCurrentMonday,getPreviousMonday或getNextMonday方法之后
  205. */
  206. public static String getSunday(String format) {
  207. int mondayPlus = getMondayPlus();
  208. Calendar currentDate = Calendar.getInstance();
  209. currentDate.add(Calendar.DATE, mondayPlus + 7 * weeks + 6);
  210. SimpleDateFormat sdf = new SimpleDateFormat(format);// "yyyy-MM-dd"
  211. String date = sdf.format(currentDate.getTime());
  212. return date;
  213. }
  214. /**
  215. * method 将字符串类型的日期转换为一个timestamp(时间戳记java.sql.Timestamp)
  216. *
  217. * @param dateString
  218. * 需要转换为timestamp的字符串
  219. * @return dataTime timestamp
  220. */
  221. public final static java.sql.Timestamp string2Time(String dateString) {
  222. DateFormat dateFormat;
  223. dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.ENGLISH);// 设定格式
  224. dateFormat.setLenient(false);
  225. java.util.Date date = null;
  226. try {
  227. date = dateFormat.parse(dateString);
  228. } catch (ParseException e) {
  229. // TODO Auto-generated catch block
  230. e.printStackTrace();
  231. }
  232. // java.sql.Timestamp dateTime = new java.sql.Timestamp(date.getTime());
  233. return new java.sql.Timestamp(date.getTime());// Timestamp类型,timeDate.getTime()返回一个long型
  234. }
  235. /**
  236. * method 将字符串类型的日期转换为一个Date(java.sql.Date)
  237. *
  238. * @param dateString
  239. * 需要转换为Date的字符串
  240. * @return dataTime Date
  241. */
  242. public final static java.sql.Date string2Date(String dateString) {
  243. DateFormat dateFormat;
  244. dateFormat = new SimpleDateFormat("yyyy-MM-dd", Locale.ENGLISH);
  245. dateFormat.setLenient(false);
  246. java.util.Date date = null;
  247. try {
  248. date = dateFormat.parse(dateString);
  249. } catch (ParseException e) {
  250. // TODO Auto-generated catch block
  251. e.printStackTrace();
  252. }
  253. // java.sql.Date dateTime = new java.sql.Date(date.getTime());// sql类型
  254. return new java.sql.Date(date.getTime());
  255. }
  256. // 记录考勤, 记录迟到、早退时间
  257. public static String getState() {
  258. String state = "正常";
  259. DateFormat df = new SimpleDateFormat("HH:mm:ss");
  260. Date d = new Date();
  261. try {
  262. Date d1 = df.parse("08:00:00");
  263. Date d2 = df.parse(df.format(d));
  264. Date d3 = df.parse("18:00:00");
  265. int t1 = (int) d1.getTime();
  266. int t2 = (int) d2.getTime();
  267. int t3 = (int) d3.getTime();
  268. if (t2 < t1) {
  269. long between = (t1 - t2) / 1000;// 除以1000是为了转换成秒
  270. long hour1 = between % (24 * 3600) / 3600;
  271. long minute1 = between % 3600 / 60;
  272. state = "迟到 :" + hour1 + "时" + minute1 + "分";
  273. } else if (t2 < t3) {
  274. long between = (t3 - t2) / 1000;// 除以1000是为了转换成秒
  275. long hour1 = between % (24 * 3600) / 3600;
  276. long minute1 = between % 3600 / 60;
  277. state = "早退 :" + hour1 + "时" + minute1 + "分";
  278. }
  279. return state;
  280. } catch (Exception e) {
  281. return state;
  282. }
  283. }
  284. /**
  285. * 数值型的时间改为字符串型时间
  286. *
  287. * @param time
  288. * @return
  289. */
  290. public static String getTime(long time) {
  291. try {
  292. Date date = new Date(time);
  293. SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//
  294. String strdate = sdf.format(date);
  295. return strdate;
  296. } catch (Exception e) {
  297. e.printStackTrace();
  298. return "0";
  299. }
  300. }
  301. /**
  302. * 传入"yyyy-MM-dd HH:mm:ss"格式字符串,传出从1970 年~~~ 至dateString表示时刻之间的ms。
  303. * @return
  304. */
  305. public static long getTimeMillis(String dateString){
  306. long timeMillis = 0;
  307. SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  308. try {
  309. Date date= sdf.parse(dateString);
  310. timeMillis = date.getTime();
  311. } catch (ParseException e) {
  312. }
  313. return timeMillis;
  314. }
  315. /**
  316. * 获得后N天的时间,格式自定义
  317. *
  318. * @param format
  319. * @return
  320. */
  321. public static String getDelayDayDate(String format,int delay) {
  322. Calendar day = Calendar.getInstance();
  323. day.add(Calendar.DATE, delay);
  324. SimpleDateFormat sdf = new SimpleDateFormat(format);// "yyyy-MM-dd"
  325. String date = sdf.format(day.getTime());
  326. return date;
  327. }
  328. /**
  329. * 获得后N小时的时间,格式自定义
  330. * @param format
  331. * @param delay
  332. * @return
  333. */
  334. public static String getDelayHourDate(String format,int delay){
  335. Calendar day = Calendar.getInstance();
  336. day.add(Calendar.HOUR, delay);
  337. SimpleDateFormat sdf = new SimpleDateFormat(format);// "yyyy-MM-dd"
  338. String date = sdf.format(day.getTime());
  339. return date;
  340. }
  341. /**
  342. * @param date1
  343. * 需要比较的时间 不能为空(null),需要正确的日期格式 ,如:2009-09-12 16:24
  344. * @param date2
  345. * 被比较的时间 为空(null)则为当前时间
  346. * @param stype 0为比较小时,1为比较分钟。
  347. * @return
  348. */
  349. public static int compareTime(String startDay, String endDay,int stype) {
  350. int n = 0;
  351. String formatStyle = "yyyy-MM-dd HH:mm";
  352. endDay = endDay == null ? getCurrentDate("yyyy-MM-dd HH:mm") : endDay;
  353. DateFormat df = new SimpleDateFormat(formatStyle);
  354. Calendar c1 = Calendar.getInstance();
  355. Calendar c2 = Calendar.getInstance();
  356. try {
  357. c1.setTime(df.parse(startDay));
  358. c2.setTime(df.parse(endDay));
  359. } catch (Exception e3) {
  360. System.out.println("wrong occured");
  361. }
  362. // List list = new ArrayList();
  363. while (!c1.after(c2)) { // 循环对比,直到相等,n 就是所要的结果
  364. // list.add(df.format(c1.getTime())); // 这里可以把间隔的日期存到数组中 打印出来
  365. n++;
  366. if(stype == 0){
  367. c1.add(Calendar.HOUR, 1); // 比较月份,月份+1
  368. }else{
  369. c1.add(Calendar.MINUTE, 1); // 比较月份,月份+1
  370. }
  371. }
  372. n = n - 1;
  373. return n;
  374. }
  375. /**
  376. * 获取词典(牛津英汉词典)音标的信息
  377. */
  378. public static String getYin(String content)
  379. {
  380. String result="";
  381. String reg="<font color=(["'])#F17D1F(["']) size=4>(?:.*?)</font>";
  382. //String str="<td class="longtd" title="VRBZ3">VRBZ3</td>";
  383. String str="<font color=red>china</font><font color=green>china</font>";
  384. Pattern p=Pattern.compile(reg);
  385. Matcher m=p.matcher(content);
  386. int i=1;
  387. while(m.find()){
  388. System.out.println(m.group(0));
  389. result=result+"<br>"+"<font color=#F17D1F>"+i+"</font>"+m.group(0).replaceAll("size=4", "style="font-family:Arial Unicode Ms";");
  390. i++;
  391. }
  392. if(i==2)
  393. {
  394. String r="<font color=#F17D1F>";
  395. int x=result.indexOf(r);
  396. result=result.substring(x+r.length()+1);
  397. }
  398. return result;
  399. }
  400. /**
  401. * 获取词典(字典)中的信息、
  402. */
  403. public static String getFont(String content)
  404. {
  405. String result="";
  406. String reg="(?:.*?)<br>";
  407. //String str="<td class="longtd" title="VRBZ3">VRBZ3</td>";
  408. String str="你<br>nǐ<br>称对方,多称指一个人,有时也指称若干人:你厂。你方。<br>泛指任何人:你死我活。<br>您<br><br>笔画数:7;<br>部首:亻;<br>笔顺编号:3235234<br><br><br>";
  409. Pattern p=Pattern.compile(reg);
  410. Matcher m=p.matcher(content);
  411. int i=1;
  412. while(m.find()){
  413. System.out.println(m.group(0));
  414. if(!m.group(0).equals("<br>"))
  415. {
  416. result=result+i+"、 "+m.group(0);
  417. i++;
  418. }
  419. }
  420. return result;
  421. }
  422. public static String getDemo(String color,String content)
  423. {
  424. String result="";
  425. String reg="<font color="+color+">(?:.*?)</font>";
  426. //String str="<td class="longtd" title="VRBZ3">VRBZ3</td>";
  427. String str="<font color=red>china</font><font color=green>china</font>";
  428. Pattern p=Pattern.compile(reg);
  429. Matcher m=p.matcher(content);
  430. if(color.equals("blue"))
  431. {
  432. int i=1;
  433. while(m.find()){
  434. System.out.println(m.group(0));
  435. result=result+"<br> <font color=blue>"+i+" </font>"+m.group(0).replaceAll("size=4", "");
  436. i++;
  437. }
  438. }
  439. else
  440. {
  441. while(m.find()){
  442. System.out.println(m.group(0));
  443. result=result+"<br>"+m.group(0).replaceAll("size=4", "");
  444. }
  445. }
  446. return result;
  447. }
  448. public static String getUse(String content)
  449. {
  450. String result="";
  451. String reg="</font>(?:.*?)<br>";
  452. //String str="<td class="longtd" title="VRBZ3">VRBZ3</td>";
  453. String str="<font color=red>china</font><font color=green>china</font>";
  454. Pattern p=Pattern.compile(reg);
  455. Matcher m=p.matcher(content);
  456. while(m.find()){
  457. System.out.println(m.group(0));
  458. result=result+m.group(0).replaceAll("size=4", "");
  459. }
  460. return result;
  461. }
  462. public static String[] getContent(String content,String prefex)
  463. {
  464. String result[]=new String[3];
  465. String reg=prefex+"(?:.*?)</div>";
  466. //String str="<td class="longtd" title="VRBZ3">VRBZ3</td>";
  467. String str="<font color=red>china</font><font color=green>china</font>";
  468. Pattern p=Pattern.compile(reg);
  469. Matcher m=p.matcher(content);
  470. int count=0;
  471. while(m.find()){
  472. if(count<3)
  473. {
  474. result[count]=m.group(0);
  475. }
  476. count++;
  477. }
  478. return result;
  479. }
  480. public static boolean getContent(String content,String prefex,String reg)
  481. {
  482. boolean result=false;
  483. Pattern p=Pattern.compile(reg);
  484. Matcher m=p.matcher(content);
  485. while(m.find()){
  486. result=true;
  487. }
  488. return result;
  489. }
  490. public static String getC(String content,String prefex)
  491. {
  492. System.out.println("未处理的t内容"+content);
  493. int start=content.indexOf(prefex);
  494. String result="";
  495. if(start!=-1)
  496. {
  497. result=content.substring(start, content.length()-1);
  498. return result.replaceAll("</div>","")+"</div>";
  499. }
  500. else
  501. {
  502. result=content;
  503. return result;
  504. }
  505. }
  506. public static String getContent(String content)
  507. {
  508. String s_content[]=new String[2];
  509. for(int i=0;i<2;i++)
  510. {
  511. s_content[i]=content;
  512. }
  513. String v=getContent(s_content[0],"<div id=v>")[0];
  514. String c=getContent(s_content[1],"<div id=c>")[0];
  515. if(!getContent(c, "<div id=c>", "[u4E00-u9FA5]"))
  516. {
  517. c=getC(s_content[1],"<div id=c>");
  518. }
  519. return c+v;
  520. }
  521. public static String replaceTag(String content)
  522. {
  523. return content.replaceAll("<br>", " ").replaceAll("bword://", "").replaceAll("\\n", "").replaceAll("<img.*>.*</img>","").replaceAll("<img.*/>","");
  524. }
  525. /**
  526. * 获取现在时间
  527. * @return返回字符串格式 yyyy-MM-dd HH:mm:ss
  528. */
  529. public static String getStringDate() {
  530. Date currentTime = new Date();
  531. SimpleDateFormat formatter = new SimpleDateFormat("M月d日");
  532. String dateString = formatter.format(currentTime);
  533. return dateString;
  534. }
  535. public static String getNum(String content)
  536. {
  537. String result="";
  538. String reg="(?:.*?)。";
  539. //String str="<td class="longtd" title="VRBZ3">VRBZ3</td>";
  540. String str="(1)在体积、面积、数量、力量、强度等方面不及一般的或不及比较的对象(跟‘大’相对):~河|鞋~了点儿|我比你~一岁|声音太~。(2)短时间地:~坐|~住。(3)排行最末的:~儿子|他是我的~弟弟。(4)年纪小的人:一家大~|上有老,下有~。(5)指妾①。(6)谦辞,称自己或与自己有关的人或事物:~弟|~店。 …小白菜@(~ ";
  541. Pattern p=Pattern.compile(reg);
  542. Matcher m=p.matcher(content);
  543. while(m.find()){
  544. if(m.group(0).length()>3)
  545. {
  546. System.out.println(m.group(0));
  547. result=result+m.group(0)+"<br>";
  548. }
  549. else
  550. {
  551. result=content;
  552. }
  553. }
  554. if(result.length()==0)
  555. {
  556. result=content;
  557. }
  558. return result;
  559. }
  560. /**
  561. * @param args
  562. * @return
  563. * @return
  564. */
  565. public static String getContentSource(String contentSource,String tag,String tagId,int wordSize)
  566. {
  567. Document doc;
  568. String linkText="";
  569. //System.out.println("未处理的文本内容:"+contentSource);
  570. try {
  571. doc = Jsoup.parse(contentSource);
  572. Element content = doc.getElementById(tagId);
  573. if(content!=null){
  574. Elements texts = content.select(tag + "[id=" + tagId + "]");
  575. for (Element link : texts) {
  576. linkText = linkText + link.text();
  577. }
  578. }
  579. else
  580. {
  581. Element t = doc.getElementById("t");
  582. if(t!=null){
  583. Elements texts = t.select("div[id=t]");
  584. for (Element link : texts) {
  585. linkText = linkText + link.text();
  586. }
  587. }
  588. else
  589. {
  590. linkText=contentSource;
  591. }
  592. }
  593. } catch (Exception e) {
  594. e.printStackTrace();
  595. }
  596. //System.out.println("得到的文本内容:"+linkText);
  597. if(linkText.length()>300)
  598. {
  599. linkText=linkText.substring(0, wordSize)+"...";
  600. }
  601. return linkText;
  602. }
  603. public static String getContentText(String contentSource,String tag,String tagId)
  604. {
  605. Document doc;
  606. String linkText="";
  607. //System.out.println("未处理的文本内容:"+contentSource);
  608. try {
  609. doc = Jsoup.parse(contentSource);
  610. Element content = doc.getElementById(tagId);
  611. if(content!=null){
  612. Elements texts = content.select(tag + "[id=" + tagId + "]");
  613. for (Element link : texts) {
  614. linkText = linkText + link.text();
  615. }
  616. }
  617. } catch (Exception e) {
  618. e.printStackTrace();
  619. }
  620. //System.out.println("得到的文本内容:"+linkText);
  621. return linkText;
  622. }
  623. /**
  624. * 得到div id=m的内容
  625. * @param contentSource
  626. * @param tag
  627. * @param tagId
  628. * @return
  629. */
  630. public static String getContentSourceOfm(String contentSource,String tag,String tagId)
  631. {
  632. Document doc;
  633. String linkText="";
  634. //System.out.println("未处理的文本内容:"+contentSource);
  635. try {
  636. doc = Jsoup.parse(contentSource);
  637. Element content = doc.getElementById(tagId);
  638. if(content!=null){
  639. Elements texts = content.select(tag + "[id=" + tagId + "]");
  640. for (Element link : texts) {
  641. linkText = linkText + link.text();
  642. }
  643. }
  644. else
  645. {
  646. Element t = doc.getElementById("t");
  647. if(t!=null){
  648. Elements texts = t.select("div[id=t]");
  649. for (Element link : texts) {
  650. linkText = linkText + link.text();
  651. }
  652. }
  653. else
  654. {
  655. linkText=contentSource;
  656. }
  657. }
  658. } catch (Exception e) {
  659. e.printStackTrace();
  660. }
  661. //System.out.println("得到的文本内容:"+linkText);
  662. if(linkText.length()>300)
  663. {
  664. linkText=linkText.substring(0, 300)+"...";
  665. }
  666. return linkText;
  667. }
  668. public static boolean isHasRelativeWordlink(String content,String tag)
  669. {
  670. if(content.contains(tag))
  671. {
  672. return true;
  673. }
  674. else
  675. {
  676. return false;
  677. }
  678. }
  679. /**
  680. * 得到相关词条的解释
  681. * @param content
  682. * @param request
  683. * @param tag
  684. * @return
  685. */
  686. public static String getRelativeWordlink(String content,HttpServletRequest request,String tag)
  687. {
  688. String param[]=content.split(tag);
  689. //URLEncoder encoder=null;
  690. content="参考词条:<a href="+request.getContextPath()+"/baikeSearchRelative.trs?searchWord="+param[1].replaceAll("\\n", "")+" target="_blank">"+param[1].replaceAll("\\n", "")+"</a>";
  691. return content;
  692. }
  693. /**
  694. * 得到相关词条的解释
  695. * @param content
  696. * @param request
  697. * @param tag
  698. * @return
  699. */
  700. public static DicDataModel getRelativeWordContent(String content,String tag,int type)
  701. {
  702. String param[]=content.split(tag);
  703. //URLEncoder encoder=null;
  704. DicDataModel dataModel=null;
  705. AppPhrasseDataResult appPhrasseDataResult=null;
  706. appPhrasseDataResult=new AppPhrasseDataResult();
  707. try {
  708. dataModel=appPhrasseDataResult.getBaikeInfo("TB_BAIKE", "name="+param[1].replaceAll("\\n", ""));
  709. } catch (Exception e) {
  710. e.printStackTrace();
  711. }
  712. if(type==0)
  713. {
  714. dataModel.setContent(DateUtil.getContentSource(dataModel.getContent(), "", "c",300),new Boolean("true"));
  715. }
  716. else
  717. {
  718. dataModel.setContent(dataModel.getContent(),new Boolean("true"));
  719. }
  720. return dataModel;
  721. }
  722. /**
  723. * 将相关词条添加到字符串数组中
  724. * @param contentSource
  725. * @param request
  726. * @return
  727. */
  728. public static String[] getRelativeWordlink(String contentSource,HttpServletRequest request)
  729. {Document doc;
  730. String relativeWord[]=null;
  731. int count=0;
  732. try {
  733. doc = Jsoup.parse(contentSource);
  734. Element content = doc.getElementById("m");
  735. //Elements links = content.getElementsByTag("td");
  736. if(content!=null){
  737. Elements links = content.select("a");
  738. relativeWord=new String[links.size()];
  739. for (Element link : links) {
  740. String linkText = link.text();
  741. if(linkText.contains("@"))
  742. {
  743. relativeWord[count]=linkText;
  744. }
  745. count++;
  746. }
  747. }
  748. } catch (Exception e) {
  749. // TODO Auto-generated catch block
  750. e.printStackTrace();
  751. }
  752. return relativeWord;
  753. }
  754. /**
  755. * 将相关词条的细缆页链接加上
  756. * @param content
  757. * @param relativeWord
  758. * @param request
  759. * @return
  760. */
  761. public static String replaceRelativeWord(String content,String relativeWord[],HttpServletRequest request)
  762. {
  763. for(int i=0;i<relativeWord.length;i++)
  764. { String c=relativeWord[i];
  765. if(StringUtils.isNotEmpty(c))
  766. {
  767. String r=relativeWord[i].substring(1);
  768. content=content.replaceFirst(r, request.getContextPath()+"/baikeSearchRelative.trs?searchWord="+r);
  769. }
  770. }
  771. return content;
  772. }
  773. /**
  774. * 得到词条的目录链接:
  775. * 默认取前5条链接
  776. * @param content
  777. * @return
  778. */
  779. public static String getLink(String contentSource, String linkContent,int count,String tag) {
  780. Document doc;
  781. String linkText = "";
  782. try {
  783. doc = Jsoup.parse(contentSource);
  784. Element content = doc.getElementById(tag);
  785. if(content!=null){
  786. Elements links = content.select("p");
  787. int i=1;
  788. for (Element link : links) {
  789. String href=getChildlink(link.outerHtml());
  790. if(StringUtils.isNotEmpty(href))
  791. {
  792. linkText = linkText +"<p>"+href+"</p>";
  793. }
  794. if(i==count)
  795. {
  796. break;
  797. }
  798. i++;
  799. }
  800. /*if(i<count)
  801. {
  802. linkText = linkText+getLinkOfm(linkContent, count-i,"m");
  803. }*/
  804. }
  805. else
  806. {
  807. linkText=contentSource;
  808. }
  809. } catch (Exception e) {
  810. // TODO Auto-generated catch block
  811. e.printStackTrace();
  812. }
  813. return linkText;
  814. }
  815. /**
  816. * 链接的数量不足时增加m标签的内容
  817. * @param contentSource
  818. * @param linkContent
  819. * @param count
  820. * @param tag
  821. * @return
  822. */
  823. public static String getLinkOfm(String linkContent,int count,String tag) {
  824. Document doc;
  825. String linkText = "";
  826. try {
  827. doc = Jsoup.parse(linkContent);
  828. Element content = doc.getElementById(tag);
  829. if(content!=null){
  830. Elements links = content.select("p");
  831. int i=0;
  832. for (Element link : links) {
  833. linkText = linkText +"<p>"+ getChildlink(link.outerHtml())+"</p>";
  834. if(i==count)
  835. {
  836. break;
  837. }
  838. i++;
  839. }
  840. }
  841. else
  842. {
  843. linkText=linkContent;
  844. }
  845. } catch (Exception e) {
  846. // TODO Auto-generated catch block
  847. e.printStackTrace();
  848. }
  849. return linkText;
  850. }
  851. /**
  852. * 获取链接的子链接的第一个链接
  853. * @param contentSource
  854. * @return
  855. */
  856. public static String getChildlink(String contentSource) {
  857. Document doc;
  858. String linkText = null;
  859. try {
  860. doc = Jsoup.parse(contentSource);
  861. Elements links = doc.select("a");
  862. if (links != null) {
  863. int i = 0;
  864. for (Element link : links) {
  865. if (i == 0) {
  866. linkText = link.outerHtml();
  867. }
  868. i++;
  869. }
  870. } else {
  871. linkText = contentSource;
  872. }
  873. } catch (Exception e) {
  874. // TODO Auto-generated catch block
  875. e.printStackTrace();
  876. }
  877. return linkText;
  878. }
  879. public static int getNowTime(int type){
  880. Calendar m_cal = Calendar.getInstance();
  881. if(type==1)
  882. {
  883. return m_cal.get(Calendar.MONTH)+1;
  884. }
  885. else
  886. return m_cal.get(Calendar.DAY_OF_MONTH);
  887. }
  888. public static void main(String[] args) {
  889. System.out.println(getContentText("<div id=t><p></div>n","div","t"));
  890. }
  891. }
复制代码

最新评论

小黑屋|在路上 ( 蜀ICP备15035742号-1 

;

GMT+8, 2025-7-8 14:31

Copyright 2015-2025 djqfx

返回顶部