package com.exjor.webdemo; import java.text.SimpleDateFormat; import java.util.Date; public class FormatFun { /** * FORMAT_STRING = yyyy-MM-dd hh:mm:ss.SSS */ public static final String STRING_FORMAT_DATETIME = "yyyy-MM-dd hh:mm:ss.SSS"; /** * FORMAT_STRING = yyyy-MM-dd */ public static final String STRING_FORMAT_ONLYDATE = "yyyy-MM-dd"; /** * FORMAT_STRING = hh:mm:ss */ public static final String STRING_FORMAT_ONLYTIME = "hh:mm:ss"; /** * FORMAT_STRING = yyyy-MM-dd hh:mm:ss.SSS */ public static final SimpleDateFormat SDF_FORMAT_DATETIME = new SimpleDateFormat(FormatFun.STRING_FORMAT_DATETIME); /** * FORMAT_STRING = yyyy-MM-dd */ public static final SimpleDateFormat SDF_FORMAT_ONLYDATE = new SimpleDateFormat(FormatFun.STRING_FORMAT_ONLYDATE); /** * FORMAT_STRING = hh:mm:ss */ public static final SimpleDateFormat SDF_FORMAT_ONLYTIME = new SimpleDateFormat(FormatFun.STRING_FORMAT_ONLYTIME); /** * FORMAT_STRING = yyyy-MM-dd hh:mm:ss.SSS */ public static final int FORMAT_DATETIME = 0; /** * FORMAT_STRING = yyyy-MM-dd */ public static final int FORMAT_ONLYDATE = 1; /** * FORMAT_STRING = hh:mm:ss */ public static final int FORMAT_ONLYTIME = 2; /** * 格式化日期对象 * @param pDate java.util.Date类型的对象,为null则返回"" * @param pFormatType 默认0=FORMAT_DATETIME; 1=FORMAT_ONLYDATE; 2=FORMAT_ONLYTIME * @return */ public static String format(Date pDate, int pFormatType) { String vString = ""; if (pDate != null) { switch (pFormatType) { case FORMAT_DATETIME: vString = SDF_FORMAT_DATETIME.format(pDate); break; case FORMAT_ONLYDATE: vString = SDF_FORMAT_ONLYDATE.format(pDate); break; case FORMAT_ONLYTIME: vString = SDF_FORMAT_ONLYTIME.format(pDate); break; default: vString = SDF_FORMAT_DATETIME.format(pDate); break; } } return vString; } /** * 格式化日期对象为DateTime格式 * @param pDate java.util.Date类型的对象,为null则返回"" * @return */ public static String format(Date pDate) { return format(pDate, FORMAT_DATETIME); } }