0


String转Date,Date转String

源码:

        Date currentTime = new Date();
        System.out.println("currentTime:"+currentTime);
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String dateString = formatter.format(currentTime);
        System.out.println("dateString:"+dateString);
        ParsePosition pos = new ParsePosition(0);// 从第一个字符开始解析
        Date currentTime_2 = formatter.parse(dateString, pos);
        System.out.println("currentTime_2:"+currentTime_2);
         Date time3 = formatter.parse(dateString);
        System.out.println("Time3:"+time3);

        Date date = new Date();//new日期对象
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//格式化当前时间
        String d = dateFormat.format(date);//获取当前时间
        System.out.println("date:"+d);

结果:

currentTime:Thu Nov 2313:08:38 CST 2023
dateString:2023-11-2313:08:38
currentTime_2:Thu Nov 2313:08:38 CST 2023
Time3:Thu Nov 2313:08:38 CST 2023
date:2023-11-2313:08:38
标签: java

本文转载自: https://blog.csdn.net/crystal4000/article/details/134574838
版权归原作者 live for myself 所有, 如有侵权,请联系我们删除。

“String转Date,Date转String”的评论:

还没有评论