指点成金-最美分享吧

登录

Java 13位时间戳转换日期格式

佚名 举报

篇首语:本文由小编为大家整理,主要介绍了Java 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     2627     /**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         try34             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         try49             date = sf.parse(currentDate);50          catch(ParseException e) 51             e.printStackTrace();52         53         return String.valueOf(date.getTime());54     55    56 

  

以上是关于Java 13位时间戳转换日期格式的主要内容,如果未能解决你的问题,请参考以下文章