Java|java日期与时间戳相互转换大全

?? 各种时间戳与日期之间相互转换的工具,,,这么多方法肯定有你想要的功能233333333
【Java|java日期与时间戳相互转换大全】
[java] view plain copy print ?

  1. package com.crm.util;
  2. import java.math.BigDecimal;
  3. import java.text.DecimalFormat;
  4. import java.text.ParseException;
  5. import java.text.SimpleDateFormat;
  6. import java.util.Calendar;
  7. import java.util.Date;
  8. /**
  9. * @author DingJiaCheng
  10. * */
  11. public class DateFormatUtil {
  12. /**
  13. * 时间戳转日期
  14. * @param ms
  15. * @return
  16. */
  17. public static Date transForDate(Integer ms){
  18. if(ms==null){
  19. ms=0;
  20. }
  21. long msl=(long)ms*1000;
  22. SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  23. Date temp=null;
  24. if(ms!=null){
  25. try {
  26. String str=sdf.format(msl);
  27. temp=sdf.parse(str);
  28. } catch (ParseException e) {
  29. e.printStackTrace();
  30. }
  31. }
  32. return temp;
  33. }
  34. /**
  35. * 获取晚上9点半的时间戳
  36. *
  37. * @return
  38. */
  39. public static int getTimes(int day, int hour, int minute) {
  40. Calendar cal = Calendar.getInstance();
  41. cal.add(Calendar.DATE, day);
  42. cal.set(Calendar.HOUR_OF_DAY, hour);
  43. cal.set(Calendar.SECOND, 0);
  44. cal.set(Calendar.MINUTE, minute);
  45. cal.set(Calendar.MILLISECOND, 0);
  46. return (int) (cal.getTimeInMillis() / 1000);
  47. }
  48. /**
  49. * 获取当前时间往上的整点时间
  50. *
  51. * @return
  52. */
  53. public static int getIntegralTime() {
  54. Calendar cal = Calendar.getInstance();
  55. cal.add(Calendar.HOUR_OF_DAY, 1);
  56. cal.set(Calendar.SECOND, 0);
  57. cal.set(Calendar.MINUTE, 0);
  58. cal.set(Calendar.MILLISECOND, 0);
  59. return (int) (cal.getTimeInMillis() / 1000);
  60. }
  61. public static int getIntegralTimeEnd() {
  62. Calendar cal = Calendar.getInstance();
  63. cal.set(Calendar.HOUR_OF_DAY, 24);
  64. cal.set(Calendar.SECOND, 0);
  65. cal.set(Calendar.MINUTE, 0);
  66. cal.set(Calendar.MILLISECOND, 0);
  67. return (int) (cal.getTimeInMillis() / 1000);
  68. }
  69. /**
  70. * 时间戳转日期
  71. * @param ms
  72. * @return
  73. */
  74. public static Date transForDate3(Integer ms){
  75. if(ms==null){
  76. ms=0;
  77. }
  78. long msl=(long)ms*1000;
  79. SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm");
  80. Date temp=null;
  81. if(ms!=null){
  82. try {
  83. String str=sdf.format(msl);
  84. temp=sdf.parse(str);
  85. } catch (ParseException e) {
  86. e.printStackTrace();
  87. }
  88. }
  89. return temp;
  90. }
  91. /**
  92. * 时间戳转日期
  93. * @param ms
  94. * @return
  95. */
  96. public static Date transForDate(Long ms){
  97. if(ms==null){
  98. ms=(long)0;
  99. }
  100. long msl=(long)ms*1000;
  101. SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  102. Date temp=null;
  103. if(ms!=null){
  104. try {
  105. String str=sdf.format(msl);
  106. temp=sdf.parse(str);
  107. } catch (ParseException e) {
  108. e.printStackTrace();
  109. }
  110. }
  111. return temp;
  112. }
  113. public static String transForDate1(Integer ms){
  114. String str = "";
  115. if(ms!=null){
  116. long msl=(long)ms*1000;
  117. SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  118. if(ms!=null){
  119. try {
  120. str=sdf.format(msl);
  121. } catch (Exception e) {
  122. e.printStackTrace();
  123. }
  124. }
  125. }
  126. return str;
  127. }
  128. public static String transForDate2(Integer ms){
  129. String str = "";
  130. if(ms!=null){
  131. long msl=(long)ms*1000;
  132. SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
  133. if(ms!=null){
  134. try {
  135. str=sdf.format(msl);
  136. } catch (Exception e) {
  137. e.printStackTrace();
  138. }
  139. }
  140. }
  141. return str;
  142. }
  143. public static String transForDate4(Integer ms){
  144. String str = "";
  145. if(ms!=null){
  146. long msl=(long)ms*1000;
  147. SimpleDateFormat sdf=new SimpleDateFormat("yyyy.MM.dd");
  148. if(ms!=null){
  149. try {
  150. str=sdf.format(msl);
  151. } catch (Exception e) {
  152. e.printStackTrace();
  153. }
  154. }
  155. }
  156. return str;
  157. }
  158. public static String transForDate5(Integer ms){
  159. String str = "";
  160. if(ms!=null){
  161. long msl=(long)ms*1000;
  162. SimpleDateFormat sdf=new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
  163. if(ms!=null){
  164. try {
  165. str=sdf.format(msl);
  166. } catch (Exception e) {
  167. e.printStackTrace();
  168. }
  169. }
  170. }
  171. return str;
  172. }
  173. public static String transForDateInChinese(Integer ms){
  174. String str = "";
  175. if(ms!=null){
  176. long msl=(long)ms*1000;
  177. SimpleDateFormat sdf=new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
  178. if(ms!=null){
  179. try {
  180. str=sdf.format(msl);
  181. } catch (Exception e) {
  182. e.printStackTrace();
  183. }
  184. }
  185. }
  186. return str;
  187. }
  188. /**
  189. * 日期转时间戳
  190. * @param date
  191. * @return
  192. */
  193. public static Integer transForMilliSecond(Date date){
  194. if(date==null) return null;
  195. return (int)(date.getTime()/1000);
  196. }
  197. /**
  198. * 获取当前时间戳
  199. * @return
  200. */
  201. public static Integer currentTimeStamp(){
  202. return (int)(System.currentTimeMillis()/1000);
  203. }
  204. /**
  205. * 日期字符串转时间戳
  206. * @param dateStr
  207. * @return
  208. */
  209. public static Integer transForMilliSecond(String dateStr){
  210. Date date = DateFormatUtil.formatDate(dateStr);
  211. return date == null ? null : DateFormatUtil.transForMilliSecond(date);
  212. }
  213. /**
  214. * 日期字符串转时间戳
  215. * @param dateStr
  216. * @return
  217. */
  218. public static Integer transForMilliSecond(String dateStr,String format){
  219. Date date = DateFormatUtil.formatDate(dateStr,format);
  220. return date == null ? null : DateFormatUtil.transForMilliSecond(date);
  221. }
  222. /**
  223. * 日期字符串转时间戳
  224. * @param dateStr
  225. * @param 格式 如"yyyy-mm-dd"
  226. * @return
  227. */
  228. public static Integer transForMilliSecondByTim(String dateStr,String tim){
  229. SimpleDateFormat sdf=new SimpleDateFormat(tim);
  230. Date date =null;
  231. try {
  232. date = sdf.parse(dateStr);
  233. } catch (ParseException e) {
  234. e.printStackTrace();
  235. }
  236. return date == null ? null : DateFormatUtil.transForMilliSecond(date);
  237. }
  238. /**
  239. * 字符串转日期,格式为:"yyyy-MM-dd HH:mm:ss"
  240. * @param dateStr
  241. * @return
  242. */
  243. public static Date formatDate(String dateStr){
  244. SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  245. Date result=null;
  246. try {
  247. result = sdf.parse(dateStr);
  248. } catch (ParseException e) {
  249. e.printStackTrace();
  250. }
  251. return result;
  252. }
  253. /**
  254. * 字符串转日期,格式为:"yyyy-MM-dd HH:mm:ss"
  255. * @param dateStr
  256. * @return
  257. */
  258. public static Date formatDate(String dateStr,String format){
  259. SimpleDateFormat sdf=new SimpleDateFormat(format);
  260. Date result=null;
  261. try {
  262. result = sdf.parse(dateStr);
  263. } catch (ParseException e) {
  264. e.printStackTrace();
  265. }
  266. return result;
  267. }
  268. /**
  269. * 日期转字符串
  270. * @param date
  271. * @return
  272. */
  273. public static String formatDate(Date date){
  274. SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  275. String result=null;
  276. result = sdf.format(date);
  277. return result;
  278. }
  279. /**
  280. * 日期转字符串
  281. * @param date
  282. * @return
  283. */
  284. public static String formatDate(Date date,String format){
  285. SimpleDateFormat sdf=new SimpleDateFormat(format);
  286. String result=null;
  287. result = sdf.format(date);
  288. return result;
  289. }
  290. /**
  291. * 时间戳格式化输出(httl模版用)
  292. *
  293. * @param ms时间戳
  294. * @param format格式化
  295. * @return
  296. */
  297. public static String transForDate(Integer ms, String format){
  298. String str = "";
  299. if(ms!=null){
  300. long msl=(long)ms*1000;
  301. SimpleDateFormat sdf=new SimpleDateFormat(format);
  302. if(!ms.equals(0)){
  303. try {
  304. str=sdf.format(msl);
  305. } catch (Exception e) {
  306. e.printStackTrace();
  307. }
  308. }
  309. }
  310. return str;
  311. }
  312. /**
  313. * 取BigDecimal类型数的整数或小数部分(httl模版用)
  314. *
  315. * @param b 值
  316. * @param mode模式 0取整 1去小数部分
  317. * @return
  318. */
  319. public static String splitBigDecimal(BigDecimal b, int mode) {
  320. DecimalFormat df = new DecimalFormat("0.00");
  321. String s = df.format(b);
  322. if(mode==0){
  323. return s.split("\\.")[0];
  324. }else {
  325. return "."+s.split("\\.")[1];
  326. }
  327. }
  328. /**
  329. * 计算两个日期之间差的天数(httl模版用)
  330. *
  331. * @param ts1时间戳1
  332. * @param ts2时间戳2
  333. * @return
  334. */
  335. public static int caculate2Days(Integer ts1, Integer ts2) {
  336. Date firstDate = DateFormatUtil.transForDate(ts1);
  337. Date secondDate = DateFormatUtil.transForDate(ts2);
  338. Calendar calendar = Calendar.getInstance();
  339. calendar.setTime(firstDate);
  340. int dayNum1 = calendar.get(Calendar.DAY_OF_YEAR);
  341. calendar.setTime(secondDate);
  342. int dayNum2 = calendar.get(Calendar.DAY_OF_YEAR);
  343. return Math.abs(dayNum1 - dayNum2);
  344. }
  345. /**
  346. * 给手机加密中间四位加星号
  347. *
  348. * @param mobile
  349. * @return
  350. */
  351. public String mobileSerect(String mobile){
  352. if(!StringUtils.isBlank(mobile)){
  353. int between = mobile.length()/2;
  354. mobile = mobile.substring(0, between-2)+"****"+mobile.substring(between+2, mobile.length());
  355. }
  356. return mobile;
  357. }
  358. /**
  359. * 给邮箱加密加星号
  360. *
  361. * @param email
  362. * @return
  363. */
  364. public String emailSerect(String email) {
  365. if(!StringUtils.isBlank(email)){
  366. int length = email.lastIndexOf("@");
  367. email = email.substring(0, 2)+"****"+email.substring(length-2, email.length());
  368. }
  369. return email;
  370. }
  371. /**
  372. * BigDecimal类型数据相加
  373. *
  374. * @param BigDecimal source
  375. * @param BigDecimal target
  376. * @return
  377. */
  378. public BigDecimal sumBigDicimal(BigDecimal source, BigDecimal target) {
  379. source = source.add(target);
  380. return source;
  381. }
  382. /**
  383. * BigDecimal类型数据相加
  384. *
  385. * @param BigDecimal source
  386. * @param BigDecimal target
  387. * @return
  388. */
  389. public BigDecimal sumBigDicimalAndDouble(BigDecimal source, Double target) {
  390. BigDecimal new_target = new BigDecimal(target);
  391. source = source.add(new_target);
  392. return source;
  393. }
  394. /**
  395. * BigDecimal类型数据相减
  396. *
  397. * @param BigDecimal source
  398. * @param BigDecimal target
  399. * @return
  400. */
  401. public BigDecimal subBigDicimal(BigDecimal source, BigDecimal target) {
  402. source = source.subtract(target);
  403. return source;
  404. }
  405. /**
  406. * 获取传入时间和当前时间的时间差
  407. * @return
  408. */
  409. public static Long getTimediff(int timeStamp){
  410. Date d1 = DateFormatUtil.transForDate(timeStamp);
  411. Date today = new Date();
  412. if(d1.getTime()
  413. return null;
  414. }
  415. return (d1.getTime()-today.getTime())/1000;
  416. }
  417. /**
  418. * 获取某周的第一天日期
  419. * @param week 0 当周 1 上一周 -1 下一周
  420. * @return
  421. */
  422. public static String weekFirstDay(int week){
  423. Calendar c1 = Calendar.getInstance();
  424. int dow = c1.get(Calendar.DAY_OF_WEEK);
  425. c1.add(Calendar.DATE, -dow-7*(week-1)-5 );
  426. String d1 = new SimpleDateFormat("yyyy-MM-dd").format(c1.getTime());
  427. return d1+" 00:00:00";
  428. }
  429. /**
  430. * 当前时间加一年
  431. */
  432. public static String addYear(int startTime){
  433. Date firstDate = DateFormatUtil.transForDate(startTime);
  434. Calendar calendar = Calendar.getInstance();
  435. calendar.setTime(firstDate);
  436. calendar.add(Calendar.YEAR,1);
  437. String d1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(calendar.getTime());
  438. return d1;
  439. }
  440. /**
  441. * 获取某周的最后一天日期
  442. * @param week
  443. * @return
  444. */
  445. public static String weekLastDay(int week){
  446. Calendar c1 = Calendar.getInstance();
  447. int dow = c1.get(Calendar.DAY_OF_WEEK);
  448. c1.add(Calendar.DATE, -dow-7*(week-1)+1);
  449. String d1 = new SimpleDateFormat("yyyy-MM-dd").format(c1.getTime());
  450. return d1+" 23:59:59";
  451. }
  452. /**
  453. * 和当前时间比对
  454. * @return
  455. */
  456. public static boolean greaterThanNow(int timeStamp){
  457. Date d1 = DateFormatUtil.transForDate(timeStamp);
  458. Date today = new Date();
  459. if(d1.getTime()>=today.getTime()){
  460. return true;
  461. }
  462. return false;
  463. }
  464. /**
  465. * HH:mm:ss格式时间转换为1970-01-01日的时间戳(也就是只有时间没有日期的情况要求使用时间戳表示时间)
  466. * @author DingJiaCheng
  467. * */
  468. public static int transFromTime(String time){
  469. return transForMilliSecond("1970-01-01 "+time,"yyyy-mm-dd HH:mm:ss");
  470. }
  471. /**
  472. * 时间戳转换为HH:mm:ss格式时间(日期去除)
  473. * @author DingJiaCheng
  474. * */
  475. public static String transToTime(int time){
  476. String s = new String(transForDate1(time));
  477. String ss[] = s.split(" ");
  478. return ss[1];
  479. }
  480. public static int transToChuo(String dateString){
  481. SimpleDateFormat simpleDateFormat =new SimpleDateFormat("yyyy-MM-dd");
  482. int res = 0;
  483. try {
  484. Date date=simpleDateFormat .parse(dateString);
  485. res = (int) date.getTime();
  486. } catch (ParseException e) {
  487. e.printStackTrace();
  488. }
  489. return res;
  490. }
  491. public static void main(String[] args) {
  492. //System.out.println(getIntegralTimeEnd());
  493. System.out.println(transForDate2(transForMilliSecond("2015-02-25 00:00:00")));
  494. //System.out.println(transForMilliSecond("2016-01-25","yyyy-mm-dd"));
  495. //System.out.println(transForDate1(transForMilliSecond("1970-01-01 00:00:00","yyyy-mm-dd HH:mm:ss")));
  496. //System.out.println(currentTimeStamp());
  497. //System.out.println(transForDate(currentTimeStamp()));
  498. //System.out.println(new Date());
  499. //System.out.println(DateUtils.getDate());
  500. System.out.println(transFromTime("00:00:01"));
  501. System.out.println(transToTime(transFromTime("15:01:13")));
  502. }
  503. }

    推荐阅读