在Java中日期的转换是一个非常常见的问题,下面给出几种常见的日期转换:

1.将日期转换为字符串格式:yyyy-MM-dd

Date now=new Date();
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
        String string=sdf.format(now);

2.将日期转换为字符串格式:yyyy-MM-dd HH:mm:ss

Date now=new Date();
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String string=sdf.format(now);

3.将日期格式转换为:yyyy-MM-dd

Date now=new Date();
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
        String string=sdf.format(now);
        Date d=java.sql.Date.valueOf(string);
        System.out.println(d);

4.将日期格式转换为字符串:hh-mm-ss

        Date now=new Date();
        SimpleDateFormat sdf=new SimpleDateFormat("hh:mm:ss");
        String string=sdf.format(now);
        System.out.println(string);

 

相关文章:

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