指定java中Date日期格式化,实现字符串转Date


 

 日期格式化

y 代表年  M 代表月   d 代表日

E 代表星期

H 代表24进制的小时   m 代表分钟   s 代表秒     S 代表毫秒

h 代表12进制的小时

public class DateTest {

    public static void main(String[] args) {

       SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

       Date date=new Date();

       String dateStr=sdf.format(date);

       System.out.println(dateStr);

    }

字符串转日期

public class DateTest {

    public static void main(String[] args) {

       SimpleDateFormat sdf=new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");

       String str="2020/10/13 10:11:12";

       try {

           Date date=sdf.parse(str);

           System.out.println("字符串:"+str+"通过yyyy/MM/dd HH:mm:ss格式转换得到"+date.toString());  }
    
catch (ParseException e)
          {
e.printStackTrace(); } } }

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM