欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

SimpleDateFormat格式化日期

程序员文章站 2022-07-02 16:36:19
//这里先获取时间 Date date=new Date(); //new一个日期格式,大写HH是24小时制,小写hh是12小时制 SimpleDateFormat temp=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); //格式化时间,得到的是Strin ......
date date=new date();
simpledateformat temp=new simpledateformat("yyyy-mm-dd hh:mm:ss");
string date2=temp.format(date);
date date3=temp.parse(date2);

===================================================================

//这里先获取时间

date date=new date();

 //new一个日期格式,大写hh是24小时制,小写hh是12小时制

simpledateformat temp=new simpledateformat("yyyy-mm-dd hh:mm:ss");

//格式化时间,得到的是string类型
string date2=temp.format(date);

//将string类型的日期date2转换成date型

date date3=temp.parse(date2);

 

 

ps:在向mysql数据库插入日期的时候。字段类型为date,显示出来格式是:yyyy-mm-dd ,字段类型为datetime,显示出来的格式是yyyy-mm-dd hh:mm:ss

值得注意的是:这里大写hh是24小时制,小写hh是12小时制