Java时间和时间戳的相互转换

快来打我* 2021-12-21 14:13 452阅读 0赞
  1. /*
  2. * 将时间转换为时间戳
  3. */
  4. public static String dateToStamp(String s) throws ParseException{
  5. String res;
  6. SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  7. Date date = simpleDateFormat.parse(s);
  8. long ts = date.getTime();
  9. res = String.valueOf(ts);
  10. return res;
  11. }
  12. /*
  13. * 将时间戳转换为时间
  14. */
  15. public static String stampToDate(String s){
  16. String res;
  17. SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  18. long lt = new Long(s);
  19. Date date = new Date(lt);
  20. res = simpleDateFormat.format(date);
  21. return res;
  22. }

Java时间和时间戳的相互转换

时间转换为时间戳:

复制代码

  1. /*
  2. * 将时间转换为时间戳
  3. */
  4. public static String dateToStamp(String s) throws ParseException{
  5. String res;
  6. SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  7. Date date = simpleDateFormat.parse(s);
  8. long ts = date.getTime();
  9. res = String.valueOf(ts);
  10. return res;
  11. }

复制代码

时间戳转换为时间:

复制代码

  1. /*
  2. * 将时间戳转换为时间
  3. */
  4. public static String stampToDate(String s){
  5. String res;
  6. SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  7. long lt = new Long(s);
  8. Date date = new Date(lt);
  9. res = simpleDateFormat.format(date);
  10. return res;
  11. }

转载于:https://www.cnblogs.com/ylht/p/10197295.html

发表评论

表情:
评论列表 (有 0 条评论,452人围观)

还没有评论,来说两句吧...

相关阅读

    相关 Python时间时间相互转换

    在用Python处理数据时,可能有时候会需要将时间转换成时间戳,或者说将时间戳转换成时间,这里分享一下时间戳与时间的相互转换的方法。在Python里面处理时间相关问题,基本上用