1
/* Date d = new Date(); String s = DateUtils.DateToString(d, "yyyy-MM-dd HH:mm:ss"); System.out.println(s); System.out.println("============================"); String s2 = "2048-01-01 12:00:00"; Date d2 = DateUtils.StrHHToDate(s2); System.out.println(DateUtils.DateToStrHH(d2)); System.out.println(d2.toString()); */ public class DateUtils { private DateUtils() { } public static String DateToString(Date date, String format) { return new SimpleDateFormat(format).format(date); } public static String DateToStrHH(Date date) { return DateToString(date, "yyyy-MM-dd HH:mm:ss"); } public static String DateToStr(Date date) { return DateToString(date, "yyyy-MM-dd"); } public static Date StringToDate(String dateStr, String format) throws ParseException { return new SimpleDateFormat(format).parse(dateStr); } public static Date StrToDate(String dateStr) throws ParseException { return new SimpleDateFormat("yyyy-MM-dd").parse(dateStr); } public static Date StrHHToDate(String dateStr) throws ParseException { return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(dateStr); } }