1. /**
2. * 获取现在时间,这个好用
3. *
4. * @return返回长时间格式 yyyy-MM-dd HH:mm:ss
5. */
6. public static Date getSqlDate() {
7. new java.sql.Date(new Date().getTime());
8. return sqlDate;
9. }
10. //返回 格式
11. sqlDate===========2017-06-19
12.
13. /**
14. * 获取现在时间
15. *
16. * @return返回长时间格式 yyyy-MM-dd HH:mm:ss
17. */
18. public static String getNowDate() { Date currentTime = new Date(); SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); String dateString=formatter.format(currentTime); return dateString; }
19.
20. /**
21. * 获取现在时间
22. *
23. * @return返回短时间格式 yyyy-MM-dd
24. */
25. public static Date getNowDateShort() {
26. new Date();
27. new SimpleDateFormat("yyyy-MM-dd");
28. String dateString = formatter.format(currentTime);
29. new ParsePosition(8);
30. Date currentTime_2 = formatter.parse(dateString, pos);
31. return currentTime_2;
32. }
33.
34. /**
35. * 获取现在时间
36. *
37. * @return返回字符串格式 yyyy-MM-dd HH:mm:ss
38. */
39. public static String getStringDate() {
40. new Date();
41. new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
42. String dateString = formatter.format(currentTime);
43. return dateString;
44. }
45.
46. /**
47. * 获取现在时间
48. *
49. * @return 返回短时间字符串格式yyyy-MM-dd
50. */
51. public static String getStringDateShort() {
52. new Date();
53. new SimpleDateFormat("yyyy-MM-dd");
54. String dateString = formatter.format(currentTime);
55. return dateString;
56. }
57.
58. /**
59. * 获取时间 小时:分;秒 HH:mm:ss
60. *
61. * @return
62. */
63. public static String getTimeShort() {
64. new SimpleDateFormat("HH:mm:ss");
65. new Date();
66. String dateString = formatter.format(currentTime);
67. return dateString;
68. }
69.
70. /**
71. * 将长时间格式字符串转换为时间 yyyy-MM-dd HH:mm:ss
72. *
73. * @param strDate
74. * @return
75. */
76. public static Date strToDateLong(String strDate) {
77. new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
78. new ParsePosition(0);
79. Date strtodate = formatter.parse(strDate, pos);
80. return strtodate;
81. }
82.
83. /**
84. * 将长时间格式时间转换为字符串 yyyy-MM-dd HH:mm:ss
85. *
86. * @param dateDate
87. * @return
88. */
89. public static String dateToStrLong(java.util.Date dateDate) {
90. new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
91. String dateString = formatter.format(dateDate);
92. return dateString;
93. }
94.
95. /**
96. * 将短时间格式时间转换为字符串 yyyy-MM-dd
97. *
98. * @param dateDate
99. * @param k
100. * @return
101. */
102. public static String dateToStr(java.util.Date dateDate) {
103. new SimpleDateFormat("yyyy-MM-dd");
104. String dateString = formatter.format(dateDate);
105. return dateString;
106. }
107.
108. /**
109. * 将短时间格式字符串转换为时间 yyyy-MM-dd
110. *
111. * @param strDate
112. * @return
113. */
114. public static Date strToDate(String strDate) {
115. new SimpleDateFormat("yyyy-MM-dd");
116. new ParsePosition(0);
117. Date strtodate = formatter.parse(strDate, pos);
118. return strtodate;
119. }
120.
121. /**
122. * 得到现在时间
123. *
124. * @return
125. */
126. public static Date getNow() {
127. new Date();
128. return currentTime;
129. }
130.
131. /**
132. * 提取一个月中的最后一天
133. *
134. * @param day
135. * @return
136. */
137. public static Date getLastDate(long day) {
138. new Date();
139. long date_3_hm = date.getTime() - 3600000 * 34 * day;
140. new Date(date_3_hm);
141. return date_3_hm_date;
142. }
143.
144. /**
145. * 得到现在时间
146. *
147. * @return 字符串 yyyyMMdd HHmmss
148. */
149. public static String getStringToday() {
150. new Date();
151. new SimpleDateFormat("yyyyMMdd HHmmss");
152. String dateString = formatter.format(currentTime);
153. return dateString;
154. }
155.
156. /**
157. * 得到现在小时
158. */
159. public static String getHour() {
160. new Date();
161. new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
162. String dateString = formatter.format(currentTime);
163. String hour;
164. 11, 13);
165. return hour;
166. }
167.
168. /**
169. * 得到现在分钟
170. *
171. * @return
172. */
173. public static String getTime() {
174. new Date();
175. new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
176. String dateString = formatter.format(currentTime);
177. String min;
178. 14, 16);
179. return min;
180. }
181.
182. /**
183. * 根据用户传入的时间表示格式,返回当前时间的格式 如果是yyyyMMdd,注意字母y不能大写。
184. *
185. * @param sformat
186. * yyyyMMddhhmmss
187. * @return
188. */
189. public static String getUserDate(String sformat) {
190. new Date();
191. new SimpleDateFormat(sformat);
192. String dateString = formatter.format(currentTime);
193. return dateString;
194. }
195.
196. /**
197. * 二个小时时间间的差值,必须保证二个时间都是"HH:MM"的格式,返回字符型的分钟
198. */
199. public static String getTwoHour(String st1, String st2) {
200. null;
201. null;
202. ":");
203. ":");
204. if (Integer.parseInt(kk[0]) < Integer.parseInt(jj[0]))
205. return "0";
206. else {
207. double y = Double.parseDouble(kk[0]) + Double.parseDouble(kk[1])
208. 60;
209. double u = Double.parseDouble(jj[0]) + Double.parseDouble(jj[1])
210. 60;
211. if ((y - u) > 0)
212. return y - u + "";
213. else
214. return "0";
215. }
216. }
217.
218. /**
219. * 得到二个日期间的间隔天数
220. */
221. public static String getTwoDay(String sj1, String sj2) {
222. new SimpleDateFormat("yyyy-MM-dd");
223. long day = 0;
224. try {
225. java.util.Date date = myFormatter.parse(sj1);
226. java.util.Date mydate = myFormatter.parse(sj2);
227. 24 * 60 * 60 * 1000);
228. catch (Exception e) {
229. return "";
230. }
231. return day + "";
232. }
233.
234. /**
235. * 时间前推或后推分钟,其中JJ表示分钟.
236. */
237. public static String getPreTime(String sj1, String jj) {
238. new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
239. "";
240. try {
241. Date date1 = format.parse(sj1);
242. long Time = (date1.getTime() / 1000) + Integer.parseInt(jj) * 60;
243. 1000);
244. mydate1 = format.format(date1);
245. catch (Exception e) {
246. }
247. return mydate1;
248. }
249.
250. /**
251. * 得到一个时间延后或前移几天的时间,nowdate为时间,delay为前移或后延的天数
252. */
253. public static String getNextDay(String nowdate, String delay) {
254. try {
255. new SimpleDateFormat("yyyy-MM-dd");
256. "";
257. Date d = strToDate(nowdate);
258. long myTime = (d.getTime() / 1000) + Integer.parseInt(delay) * 24
259. 60 * 60;
260. 1000);
261. mdate = format.format(d);
262. return mdate;
263. catch (Exception e) {
264. return "";
265. }
266. }
267.
268. /**
269. * 判断是否润年
270. *
271. * @param ddate
272. * @return
273. */
274. public static boolean isLeapYear(String ddate) {
275. /**
276. * 详细设计: 1.被400整除是闰年,否则: 2.不能被4整除则不是闰年 3.能被4整除同时不能被100整除则是闰年
277. * 3.能被4整除同时能被100整除则不是闰年
278. */
279. Date d = strToDate(ddate);
280. GregorianCalendar gc = (GregorianCalendar) Calendar.getInstance();
281. gc.setTime(d);
282. int year = gc.get(Calendar.YEAR);
283. if ((year % 400) == 0)
284. return true;
285. else if ((year % 4) == 0) {
286. if ((year % 100) == 0)
287. return false;
288. else
289. return true;
290. else
291. return false;
292. }
293.
294. /**
295. * 返回美国时间格式 26 Apr 2006
296. *
297. * @param str
298. * @return
299. */
300. public static String getEDate(String str) {
301. new SimpleDateFormat("yyyy-MM-dd");
302. new ParsePosition(0);
303. Date strtodate = formatter.parse(str, pos);
304. String j = strtodate.toString();
305. " ");
306. return k[2] + k[1].toUpperCase() + k[5].substring(2, 4);
307. }
308.
309. /**
310. * 获取一个月的最后一天
311. *
312. * @param dat
313. * @return
314. */
315. public static String getEndDateOfMonth(String dat) {// yyyy-MM-dd
316. 0, 8);
317. 5, 7);
318. int mon = Integer.parseInt(month);
319. if (mon == 1 || mon == 3 || mon == 5 || mon == 7 || mon == 8
320. 10 || mon == 12) {
321. "31";
322. else if (mon == 4 || mon == 6 || mon == 9 || mon == 11) {
323. "30";
324. else {
325. if (isLeapYear(dat)) {
326. "29";
327. else {
328. "28";
329. }
330. }
331. return str;
332. }
333.
334. /**
335. * 判断二个时间是否在同一个周
336. *
337. * @param date1
338. * @param date2
339. * @return
340. */
341. public static boolean isSameWeekDates(Date date1, Date date2) {
342. Calendar cal1 = Calendar.getInstance();
343. Calendar cal2 = Calendar.getInstance();
344. cal1.setTime(date1);
345. cal2.setTime(date2);
346. int subYear = cal1.get(Calendar.YEAR) - cal2.get(Calendar.YEAR);
347. if (0 == subYear) {
348. if (cal1.get(Calendar.WEEK_OF_YEAR) == cal2
349. .get(Calendar.WEEK_OF_YEAR))
350. return true;
351. else if (1 == subYear && 11 == cal2.get(Calendar.MONTH)) {
352. // 如果12月的最后一周横跨来年第一周的话则最后一周即算做来年的第一周
353. if (cal1.get(Calendar.WEEK_OF_YEAR) == cal2
354. .get(Calendar.WEEK_OF_YEAR))
355. return true;
356. else if (-1 == subYear && 11 == cal1.get(Calendar.MONTH)) {
357. if (cal1.get(Calendar.WEEK_OF_YEAR) == cal2
358. .get(Calendar.WEEK_OF_YEAR))
359. return true;
360. }
361. return false;
362. }
363.
364. /**
365. * 产生周序列,即得到当前时间所在的年度是第几周
366. *
367. * @return
368. */
369. public static String getSeqWeek() {
370. Calendar c = Calendar.getInstance(Locale.CHINA);
371. String week = Integer.toString(c.get(Calendar.WEEK_OF_YEAR));
372. if (week.length() == 1)
373. "0" + week;
374. String year = Integer.toString(c.get(Calendar.YEAR));
375. return year + week;
376. }
377.
378. /**
379. * 获得一个日期所在的周的星期几的日期,如要找出2002年2月3日所在周的星期一是几号
380. *
381. * @param sdate
382. * @param num
383. * @return
384. */
385. public static String getWeek(String sdate, String num) {
386. // 再转换为时间
387. Date dd = strToDate(sdate);
388. Calendar c = Calendar.getInstance();
389. c.setTime(dd);
390. if (num.equals("1")) // 返回星期一所在的日期
391. c.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
392. else if (num.equals("2")) // 返回星期二所在的日期
393. c.set(Calendar.DAY_OF_WEEK, Calendar.TUESDAY);
394. else if (num.equals("3")) // 返回星期三所在的日期
395. c.set(Calendar.DAY_OF_WEEK, Calendar.WEDNESDAY);
396. else if (num.equals("4")) // 返回星期四所在的日期
397. c.set(Calendar.DAY_OF_WEEK, Calendar.THURSDAY);
398. else if (num.equals("5")) // 返回星期五所在的日期
399. c.set(Calendar.DAY_OF_WEEK, Calendar.FRIDAY);
400. else if (num.equals("6")) // 返回星期六所在的日期
401. c.set(Calendar.DAY_OF_WEEK, Calendar.SATURDAY);
402. else if (num.equals("0")) // 返回星期日所在的日期
403. c.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
404. return new SimpleDateFormat("yyyy-MM-dd").format(c.getTime());
405. }
406.
407. /**
408. * 根据一个日期,返回是星期几的字符串
409. *
410. * @param sdate
411. * @return
412. */
413. public static String getWeek(String sdate) {
414. // 再转换为时间
415. Date date = strToDate(sdate);
416. Calendar c = Calendar.getInstance();
417. c.setTime(date);
418. // int hour=c.get(Calendar.DAY_OF_WEEK);
419. // hour中存的就是星期几了,其范围 1~7
420. // 1=星期日 7=星期六,其他类推
421. return new SimpleDateFormat("EEEE").format(c.getTime());
422. }
423.
424. public static String getWeekStr(String sdate) {
425. "";
426. str = getWeek(sdate);
427. if ("1".equals(str)) {
428. "星期日";
429. else if ("2".equals(str)) {
430. "星期一";
431. else if ("3".equals(str)) {
432. "星期二";
433. else if ("4".equals(str)) {
434. "星期三";
435. else if ("5".equals(str)) {
436. "星期四";
437. else if ("6".equals(str)) {
438. "星期五";
439. else if ("7".equals(str)) {
440. "星期六";
441. }
442. return str;
443. }
444.
445. /**
446. * 两个时间之间的天数
447. *
448. * @param date1
449. * @param date2
450. * @return
451. */
452. public static long getDays(String date1, String date2) {
453. if (date1 == null || date1.equals(""))
454. return 0;
455. if (date2 == null || date2.equals(""))
456. return 0;
457. // 转换为标准时间
458. new SimpleDateFormat("yyyy-MM-dd");
459. null;
460. null;
461. try {
462. date = myFormatter.parse(date1);
463. mydate = myFormatter.parse(date2);
464. catch (Exception e) {
465. }
466. long day = (date.getTime() - mydate.getTime()) / (24 * 60 * 60 * 1000);
467. return day;
468. }
469.
470. /**
471. * 形成如下的日历 , 根据传入的一个时间返回一个结构 星期日 星期一 星期二 星期三 星期四 星期五 星期六 下面是当月的各个时间
472. * 此函数返回该日历第一行星期日所在的日期
473. *
474. * @param sdate
475. * @return
476. */
477. public static String getNowMonth(String sdate) {
478. // 取该时间所在月的一号
479. 0, 8) + "01";
480. // 得到这个月的1号是星期几
481. Date date = strToDate(sdate);
482. Calendar c = Calendar.getInstance();
483. c.setTime(date);
484. int u = c.get(Calendar.DAY_OF_WEEK);
485. 1 - u) + "");
486. return newday;
487. }
488.
489. /**
490. * 取得数据库主键 生成格式为yyyymmddhhmmss+k位随机数
491. *
492. * @param k
493. * 表示是取几位随机数,可以自己定
494. */
495. public static String getNo(int k) {
496. return getUserDate("yyyyMMddhhmmss") + getRandom(k);
497. }
498.
499. /**
500. * 返回一个随机数
501. *
502. * @param i
503. * @return
504. */
505. public static String getRandom(int i) {
506. new Random();
507. // int suiJiShu = jjj.nextInt(9);
508. if (i == 0)
509. return "";
510. "";
511. for (int k = 0; k < i; k++) {
512. 9);
513. }
514. return jj;
515. }
516.
517. /**
518. * @param args
519. */
520. public static boolean RightDate(String date) {
521. new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
522. ;
523. if (date == null)
524. return false;
525. if (date.length() > 10) {
526. new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
527. else {
528. new SimpleDateFormat("yyyy-MM-dd");
529. }
530. try {
531. sdf.parse(date);
532. catch (ParseException pe) {
533. return false;
534. }
535. return true;
536. }
537.
538. /***************************************************************************
539. * //nd=1表示返回的值中包含年度 //yf=1表示返回的值中包含月份 //rq=1表示返回的值中包含日期 //format表示返回的格式 1
540. * 以年月日中文返回 2 以横线-返回 // 3 以斜线/返回 4 以缩写不带其它符号形式返回 // 5 以点号.返回
541. **************************************************************************/
542. public static String getStringDateMonth(String sdate, String nd, String yf,
543. String rq, String format) {
544. new Date();
545. new SimpleDateFormat("yyyy-MM-dd");
546. String dateString = formatter.format(currentTime);
547. 0, 4); // 年份
548. 5, 7); // 月份
549. 8, 10); // 日期
550. "";
551. if (sdate == null || sdate.equals("") || !Isdate(sdate)) { // 处理空值情况
552. if (nd.equals("1")) {
553. sreturn = s_nd;
554. // 处理间隔符
555. if (format.equals("1"))
556. "年";
557. else if (format.equals("2"))
558. "-";
559. else if (format.equals("3"))
560. "/";
561. else if (format.equals("5"))
562. ".";
563. }
564. // 处理月份
565. if (yf.equals("1")) {
566. sreturn = sreturn + s_yf;
567. if (format.equals("1"))
568. "月";
569. else if (format.equals("2"))
570. "-";
571. else if (format.equals("3"))
572. "/";
573. else if (format.equals("5"))
574. ".";
575. }
576. // 处理日期
577. if (rq.equals("1")) {
578. sreturn = sreturn + s_rq;
579. if (format.equals("1"))
580. "日";
581. }
582. else {
583. // 不是空值,也是一个合法的日期值,则先将其转换为标准的时间格式
584. // sdate = roc.util.RocDate.getOKDate(sdate);
585. 0, 4); // 年份
586. 5, 7); // 月份
587. 8, 10); // 日期
588. if (nd.equals("1")) {
589. sreturn = s_nd;
590. // 处理间隔符
591. if (format.equals("1"))
592. "年";
593. else if (format.equals("2"))
594. "-";
595. else if (format.equals("3"))
596. "/";
597. else if (format.equals("5"))
598. ".";
599. }
600. // 处理月份
601. if (yf.equals("1")) {
602. sreturn = sreturn + s_yf;
603. if (format.equals("1"))
604. "月";
605. else if (format.equals("2"))
606. "-";
607. else if (format.equals("3"))
608. "/";
609. else if (format.equals("5"))
610. ".";
611. }
612. // 处理日期
613. if (rq.equals("1")) {
614. sreturn = sreturn + s_rq;
615. if (format.equals("1"))
616. "日";
617. }
618. }
619. return sreturn;
620. }
621.
622. public static String getNextMonthDay(String sdate, int m) {
623. sdate = getOKDate(sdate);
624. int year = Integer.parseInt(sdate.substring(0, 4));
625. int month = Integer.parseInt(sdate.substring(5, 7));
626. month = month + m;
627. if (month < 0) {
628. 12;
629. 1;
630. else if (month > 12) {
631. 12;
632. 1;
633. }
634. "";
635. if (month < 10)
636. "0" + month;
637. else
638. "" + month;
639. return year + "-" + smonth + "-10";
640. }
641.
642. public static String getOKDate(String sdate) {
643. if (sdate == null || sdate.equals(""))
644. return getStringDateShort();
645. if (!Isdate(sdate)) {
646. sdate = getStringDateShort();
647. }
648. // 将“/”转换为“-”
649. "/", "-");
650. // 如果只有8位长度,则要进行转换
651. if (sdate.length() == 8)
652. 0, 4) + "-" + sdate.substring(4, 6) + "-"
653. 6, 8);
654. new SimpleDateFormat("yyyy-MM-dd");
655. new ParsePosition(0);
656. Date strtodate = formatter.parse(sdate, pos);
657. String dateString = formatter.format(strtodate);
658. return dateString;
659. }
660.
661. public static void main(String[] args) throws Exception {
662. try {
663. // System.out.print(Integer.valueOf(getTwoDay("2006-11-03 12:22:10",
664. // "2006-11-02 11:22:09")));
665. catch (Exception e) {
666. throw new Exception();
667. }
668. // System.out.println("sss");
669. }
670. 查询时时间如果是这种类型的 "creditTime\": 1497591088000
671. 可以在pojo 实体类 get方法中 添加注释 @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
672. 返回结果 就是 creditTime": "2017-06-16 13:31:25
673.java 时间转年
转载本文章为转载内容,我们尊重原作者对文章享有的著作权。如有内容错误或侵权问题,欢迎原作者联系我们进行内容更正或删除文章。
上一篇:原神模型blender
下一篇:iOS开发中录音问题处理
提问和评论都可以,用心的回复会被更多人看到
评论
发布评论
相关文章
-
【C/C++ 学习】mutable关键字
mutable关键字学习记录
#c语言 #c++ #学习 成员变量 #include -
Ansible实战:VMware下K8s自动化部署指南
本文详细介绍了如何在VMware虚拟机环境中,使用Ansible工具自动化部署Kubernetes(K8s)集群的全过程。主要内容包括:1) VMware环境搭建及CentOS7虚拟机配置;2) 通过MobaXterm进行SSH连接管理;3) 在控制节点安装Ansible并配置免密登录;4) 编写Ansible Playbook实现K8s集群自动化部署,涵盖Docker安装、K8s组件配置、Master节点初始化及Worker节点加入等关键步骤;5) 验证集群部署结果。本指南提供了完整的操作流程和详细配置说
#ansible #kubernetes #自动化 Ansible CentOS
















