Java SimpleDateFormat change time String format

Description

Java SimpleDateFormat change time String format



import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class Main {
    public static void main(String[] argv) throws Exception {
        String timeStr = "2020-12-31 12:12:21";
        System.out.println(formatTimeStr(timeStr));
    }/*from  ww  w  .j  a va2  s.co  m*/

    public static String formatTimeStr(String timeStr) {
        String newTimeStr = "";
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            Date d = dateFormat.parse(timeStr);
            dateFormat.applyPattern("yyyyMMddHHmmss");
            newTimeStr = dateFormat.format(d);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return newTimeStr;
    }
}



PreviousNext

Related