首页 > 编程语言 >java 获取当前时间并转化为yyyy-MM-dd HH:mm:ss格式 时间戳和字符串之间转换

java 获取当前时间并转化为yyyy-MM-dd HH:mm:ss格式 时间戳和字符串之间转换

时间:2022-09-30 11:22:16浏览次数:78  
标签:java String ss dd SimpleDateFormat yyyy MM new

java 获取当前时间并转化为yyyy-MM-dd HH:mm:ss格式 时间戳和字符串之间转换

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

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

 

运行结果:

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

 

https://blog.csdn.net/QGhurt/article/details/116425296

标签:java,String,ss,dd,SimpleDateFormat,yyyy,MM,new
From: https://www.cnblogs.com/sunny3158/p/16744351.html

相关文章

  • Java获取当天或者明天等零点时间(00:00:00)0时0分0秒的方法 Calendar calendar = Calend
    Java获取当天或者明天等零点时间(00:00:00)0时0分0秒的方法Calendarcalendar=Calendar.getInstance();Java获取当天或者明天等零点时间(00:00:00)0时0分0秒的方法Sim......
  • java基础day-02
    第二章、面向对象一、什么是面向对象面向对象就是用人的世界观改变计算机世界观1.1对象万物皆可对象学习面向对象之前先了解什么是属性和行为?属性:表示的是事物的特征,是静态......
  • JAVA基础day05-实用类
    一、实用类介绍常用JavaAPI(封装在包里)Java.langEnum、包装类、Math、String、StringBuffer、System……java.util:工具类java.io:输入输出操作java.sql:数据库的操作……1.......
  • JavaWeb基础day01_XML
    一、XMLXML文件的默认打开方式是浏览器xml:是可扩展的标记语言ExtensibleMarkupLanguage。以一种标签语言与HTML类似1、xml的作用编写配置文件:C3P0编写XML配置文件做数据......
  • Day02-Java报表以及表格
    Java报表以及表格文章目录​​Java报表以及表格​​​​一、POI操作Excel​​​​1.1引入依赖​​​​1.2POI操作Excel高低版本区别​​​​1.3入门案例​​​​1.3.1低......
  • CSS:鼠标移动到图片上的动画
    CSS:鼠标移动到图片上的动画.picimg{ width:100%; left:0; top:0; right:0; bottom:0; margin:auto; transition:all.5s;}.pic:hoveri......
  • JavaScript箭头函数的使用以及和普通函数之间的使用
    箭头函数的使用以及和普通函数之间的转换// 第一种不带参数    // var one = function(){    //   return 'I am one'    // }    var......
  • 有人说,25岁再学习Java太晚了,我不这样认为!
    我个人认为任何时候,任何时期都不算晚!不过你要做好心理准备,毕竟自学java就业很难,你可以找一个行之有效的学习路线,跟着学习路线来学习,还是很有可能的,在学习的过程中,除了坚持......
  • 反射-理解Class类并获取Class实例
    反射-理解Class类并获取Class实例一、class类介绍在Object类中定义了以下的方法,此方法将被所有子类继承publicfinalnativeClass<?>getClass();getClass方法返......
  • 使用POI让java可以操作Excel(2)
    1.基本使用设置文本对齐方式mportjava.io.FileOutputStream;importorg.apache.poi.hssf.usermodel.HSSFRichTextString;importorg.apache.poi.hssf.usermodel.HSSFWorkb......