日期格式化工具DateFormatUtil
======================================================Date格式化工具类:
import java.text.SimpleDateFormat; import java.util.Date; public class DateFormatUtil { public static final String DATE_PATTERN_yyyyMMddHHmmssSSS = "yyyyMMddHHmmssSSS"; public static final String DATE_PATTERN_yyyyMMddHHmmss = "yyyyMMddHHmmss"; public static final String DATE_PATTERN_yyyyMMdd_HHmmss = "yyyy-MM-dd HH:mm:ss"; public static final String DATE_PATTERN_yyyyMMdd = "yyyyMMdd"; public static final String DATE_PATTERN_yyyy_MM_dd = "yyyy-MM-dd"; public static final String DATE_PATTERN_yyyy_MM_dd2 = "yyyy/MM/dd"; public static final String DATE_PATTERN_yyyy_MM_dd3 = "yyyy年MM月dd日"; public static final String TIME_PATTERN_hhmmss = "hhmmss"; public static String formatCurDate(String pattern){ return formatDate(new Date(), pattern); } public static String formatDate(Date inputDate, String pattern){ SimpleDateFormat formatUtil = new SimpleDateFormat(pattern); return formatUtil.format(inputDate); } }
======================================================Date格式化工具测试类:
/** * 格式化输入日期 */ @Test public void test_formatDate() { Date curDate = new Date(); String formatInputDate = DateFormatUtil.formatDate(curDate, DateFormatUtil.DATE_PATTERN_yyyyMMdd_HHmmss); System.out.println(formatInputDate); } /** * 格式化当前日期 */ @Test public void test_formatCurDate() { String formatCurDate = DateFormatUtil.formatCurDate(DateFormatUtil.DATE_PATTERN_yyyyMMdd_HHmmss); System.out.println(formatCurDate); }