Java tool class: Conversion between String and DateTime types

1. Convert String to DateTime

Hutool dependency needs to be introduced before conversion

String datestr = "2022-5-19"; 
DateTime datetime = DateUtil.parse(datestr);

2. Convert DateTime to String

public String DateTimeToString(Date date){
    //设置时间格式
    String dateformat = "yyyy-MM-dd HH:mm:ss";
    SimpleDateFormat f = new SimpleDateFormat(dateformat);
    //转换成对应的时间格式
    String dateStr = f.format(date);
    return dateStr;
}

Guess you like

Origin blog.csdn.net/challenglistic/article/details/126875199