13位时间戳与格式化日期之间的转换实现

1 import java.text.ParseException;
 2 import java.text.SimpleDateFormat;
 3 import java.util.Date;
 4 
 5 public class DateUtil {
 6     
 7     private static SimpleDateFormat sf = null;
 8     
 9     /**
10      * 获取系统时间 
11      */
12     public static String getCurrentDate() {
13         Date d = new Date();
14          sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
15         return sf.format(d);
16     }
17                                       
18     /**
19      * 时间戳转换成字符串
20      */
21     public static String getDateToString(long time) {
22         Date d = new Date(time);
23         sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
24         return sf.format(d);
25     }
26 
27     /**
28      * 将字符串转为时间戳
29      */
30     public static long getStringToDate(String time) {
31         sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
32         Date date = new Date();
33         try{
34             date = sf.parse(time);
35         } catch(ParseException e) {
36             e.printStackTrace();
37         }
38         return date.getTime();
39     }
40     
41     /**
42      * 直接获取时间戳
43      */
44     public static String getTimeStamp() {
45         String currentDate = getCurrentDate();
46         sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
47         Date date = new Date();
48         try{
49             date = sf.parse(currentDate);
50         } catch(ParseException e) {
51             e.printStackTrace();
52         }
53         return String.valueOf(date.getTime());
54     }
55     
56 }