JAVA的日期类DATE

好记性不如烂笔头。

1:常见场景  字符串转时间格式,日期转换字符串(在前后端交互 json)

   导入包(好像我的IDEA 不知道装了什么插件 会自动补齐提示)

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
        //y 代表年
        //M 代表月
        //d 代表日
        //H 代表24进制的小时
        //h 代表12进制的小时
        //m 代表分钟
        //s 代表秒
        //S 代表毫秒
        var timeFormat="yyyy-MM-dd hh:dd:ss";

        //字符串转换时间
        SimpleDateFormat sdf=new SimpleDateFormat(timeFormat);
        String str="2019-02-12 12:12:12";
        try {
            Date d=sdf.parse( str);
            System.out.printf("字符串转换时间: %s %n",d.toString());
        } catch (ParseException e) {
            e.printStackTrace();
        }

        //日期转字符串
        SimpleDateFormat sdf1 =new SimpleDateFormat(timeFormat );
        Date d= new Date();
        String str1 = sdf.format(d);
        System.out.println("当前时间: "+str1);

原文地址:https://www.cnblogs.com/y112102/p/11275016.html