日期格式化

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(); } } }

 

相关文章:

  • 2021-11-24
  • 2022-12-23
  • 2021-09-08
  • 2022-01-11
  • 2022-12-23
  • 2021-10-02
  • 2021-09-06
  • 2021-12-27
猜你喜欢
  • 2021-10-04
  • 2022-12-23
  • 2022-12-23
  • 2021-06-25
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案