java 时间戳转时间 指定时间转时间戳 时间戳转日期 指定日期转时间戳

先看效果:

 

源码:

 1 package com.lgq.common;
 2 
 3 import java.text.SimpleDateFormat;
 4 import java.util.Date;
 5 
 6 public class timeTest {
 7     public static void main(String[] args) {
 8 
 9         // 时间戳转时间:
10         long timel = 1520821882000L; // 时间戳
11         System.out.println("时间戳转时间:" + toTime(timel));
12 
13         //时间转时间戳:
14         String time = "2018-03-12 10:31:22";
15         System.out.println("时间转时间戳:" + toTimes(time));
16 
17     }
18 
19     /**
20      * 时间戳转时间:
21      *
22      * @param timestamp
23      * @return
24      */
25     public static String toTime(long timestamp) {
26         Date date = new Date(timestamp);
27         SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
28         return sdf.format(date);
29     }
30 
31     /**
32      * 时间转时间戳:
33      *
34      * @return
35      */
36     public static Long toTimes(String time) {
37         SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
38         Date date;
39         try {
40             date = sdf.parse(time);
41             return date.getTime();
42         } catch (Exception e) {
43             e.printStackTrace();
44         }
45         return null;
46     }
47 
48 }

 

posted @ 2023-09-15 23:14  刘贵庆  阅读(384)  评论(0编辑  收藏  举报