时间戳转换器

java获取时间戳yyyymmddhhmmss

方法一(线程不安全, 不建议使用)


 
  1. private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
  2. Date now = new Date();
  3. String time = sdf.format(now);

方法二(线程安全,建议使用)


 
  1. import java.time.LocalDateTime;
  2. import java.time.format.DateTimeFormatter;
  3.  
  4. public class testMain {
  5.  
  6. public static void main(String[] args) {
  7. // yyyy-MM-dd HH:mm:ss.SSS ---> 年-月-日 时-分-秒-毫秒 (想删掉哪个小部分就直接删掉哪个小部分)
  8.  
  9. String timeStr1=LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
  10. String timeStr2=LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSS"));
  11. System.out.println("当前时间为:"+timeStr1);
  12. System.out.println("当前时间为:"+timeStr2);
  13. }
  14. }
  15.  

 

运行结果:

当前时间为:2018-11-27 10:41:47
当前时间为:2018-11-27 10:41:47.392


时间转时间戳:


 
  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. }

 
  1. import java.text.ParseException;
  2. import java.text.SimpleDateFormat;
  3. import java.util.Date;
  4.  
  5. public class Test2 {
  6. public static void main(String[] args) {
  7. SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
  8. System.out.println(sdf.format(new Date()));
  9. //获取当前时间戳,也可以是你自已给的一个随机的或是别人给你的时间戳(一定是long型的数据)
  10. long timeStamp = System.currentTimeMillis();
  11. //这个是你要转成后的时间的格式
  12. SimpleDateFormat sdff=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  13. // 时间戳转换成时间
  14. String sd = sdff.format(new Date(timeStamp));
  15. System.out.println(sd);//打印出你要的时间
  16. }
  17. /*
  18. * 将时间转换为时间戳
  19. */
  20. public static String dateToStamp(String s) throws ParseException {
  21. String res;
  22. SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  23. Date date = simpleDateFormat.parse(s);
  24. long ts = date.getTime();
  25. res = String.valueOf(ts);
  26. return res;
  27. }
  28.  
  29. /*
  30. * 将时间戳转换为时间
  31. */
  32. public static String stampToDate(String s){
  33. String res;
  34. SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  35. long lt = new Long(s);
  36. Date date = new Date(lt);
  37. res = simpleDateFormat.format(date);
  38. return res;
  39. }
  40. }

 
  1. @Test
  2. public void test1(){
  3. /*SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
  4. System.out.println(sdf.format(new Date()));*/
  5.  
  6. SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmm");
  7. System.out.println(sdf.format(new Date()));
  8.  
  9. long currentTimeMillis = System.currentTimeMillis();
  10. System.out.println(currentTimeMillis);
  11. SimpleDateFormat sdf2=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  12. String format = sdf2.format(new Date(currentTimeMillis));
  13. System.out.println(format);
  14.  
  15. }

 


相关文章
苏ICP备2022026517号-1  |   苏公网安备 32081202000315号
淮安先皓网络科技有限公司 © 版权所有  联系我们