package Demo1;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class DateUtils {
private DateUtils(){}
public static String dateToString(Date date, String format){
SimpleDateFormat sdf = new SimpleDateFormat(format);
String s = sdf.format(date);
return s;
}
public static Date stringTODate(String s,String format) throws ParseException {
SimpleDateFormat sdf = new SimpleDateFormat(format);
Date d = sdf.parse(s);
return d;
}
}
package Demo1;
import java.text.ParseException;
import java.util.Date;
public class DateDemo {
public static void main(String[] args) throws ParseException {
Date d = new Date();
String s1 = DateUtils.dateToString(d,"yyyy年MM月dd日 HH:mm:ss");
System.out.println(s1);
String s="2021-08-24 14:00:00";
Date dd = DateUtils.stringTODate(s,"yyyy-MM-dd HH:mm:ss");
System.out.println(dd);
}
}