导航

String类型时间与Date时间转换

Posted on 2022-10-29 14:39  疯狂搬砖人  阅读(1396)  评论(0)    收藏  举报

1. String类型的时间转为DateTime

public static Date transferString2Date(String s) {
    Date date = new Date();
    try {
        date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(s);
    } catch (ParseException e) {
        //LOGGER.error("时间转换错误, string = {}", s, e);
    }
    return date;
}

2. String类型的时间转为Date

public static Date transferString2Date(String s) {
    Date date = new Date();
    try {
        date = new SimpleDateFormat("yyyy-MM-dd").parse(s);
    } catch (ParseException e) {
        //LOGGER.error("时间转换错误, string = {}", s, e);
    }
    return date;
}

3.Date类型的时间转为String

  Date date = new Date();
  SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss SSS");
  String dateStr = fmt.format(date);