转载请注明出处,谢谢!
日常代码中,经常会遇到很多对于时间格式的处理业务,最近项目中也设计到些许,记录一下,备用!
代码如下:
import java.text.SimpleDateFormat; import java.util.Date; /*** * @author sgl* DateFormatUtils 时间格式转换工具*/ public class DateFormatUtils {/*** 年月日 yyyy-MM-dd*/public static final String yyMMdd="yyyy-MM-dd";/*** 标准类型_汉字版 yyyy年MM月dd日 HH时mm分ss秒*/public static final String NORMAL_CN="yyyy年MM月dd日 HH时mm分ss秒";/*** 标准类型 yyyy-MM-dd HH:mm:ss*/public static final String NORMAL="yyyy-MM-dd HH:mm:ss";/*** 时分秒 HH:mm:ss*/public static final String Hms="HH:mm:ss";/*** * @param strtime 需要返回的时间格式的正则表达式* @return 匹配正则表达式的格式化时间串(yyyy-MM-dd:2015-08-06)返回当前时间*/public static String getCurrTimeStr(String strtime) {SimpleDateFormat sdf = new SimpleDateFormat(strtime);System.err.println(sdf.format(new Date(System.currentTimeMillis())));return sdf.format(new Date(System.currentTimeMillis()));}/*** * @param date 日期* @param timeFormat 时间格式* @return 返回指定格式的时间字符串*/public static String getTimeStrByDate(Date date,String timeFormat){SimpleDateFormat sdf = new SimpleDateFormat(timeFormat);return sdf.format(date);}/*** 将特定格式的时间串转换成Date类型(其中年份减去1900,另有意义,此处不做说明,网上相关资料很多)* @param times 格式如2016-3-22类型的时间字符串* @return*/public static Date getDates(String times){String[] temps=times.split("-");int[] dates=new int[temps.length];for (int i = 0; i < temps.length; i++) {dates[i]=Integer.parseInt(temps[i]);}return new Date(dates[0]-1900,dates[1],dates[2]);} }