Java - 时间格式转换工具

/**
    * 转换String型日期格式
    * @param oldFormat 旧日期格式 "yyyy-MM-dd HH:mm"
    * @param newFormat 新日期格式 "yyyy-MM-dd HH:mm:ss"
    * @param changeTime 需要转换格式的时间
    * @return
    */
   public static String changeDateFormat(String oldFormat, String newFormat, String changeTime) throws ParseException {

       // string 转 calender
       SimpleDateFormat sdf = new SimpleDateFormat(oldFormat);
       Date changeDate = sdf.parse(changeTime);

       Calendar calendar = Calendar.getInstance();
       calendar.setTime(changeDate);	// 获得开始日期

       // calender转string
       SimpleDateFormat sdf2 = new SimpleDateFormat(newFormat);
       changeTime= sdf2.format(calendar.getTime());

       return changeTime;
   }

调用示例:

// 将 年月日 时分 -> 年月日 时分秒
changeTime= DateFormatUtils.changeDateFormat("yyyy-MM-dd HH:mm", "yyyy-MM-dd HH:mm:ss", changeTime);
posted @ 2022-05-19 16:07  御坂10027  阅读(1)  评论(0)    收藏  举报  来源