SimpleDateFormat(\"yyyy-MM-dd hh:mm:ss.SSS\")时间转换问题

蹲街弑〆低调 提交于 2020-02-22 02:26:09

SimpleDateFormat("yyyy-MM-dd hh:mm:ss.SSS")时间转换问题

  • 程序代码:

      import java.text.ParseException;
      import java.text.SimpleDateFormat;
      import java.util.Date;
    
      public class Time {
      public static void main(String[] args) throws ParseException {
      //定义一个时间字符串
      String date_str = "2016-06-23 09:46:27.000";
      //将时间字符串转换成Date形式,不能够强制转换
      Date date = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss.SSS").parse(date_str);
      //定义新的字符串str1和str2,将Date型转为String型
      String str1 = (new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")).format(date);
      String str2 = (new SimpleDateFormat("yyyy年MM月dd日 ")).format(date);
    
      //取当前时间进行修改,方法1
      String str3 = (new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")).format(new Date());
      //取当前时间进行修改,方法2,其实和方法1类似
      SimpleDateFormat format=new SimpleDateFormat("yyyy年MM月dd日 HH时mm分ss秒");
      String str4 = format.format(new Date());
              System.out.println("str1:   "+str1);
      System.out.println("str2:   "+str2);
      System.out.println("str3:   "+str3);
      System.out.println("str4:   "+str4);
          }   
      }
  • 输出:

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!