JAVA时间转换总结

minautou / 2024-09-14 / 原文

JAVA时间转换总结

 

  1.格式化时间  Date ~ 2022-03-24 03:30:13

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


  2.格式化时间 2022-03-24T03:30:13.000000 ~ 2022-03-24 03:30:13
    SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
   String createAt = "2022-03-24T03:30:13.000000".replace("T", " ");
   Date date;
   Calendar cal = Calendar.getInstance();
   try {
   date = format.parse(createAt);
   cal.setTime(date);
   } catch (ParseException e) {
     System.out.println("portalDateToStr error:" + e);
   }
   Date calTime = cal.getTime();
   String formatRs = format.format(date);