1. package com.td.util;
  2.  
  3. import java.sql.Timestamp;
  4. import java.text.ParseException;
  5. import java.text.ParsePosition;
  6. import java.text.SimpleDateFormat;
  7. import java.util.ArrayList;
  8. import java.util.Calendar;
  9. import java.util.Date;
  10. import java.util.GregorianCalendar;
  11. import java.util.List;
  12. import java.util.Locale;
  13. import java.util.Random;
  14.  
  15. import org.apache.log4j.chainsaw.Main;
  16.  
  17. /**
  18. * 超详细的时间工具类
  19. * @author Administrator
  20. *
  21. */
  22. public class TimeUtil {
  23.  
  24. //============================借助Calendar类获取今天、昨天、本周、上周、本年及特定时间的开始时间和结束时间(返回类型为date类型)========================
  25.  
  26. /**
  27. * 获取当天开始时间
  28. * @return
  29. */
  30. public static Date getDayBegin(){
  31. Calendar cal=Calendar.getInstance();
  32. cal.set(Calendar.HOUR_OF_DAY, 0);//0点
  33. cal.set(Calendar.MINUTE, 0);//0分
  34. cal.set(Calendar.SECOND, 0);//0秒
  35. cal.set(Calendar.MILLISECOND, 0);//0毫秒
  36. return cal.getTime();
  37. }
  38.  
  39. /**
  40. * 获取当天结束时间
  41. * @return
  42. */
  43. public static Date getDayEnd(){
  44. Calendar cal=Calendar.getInstance();
  45. cal.set(Calendar.HOUR_OF_DAY, 23);//23点
  46. cal.set(Calendar.MINUTE, 59);//59分
  47. cal.set(Calendar.SECOND, 59);//59秒
  48. return cal.getTime();
  49. }
  50.  
  51. /**
  52. * 获取昨天开始时间
  53. * @return
  54. */
  55. public static Date getBeginDayOfYesterday(){
  56. Calendar cal=Calendar.getInstance();
  57. cal.setTime(getDayBegin());//当天开始时间
  58. cal.add(Calendar.DAY_OF_MONTH, -1);//当天月份天数减1
  59. return cal.getTime();
  60. }
  61.  
  62. /**
  63. * 获取昨天结束时间
  64. * @return
  65. */
  66. public static Date getEndDayOfYesterday(){
  67. Calendar cal=Calendar.getInstance();
  68. cal.setTime(getDayEnd());//当天结束时间
  69. cal.add(Calendar.DAY_OF_MONTH, -1);//当天月份天数减1
  70. return cal.getTime();
  71. }
  72.  
  73. /**
  74. * 获取明天开始时间
  75. * @return
  76. */
  77. public static Date getBeginDayOfTomorrow(){
  78. Calendar cal=Calendar.getInstance();
  79. cal.setTime(getDayBegin());//当天开始时间
  80. cal.add(Calendar.DAY_OF_MONTH, 1);//当天月份天数加1
  81. return cal.getTime();
  82. }
  83.  
  84. /**
  85. * 获取明天结束时间
  86. * @return
  87. */
  88. public static Date getEndDayOfTomorrow(){
  89. Calendar cal=Calendar.getInstance();
  90. cal.setTime(getDayEnd());//当天结束时间
  91. cal.add(Calendar.DAY_OF_MONTH, 1);//当天月份天数加1
  92. return cal.getTime();
  93. }
  94.  
  95. /**
  96. * 获取某个日期的开始时间
  97. * @param d
  98. * @return
  99. */
  100. public static Timestamp getDayStartTime(Date d) {
  101. Calendar calendar=Calendar.getInstance();
  102. if(null!=d){
  103. calendar.setTime(d);
  104. }
  105. calendar.set(calendar.get(Calendar.YEAR),
  106. calendar.get(Calendar.MONTH),
  107. calendar.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
  108. calendar.set(Calendar.MILLISECOND, 0);
  109. return new Timestamp(calendar.getTimeInMillis());
  110. }
  111.  
  112. /**
  113. * 获取某个日期的结束时间
  114. * @param d
  115. * @return
  116. */
  117. public static Timestamp getDayEndTime(Date d) {
  118. Calendar calendar=Calendar.getInstance();
  119. if(null!=d){
  120. calendar.setTime(d);
  121. }
  122. calendar.set(calendar.get(Calendar.YEAR),
  123. calendar.get(Calendar.MONTH),
  124. calendar.get(Calendar.DAY_OF_MONTH), 23, 59, 59);
  125. calendar.set(Calendar.MILLISECOND, 999);
  126. return new Timestamp(calendar.getTimeInMillis());
  127. }
  128.  
  129. /**
  130. * 获取本周的开始时间
  131. * @return
  132. */
  133. @SuppressWarnings("unused")
  134. public static Date getBeginDayOfWeek(){
  135. Date date=new Date();
  136. if(date==null){
  137. return null;
  138. }
  139. Calendar cal=Calendar.getInstance();
  140. cal.setTime(date);
  141. int dayOfWeek=cal.get(Calendar.DAY_OF_WEEK);
  142. if(dayOfWeek==1){
  143. dayOfWeek+=7;
  144. }
  145. cal.add(Calendar.DATE, 2-dayOfWeek);
  146. return getDayStartTime(cal.getTime());
  147. }
  148.  
  149. /**
  150. * 获取本周的结束时间
  151. * @return
  152. */
  153. public static Date getEndDayOfWeek(){
  154. Calendar cal=Calendar.getInstance();
  155. cal.setTime(getBeginDayOfWeek());
  156. cal.add(Calendar.DAY_OF_WEEK, 6);
  157. Date weekEndSta = cal.getTime();
  158. return getDayEndTime(weekEndSta);
  159. }
  160.  
  161. /**
  162. * 获取上周开始时间
  163. */
  164. @SuppressWarnings("unused")
  165. public static Date getBeginDayOfLastWeek() {
  166. Date date=new Date();
  167. if (date==null) {
  168. return null;
  169. }
  170. Calendar cal=Calendar.getInstance();
  171. cal.setTime(date);
  172. int dayofweek=cal.get(Calendar.DAY_OF_WEEK);
  173. if (dayofweek==1) {
  174. dayofweek+=7;
  175. }
  176. cal.add(Calendar.DATE, 2-dayofweek-7);
  177. return getDayStartTime(cal.getTime());
  178. }
  179.  
  180. /**
  181. * 获取上周的结束时间
  182. * @return
  183. */
  184. public static Date getEndDayOfLastWeek(){
  185. Calendar cal=Calendar.getInstance();
  186. cal.setTime(getBeginDayOfLastWeek());
  187. cal.add(Calendar.DAY_OF_WEEK, 6);
  188. Date weekEndSta = cal.getTime();
  189. return getDayEndTime(weekEndSta);
  190. }
  191.  
  192. /**
  193. * 获取今年是哪一年
  194. * @return
  195. */
  196. public static Integer getNowYear(){
  197. Date date = new Date();
  198. GregorianCalendar gc=(GregorianCalendar)Calendar.getInstance();
  199. gc.setTime(date);
  200. return Integer.valueOf(gc.get(1));
  201. }
  202.  
  203. /**
  204. * 获取本月是哪一月
  205. * @return
  206. */
  207. public static int getNowMonth() {
  208. Date date = new Date();
  209. GregorianCalendar gc=(GregorianCalendar)Calendar.getInstance();
  210. gc.setTime(date);
  211. return gc.get(2) + 1;
  212. }
  213.  
  214. /**
  215. * 获取本月的开始时间
  216. * @return
  217. */
  218. public static Date getBeginDayOfMonth() {
  219. Calendar calendar=Calendar.getInstance();
  220. calendar.set(getNowYear(), getNowMonth()-1, 1);
  221. return getDayStartTime(calendar.getTime());
  222. }
  223.  
  224. /**
  225. * 获取本月的结束时间
  226. * @return
  227. */
  228. public static Date getEndDayOfMonth() {
  229. Calendar calendar=Calendar.getInstance();
  230. calendar.set(getNowYear(), getNowMonth()-1, 1);
  231. int day = calendar.getActualMaximum(5);
  232. calendar.set(getNowYear(), getNowMonth()-1, day);
  233. return getDayEndTime(calendar.getTime());
  234. }
  235.  
  236. /**
  237. * 获取上月的开始时间
  238. * @return
  239. */
  240. public static Date getBeginDayOfLastMonth() {
  241. Calendar calendar=Calendar.getInstance();
  242. calendar.set(getNowYear(), getNowMonth()-2, 1);
  243. return getDayStartTime(calendar.getTime());
  244. }
  245.  
  246. /**
  247. * 获取上月的结束时间
  248. * @return
  249. */
  250. public static Date getEndDayOfLastMonth() {
  251. Calendar calendar=Calendar.getInstance();
  252. calendar.set(getNowYear(), getNowMonth()-2, 1);
  253. int day = calendar.getActualMaximum(5);
  254. calendar.set(getNowYear(), getNowMonth()-2, day);
  255. return getDayEndTime(calendar.getTime());
  256. }
  257.  
  258. /**
  259. * 获取本年的开始时间
  260. * @return
  261. */
  262. public static java.util.Date getBeginDayOfYear() {
  263. Calendar cal=Calendar.getInstance();
  264. cal.set(Calendar.YEAR, getNowYear());
  265. cal.set(Calendar.MONTH, Calendar.JANUARY);
  266. cal.set(Calendar.DATE, 1);
  267. return getDayStartTime(cal.getTime());
  268. }
  269.  
  270. /**
  271. * 获取本年的结束时间
  272. * @return
  273. */
  274. public static java.util.Date getEndDayOfYear() {
  275. Calendar cal = Calendar.getInstance();
  276. cal.set(Calendar.YEAR, getNowYear());
  277. cal.set(Calendar.MONTH, Calendar.DECEMBER);
  278. cal.set(Calendar.DATE, 31);
  279. return getDayEndTime(cal.getTime());
  280. }
  281.  
  282. /**
  283. * 两个日期相减得到的天数
  284. * @param beginDate
  285. * @param endDate
  286. * @return
  287. */
  288. public static int getDiffDays(Date beginDate, Date endDate) {
  289. if(beginDate==null||endDate==null) {
  290. throw new IllegalArgumentException("getDiffDays param is null!");
  291. }
  292. long diff=(endDate.getTime()-beginDate.getTime())/(1000*60*60*24);
  293. int days = new Long(diff).intValue();
  294. return days;
  295. }
  296.  
  297. /**
  298. * 两个日期相减得到的毫秒数
  299. * @param beginDate
  300. * @param endDate
  301. * @return
  302. */
  303. public static long dateDiff(Date beginDate, Date endDate) {
  304. long date1ms=beginDate.getTime();
  305. long date2ms=endDate.getTime();
  306. return date2ms-date1ms;
  307. }
  308.  
  309. /**
  310. * 获取两个日期中的最大日起
  311. * @param beginDate
  312. * @param endDate
  313. * @return
  314. */
  315. public static Date max(Date beginDate, Date endDate) {
  316. if(beginDate==null) {
  317. return endDate;
  318. }
  319. if(endDate==null) {
  320. return beginDate;
  321. }
  322. if(beginDate.after(endDate)) {//beginDate日期大于endDate
  323. return beginDate;
  324. }
  325. return endDate;
  326. }
  327.  
  328. /**
  329. * 获取两个日期中的最小日期
  330. * @param beginDate
  331. * @param endDate
  332. * @return
  333. */
  334. public static Date min(Date beginDate, Date endDate) {
  335. if(beginDate==null) {
  336. return endDate;
  337. }
  338. if(endDate==null) {
  339. return beginDate;
  340. }
  341. if(beginDate.after(endDate)) {
  342. return endDate;
  343. }
  344. return beginDate;
  345. }
  346.  
  347. /**
  348. * 获取某月该季度的第一个月
  349. * @param date
  350. * @return
  351. */
  352. public static Date getFirstSeasonDate(Date date) {
  353. final int[] SEASON={ 1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 4 };
  354. Calendar cal=Calendar.getInstance();
  355. cal.setTime(date);
  356. int sean = SEASON[cal.get(Calendar.MONTH)];
  357. cal.set(Calendar.MONTH, sean*3-3);
  358. return cal.getTime();
  359. }
  360.  
  361. /**
  362. * 返回某个日期下几天的日期
  363. * @param date
  364. * @param i
  365. * @return
  366. */
  367. public static Date getNextDay(Date date, int i) {
  368. Calendar cal=new GregorianCalendar();
  369. cal.setTime(date);
  370. cal.set(Calendar.DATE,cal.get(Calendar.DATE)+i);
  371. return cal.getTime();
  372. }
  373.  
  374. /**
  375. * 返回某个日期前几天的日期
  376. * @param date
  377. * @param i
  378. * @return
  379. */
  380. public static Date getFrontDay(Date date, int i) {
  381. Calendar cal=new GregorianCalendar();
  382. cal.setTime(date);
  383. cal.set(Calendar.DATE, cal.get(Calendar.DATE)-i);
  384. return cal.getTime();
  385. }
  386.  
  387. /**
  388. * 获取某年某月按天切片日期集合(某个月间隔多少天的日期集合)
  389. * @param beginYear
  390. * @param beginMonth
  391. * @param k
  392. * @return
  393. */
  394. @SuppressWarnings({ "unchecked", "rawtypes" })
  395. public static List getTimeList(int beginYear, int beginMonth, int k) {
  396. List list = new ArrayList();
  397. Calendar begincal=new GregorianCalendar(beginYear,beginMonth, 1);
  398. int max = begincal.getActualMaximum(Calendar.DATE);
  399. for (int i = 1; i < max; i = i + k) {
  400. list.add(begincal.getTime());
  401. begincal.add(Calendar.DATE, k);
  402. }
  403. begincal = new GregorianCalendar(beginYear, beginMonth, max);
  404. list.add(begincal.getTime());
  405. return list;
  406. }
  407.  
  408. /**
  409. * 获取某年某月到某年某月按天的切片日期集合(间隔天数的集合)
  410. * @param beginYear
  411. * @param beginMonth
  412. * @param endYear
  413. * @param endMonth
  414. * @param k
  415. * @return
  416. */
  417. @SuppressWarnings({ "rawtypes", "unchecked" })
  418. public static List getTimeList(int beginYear,int beginMonth,int endYear,int endMonth, int k) {
  419. List list = new ArrayList();
  420. if (beginYear==endYear){
  421. for(int j=beginMonth;j<=endMonth;j++){
  422. list.add(getTimeList(beginYear,j,k));
  423. }
  424. }else{
  425. {
  426. for(int j=beginMonth;j<12;j++){
  427. list.add(getTimeList(beginYear,j,k));
  428. }
  429. for(int i=beginYear+1;i<endYear;i++) {
  430. for (int j=0; j<12; j++) {
  431. list.add(getTimeList(i,j,k));
  432. }
  433. }
  434. for (int j=0;j <= endMonth; j++) {
  435. list.add(getTimeList(endYear, j, k));
  436. }
  437. }
  438. }
  439. return list;
  440. }
  441.  
  442. //=================================时间格式转换==========================
  443.  
  444. /**
  445. * date类型进行格式化输出(返回类型:String)
  446. * @param date
  447. * @return
  448. */
  449. public static String dateFormat(Date date) {
  450. SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  451. String dateString = formatter.format(date);
  452. return dateString;
  453. }
  454.  
  455. /**
  456. * 将"2015-08-31 21:08:06"型字符串转化为Date
  457. * @param str
  458. * @return
  459. * @throws ParseException
  460. */
  461. public static Date StringToDate(String str) throws ParseException{
  462. SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  463. Date date = (Date) formatter.parse(str);
  464. return date;
  465. }
  466.  
  467. /**
  468. * 将CST时间类型字符串进行格式化输出
  469. * @param str
  470. * @return
  471. * @throws ParseException
  472. */
  473. public static String CSTFormat(String str) throws ParseException{
  474. SimpleDateFormat formatter = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy", Locale.US);
  475. Date date = (Date) formatter.parse(str);
  476. return dateFormat(date);
  477. }
  478.  
  479. /**
  480. * 将long类型转化为Date
  481. * @param str
  482. * @return
  483. * @throws ParseException
  484. */
  485. public static Date LongToDare(long str) throws ParseException{
  486. return new Date(str * 1000);
  487. }
  488.  
  489. //====================================其他常见日期操作方法======================
  490.  
  491. /**
  492. * 判断当前日期是否在[startDate, endDate]区间
  493. *
  494. * @param startDate 开始日期
  495. * @param endDate 结束日期
  496. * @author jqlin
  497. * @return
  498. */
  499. public static boolean isEffectiveDate(Date startDate, Date endDate){
  500. if(startDate == null || endDate == null){
  501. return false;
  502. }
  503. long currentTime = new Date().getTime();
  504. if(currentTime >= startDate.getTime()
  505. && currentTime <= endDate.getTime()){
  506. return true;
  507. }
  508. return false;
  509. }
  510.  
  511. /**
  512. * 得到二个日期间的间隔天数
  513. * @param secondString:后一个日期
  514. * @param firstString:前一个日期
  515. * @return
  516. */
  517. public static String getTwoDay(String secondString, String firstString) {
  518. SimpleDateFormat myFormatter = new SimpleDateFormat("yyyy-MM-dd");
  519. long day = 0;
  520. try {
  521. java.util.Date secondTime = myFormatter.parse(secondString);
  522. java.util.Date firstTime = myFormatter.parse(firstString);
  523. day = (secondTime.getTime() - firstTime.getTime()) / (24 * 60 * 60 * 1000);
  524. } catch (Exception e) {
  525. return "";
  526. }
  527. return day + "";
  528. }
  529.  
  530. /**
  531. * 时间前推或后推分钟,其中JJ表示分钟.
  532. * @param StringTime:时间
  533. * @param minute:分钟(有正负之分)
  534. * @return
  535. */
  536. public static String getPreTime(String StringTime, String minute) {
  537. SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  538. String mydate1 = "";
  539. try {
  540. Date date1 = format.parse(StringTime);
  541. long Time = (date1.getTime() / 1000) + Integer.parseInt(minute) * 60;
  542. date1.setTime(Time * 1000);
  543. mydate1 = format.format(date1);
  544. } catch (Exception e) {
  545. return "";
  546. }
  547. return mydate1;
  548. }
  549.  
  550. /**
  551. * 将短时间格式字符串转换为时间 yyyy-MM-dd
  552. *
  553. * @param strDate
  554. * @return
  555. */
  556. public static Date strToDate(String strDate) {
  557. SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
  558. ParsePosition pos = new ParsePosition(0);
  559. Date strtodate = formatter.parse(strDate, pos);
  560. return strtodate;
  561. }
  562.  
  563. /**
  564. * 得到一个时间延后或前移几天的时间
  565. * @param nowdate:时间
  566. * @param delay:前移或后延的天数
  567. * @return
  568. */
  569. public static String getNextDay(String nowdate, String delay) {
  570. try{
  571. SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
  572. String mdate = "";
  573. Date d = strToDate(nowdate);
  574. long myTime = (d.getTime() / 1000) + Integer.parseInt(delay) * 24 * 60 * 60;
  575. d.setTime(myTime * 1000);
  576. mdate = format.format(d);
  577. return mdate;
  578. }catch(Exception e){
  579. return "";
  580. }
  581. }
  582.  
  583. /**
  584. * 判断是否闰年
  585. * @param ddate
  586. * @return
  587. */
  588. public static boolean isLeapYear(String ddate) {
  589. /**
  590. * 详细设计: 1.被400整除是闰年,否则: 2.不能被4整除则不是闰年 3.能被4整除同时不能被100整除则是闰年
  591. * 3.能被4整除同时能被100整除则不是闰年
  592. */
  593. Date d = strToDate(ddate);
  594. GregorianCalendar gc = (GregorianCalendar) Calendar.getInstance();
  595. gc.setTime(d);
  596. int year = gc.get(Calendar.YEAR);
  597. if ((year % 400) == 0){
  598. return true;
  599. }else if ((year % 4) == 0){
  600. if ((year % 100) == 0){
  601. return false;
  602. }else{
  603. return true;
  604. }
  605. }else{
  606. return false;
  607. }
  608. }
  609.  
  610. /**
  611. * 返回美国时间格式
  612. * @param str
  613. * @return
  614. */
  615. public static String getEDate(String str) {
  616. SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
  617. ParsePosition pos = new ParsePosition(0);
  618. Date strtodate = formatter.parse(str, pos);
  619. String j = strtodate.toString();
  620. String[] k = j.split(" ");
  621. return k[2] + k[1].toUpperCase() + k[5].substring(2, 4);
  622. }
  623.  
  624. /**
  625. * 判断二个时间是否在同一个周
  626. * @param date1
  627. * @param date2
  628. * @return
  629. */
  630. public static boolean isSameWeekDates(Date date1, Date date2) {
  631. Calendar cal1 = Calendar.getInstance();
  632. Calendar cal2 = Calendar.getInstance();
  633. cal1.setTime(date1);
  634. cal2.setTime(date2);
  635. int subYear = cal1.get(Calendar.YEAR) - cal2.get(Calendar.YEAR);
  636. if(0 == subYear) {
  637. if (cal1.get(Calendar.WEEK_OF_YEAR) == cal2.get(Calendar.WEEK_OF_YEAR)){
  638. return true;
  639. }
  640. }else if(1 == subYear && 11 == cal2.get(Calendar.MONTH)) {
  641. // 如果12月的最后一周横跨来年第一周的话则最后一周即算做来年的第一周
  642. if (cal1.get(Calendar.WEEK_OF_YEAR) == cal2.get(Calendar.WEEK_OF_YEAR)){
  643. return true;
  644. }
  645. }else if (-1 == subYear && 11 == cal1.get(Calendar.MONTH)) {
  646. if (cal1.get(Calendar.WEEK_OF_YEAR) == cal2.get(Calendar.WEEK_OF_YEAR)){
  647. return true;
  648. }
  649. }
  650. return false;
  651. }
  652.  
  653. /**
  654. * 产生周序列,即得到当前时间所在的年度是第几周
  655. * @return
  656. */
  657. public static String getSeqWeek() {
  658. Calendar c = Calendar.getInstance(Locale.CHINA);
  659. String week = Integer.toString(c.get(Calendar.WEEK_OF_YEAR));
  660. if (week.length() == 1)
  661. week = "0" + week;
  662. String year = Integer.toString(c.get(Calendar.YEAR));
  663. return year +"年第"+ week+"周";
  664. }
  665.  
  666. /**
  667. * 获得一个日期所在的周的星期几的日期,如要找出2002年2月3日所在周的星期一是几号
  668. * @param sdate:日期
  669. * @param num:星期几(星期天是一周的第一天)
  670. * @return
  671. */
  672. public static String getWeek(String sdate, String num) {
  673. // 再转换为时间
  674. Date dd = strToDate(sdate);
  675. Calendar c = Calendar.getInstance();
  676. c.setTime(dd);
  677. if (num.equals("1")) // 返回星期一所在的日期
  678. c.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
  679. else if (num.equals("2")) // 返回星期二所在的日期
  680. c.set(Calendar.DAY_OF_WEEK, Calendar.TUESDAY);
  681. else if (num.equals("3")) // 返回星期三所在的日期
  682. c.set(Calendar.DAY_OF_WEEK, Calendar.WEDNESDAY);
  683. else if (num.equals("4")) // 返回星期四所在的日期
  684. c.set(Calendar.DAY_OF_WEEK, Calendar.THURSDAY);
  685. else if (num.equals("5")) // 返回星期五所在的日期
  686. c.set(Calendar.DAY_OF_WEEK, Calendar.FRIDAY);
  687. else if (num.equals("6")) // 返回星期六所在的日期
  688. c.set(Calendar.DAY_OF_WEEK, Calendar.SATURDAY);
  689. else if (num.equals("0")) // 返回星期日所在的日期
  690. c.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
  691. return new SimpleDateFormat("yyyy-MM-dd").format(c.getTime());
  692. }
  693.  
  694. /**
  695. * 根据一个日期,返回是星期几的字符串
  696. * @param sdate
  697. * @return
  698. */
  699. public static String getWeek(String sdate) {
  700. // 再转换为时间
  701. Date date = strToDate(sdate);
  702. Calendar c = Calendar.getInstance();
  703. c.setTime(date);
  704. // int hour=c.get(Calendar.DAY_OF_WEEK);
  705. // hour中存的就是星期几了,其范围 1~7
  706. // 1=星期日 7=星期六,其他类推
  707. return new SimpleDateFormat("EEEE").format(c.getTime());
  708. }
  709.  
  710. /**
  711. * 根据一个日期,返回是星期几的字符串
  712. * @param sdate
  713. * @return
  714. */
  715. public static String getWeekStr(String sdate){
  716. String str = "";
  717. str = getWeek(sdate);
  718. if("1".equals(str)){
  719. str = "星期日";
  720. }else if("2".equals(str)){
  721. str = "星期一";
  722. }else if("3".equals(str)){
  723. str = "星期二";
  724. }else if("4".equals(str)){
  725. str = "星期三";
  726. }else if("5".equals(str)){
  727. str = "星期四";
  728. }else if("6".equals(str)){
  729. str = "星期五";
  730. }else if("7".equals(str)){
  731. str = "星期六";
  732. }
  733. return str;
  734. }
  735.  
  736. /**
  737. * 两个时间之间的天数
  738. * @param date1
  739. * @param date2
  740. * @return
  741. */
  742. public static long getDays(String date1, String date2) {
  743. if (date1 == null || date1.equals(""))
  744. return 0;
  745. if (date2 == null || date2.equals(""))
  746. return 0;
  747. // 转换为标准时间
  748. SimpleDateFormat myFormatter = new SimpleDateFormat("yyyy-MM-dd");
  749. java.util.Date date = null;
  750. java.util.Date mydate = null;
  751. try {
  752. date = myFormatter.parse(date1);
  753. mydate = myFormatter.parse(date2);
  754. } catch (Exception e) {
  755. }
  756. long day = (date.getTime() - mydate.getTime()) / (24 * 60 * 60 * 1000);
  757. return day;
  758. }
  759.  
  760. /**
  761. * 形成如下的日历 , 根据传入的一个时间返回一个结构 星期日 星期一 星期二 星期三 星期四 星期五 星期六 下面是当月的各个时间
  762. * 此函数返回该日历第一行星期日所在的日期
  763. * @param sdate
  764. * @return
  765. */
  766. public static String getNowMonth(String sdate) {
  767. // 取该时间所在月的一号
  768. sdate = sdate.substring(0, 8) + "01";
  769.  
  770. // 得到这个月的1号是星期几
  771. Date date = strToDate(sdate);
  772. Calendar c = Calendar.getInstance();
  773. c.setTime(date);
  774. int u = c.get(Calendar.DAY_OF_WEEK);
  775. String newday = getNextDay(sdate, (1 - u) + "");
  776. return newday;
  777. }
  778.  
  779. /**
  780. * 根据用户传入的时间表示格式,返回当前时间的格式 如果是yyyyMMdd,注意字母y不能大写
  781. * @param sformat
  782. * @return
  783. */
  784. public static String getUserDate(String sformat) {
  785. Date currentTime = new Date();
  786. SimpleDateFormat formatter = new SimpleDateFormat(sformat);
  787. String dateString = formatter.format(currentTime);
  788. return dateString;
  789. }
  790.  
  791. /**
  792. * 返回一个i位数的随机数
  793. * @param i
  794. * @return
  795. */
  796. public static String getRandom(int i) {
  797. Random jjj = new Random();
  798. // int suiJiShu = jjj.nextInt(9);
  799. if (i == 0)
  800. return "";
  801. String jj = "";
  802. for (int k = 0; k < i; k++) {
  803. jj = jj + jjj.nextInt(9);
  804. }
  805. return jj;
  806. }
  807.  
  808. /**
  809. * 取得数据库主键 生成格式为yyyymmddhhmmss+k位随机数
  810. * @param k:表示是取几位随机数,可以自己定
  811. * @return
  812. */
  813. public static String getNo(int k) {
  814. return getUserDate("yyyyMMddhhmmss") + getRandom(k);
  815. }
  816.  
  817. public static void main(String[] args) {
  818. System.out.println(getNo(8));
  819. }
  820. }

  

超详细的Java时间工具类的更多相关文章

  1. Java日期工具类,Java时间工具类,Java时间格式化

    Java日期工具类,Java时间工具类,Java时间格式化 >>>>>>>>>>>>>>>>>&g ...

  2. 小记Java时间工具类

    小记Java时间工具类 废话不多说,这里主要记录以下几个工具 两个时间只差(Data) 获取时间的格式 格式化时间 返回String 两个时间只差(String) 获取两个时间之间的日期.月份.年份 ...

  3. java时间工具类

    在项目中,很多地方需要根据时间获取相应的数据,将时间格式化,或者时间比较等相关操作.一个良好的工具类不仅可以减少代码冗余,还能促进业务处理,加快进度. /** * @author: lxw * @Da ...

  4. 一个好的Java时间工具类DateTime

    此类的灵感来源于C# 虽然网上有什么date4j,但是jar太纠结了,先给出源码,可以继承到自己的util包中,作为一个资深程序员,我相信都有不少好的util工具类,我也希望经过此次分享,能带动技术大 ...

  5. JAVA时间工具类,在维护的项目里的

    package com.inspur.jobSchedule.util; import org.apache.commons.lang3.time.DateUtils; import org.apac ...

  6. Java 时间工具类

    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");  1.Calendar 转化 String  ...

  7. java时间工具类,时间相互转换

    /* * @author XueWeiWei * @date 2019/8/26 16:22 */ package com.nps.utils; import java.text.ParseExcep ...

  8. JAVA时间工具类用法

    1.获得N天前的TIMESTAMP Calendar cl = Calendar.getInstance(); cl.add(Calendar.DAY_OF_YEAR, -7); Date date ...

  9. J2EE进阶(十四)超详细的Java后台开发面试题之Spring IOC与AOP

    J2EE进阶(十四)超详细的Java后台开发面试题之Spring IOC与AOP 前言   搜狐畅游笔试题中有一道问答题涉及到回答谈谈对Spring IOC与AOP的理解.特将相关内容进行整理.    ...

随机推荐

  1. Codeforces816B Karen and Coffee 2017-06-27 15:18 39人阅读 评论(0) 收藏

    B. Karen and Coffee time limit per test 2.5 seconds memory limit per test 512 megabytes input standa ...

  2. hdu 5055

    http://acm.hdu.edu.cn/showproblem.php?pid=5055 n个digit能组合出的最大无前导0奇数 无聊的模拟 #include <cstdio> #i ...

  3. Scala_方法、函数、柯里化

    方法.函数.柯里化 方法 声明方法: scala> def m1(x:Int,y:Int):Int = {     | x + y     | }m1: (x: Int, y: Int)Ints ...

  4. codeforces 434D

    题意:有n<=50个点,每个点有xi有[li, ri]种取值,-100 <= li <= ri <= 100,并且给定m<=100条边,每条边为u,v,d表示xu< ...

  5. 一些仪器的解码程序(delphi)

    http://www.jiandande.com/html/ITzixun-jishu/Lisyanjiuyuan/2013/0204/1600_3.html 看了后觉得不错,可能有需要的 ----- ...

  6. [leetcode 8] String to Integer

    1 题目: Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input ...

  7. 为什么在UDP包中不能获取发包方的地址

    首先,我们要先了解一下UDP包的结构. 图1 UDP报文格式 从图1,我们可以看出,从UDP包中,我们可以获取的信息只有源端口和目的地端口.我们不能获取到源IP因为报文中没有源IP.真正包含IP地址的 ...

  8. Winform相关

    (1)C# WinForm程序退出的方法 1.this.Close();   只是关闭当前窗口,若不是主窗体的话,是无法退出程序的,另外若有托管线程(非主线程),也无法干净地退出: 2.Applica ...

  9. 深入理解Aspnet Core之Identity(5)

    主题 本篇我将会介绍验证用户的机制当账户被创建的时候,同样 这个过程主要有IUserValidator这个接口来实现的,和密码验证一样Identity同样也内置已经实现好的账户验证.账户验证的比较简单 ...

  10. 剑指offer编程题Java实现——面试题4后的相关题目

    题目描述: 有两个排序的数字A1和A2,内存在A1的末尾有足够多的空余空间容纳A2.请实现一个函数,把A2中的所有数字插入到A1中并且所有的数字是排序的. 还是利用从后向前比较两个数组中的数字的方式来 ...