DateUtil(SimpleDateFormat)

时间:2023-04-08 16:27:13
import java.util.Calendar;
import java.util.Date;
import java.text.SimpleDateFormat; public class DateUtil
{
private static SimpleDateFormat format1;
private static SimpleDateFormat format2;
private static SimpleDateFormat format3;
private static SimpleDateFormat format4; static {
DateUtil.format1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
DateUtil.format2 = new SimpleDateFormat("yyyy-MM-dd");
DateUtil.format3 = new SimpleDateFormat("yyyyMMddHHmmss");
DateUtil.format4 = new SimpleDateFormat("HH:mm:ss");
} public static Date getNow() {
return Calendar.getInstance().getTime();
} public static String format_yyyy_MM_dd_HH_mm_ss(final Date date) {
return DateUtil.format1.format(date);
} public static String format_yyyy_MM_dd(final Date date) {
return DateUtil.format2.format(date);
} public static String format_yyyyMMddHHmmss(final Date date) {
return DateUtil.format3.format(date);
} public static String format_HH_mm_ss(final Date date) {
return DateUtil.format4.format(date);
} public static String format(final String pattern, final Date date) {
return new SimpleDateFormat(pattern).format(date);
} public static Date getTimeForAnyHourInToday(final int hour) {
final Calendar c = Calendar.getInstance();
c.set(10, hour);
c.set(12, 0);
c.set(13, 0);
c.set(14, 0);
return c.getTime();
}
}