Java中util包下有很多的工具类及方法,但往往有很多并能直接用到业务中,我们需要在这些工具上进行一些加工,比如日期格式,以下带有Main方法,可以自行调试,然后加上注释,以便以后使用。
import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class DateUtils {
private static ThreadLocal<SimpleDateFormat> dates = new ThreadLocal<SimpleDateFormat>(){
@Override
protected SimpleDateFormat initialValue() {
return new SimpleDateFormat("MM,dd,HH,mm");
}
};
public static String [] getMonthDateHourMinute(Date date){
String [] tmp = dates.get().format(date).split(",");
return tmp;
}
private static ThreadLocal<SimpleDateFormat> dateAlls = new ThreadLocal<SimpleDateFormat>(){
@Override
protected SimpleDateFormat initialValue() {
return new SimpleDateFormat("MM,dd,HH,mm,ss");
}
};
public static synchronized int days(int month){
Calendar cal = Calendar.getInstance();
cal.set(Calendar.MONTH, month - 1);
return cal.getActualMaximum(Calendar.DATE);
}
public static int [] getMonthDateHourMinuteSecond(Date date){
String [] tmp = dateAlls.get().format(date).split(",");
int [] result = new int[tmp.length];
for(int i = 0; i < tmp.length; i++){
result[i] = Integer.parseInt(tmp[i]);
}
return result;
}
private static ThreadLocal<SimpleDateFormat> simpleDateFormats = new ThreadLocal<SimpleDateFormat>(){
@Override
protected SimpleDateFormat initialValue() {
return new SimpleDateFormat(DateFormat.YYYYMMDD.getFormat());
}
};
private static ThreadLocal<SimpleDateFormat> dateFormats = new ThreadLocal<SimpleDateFormat>(){
@Override
protected SimpleDateFormat initialValue() {
return new SimpleDateFormat(DateFormat.YYYY_MM_DD.getFormat());
}
};
private static ThreadLocal<SimpleDateFormat> dateTimeHourFormats = new ThreadLocal<SimpleDateFormat>(){
@Override
protected SimpleDateFormat initialValue() {
return new SimpleDateFormat(DateFormat.YYYY_MM_DD_HH.getFormat());
}
};
private static ThreadLocal<SimpleDateFormat> dateTimeWithoutSecondFormats = new ThreadLocal<SimpleDateFormat>(){
@Override
protected SimpleDateFormat initialValue() {
return new SimpleDateFormat(DateFormat.YYYY_MM_DD_HH_MM.getFormat());
}
};
private static ThreadLocal<SimpleDateFormat> simpleDateTimeFormats = new ThreadLocal<SimpleDateFormat>(){
@Override
protected SimpleDateFormat initialValue() {
return new SimpleDateFormat(DateFormat.YYYYMMDDHHMMSS.getFormat());
}
};
private static ThreadLocal<SimpleDateFormat> dateTimeFormats = new ThreadLocal<SimpleDateFormat>(){
@Override
protected SimpleDateFormat initialValue() {
return new SimpleDateFormat(DateFormat.YYYY_MM_DD_HH_MM_SS.getFormat());
}
};
private static Map<Pattern, ThreadLocal<SimpleDateFormat>> threadLocalMap = new HashMap<Pattern, ThreadLocal<SimpleDateFormat>>();
static {
threadLocalMap.put(Pattern.compile("^[\\d]{8}$"), simpleDateFormats);//DateFormat.YYYYMMDD
threadLocalMap.put(Pattern.compile("^[\\d]{14}$"), simpleDateTimeFormats);//DateFormat.YYYYMMDDMMHHSS
threadLocalMap.put(Pattern.compile("^[\\d]{4}-[\\d]{2}-[\\d]{2}$"), dateFormats);
threadLocalMap.put(Pattern.compile("^[\\d]{4}-[\\d]{2}-[\\d]{2}[\\s]{1}[\\d]{2}$"), dateTimeHourFormats);
threadLocalMap.put(Pattern.compile("^[\\d]{4}-[\\d]{2}-[\\d]{2}[\\s]{1}[\\d]{2}[:][\\d]{2}$"), dateTimeWithoutSecondFormats);
threadLocalMap.put(Pattern.compile("^[\\d]{4}-[\\d]{2}-[\\d]{2}[\\s]{1}[\\d]{2}[:][\\d]{2}[:][\\d]{2}$"), dateTimeFormats);
}
public static void main(String[] args) {
System.out.println(getDate("2013-03-01 22:01:03"));
Date date = new Date();
System.out.println(DateUtils.formatYYYY_MM_DD(date));
System.out.println(DateUtils.formatYYYY_MM_DD_HH(date));
System.out.println(DateUtils.formatYYYY_MM_DD_HH_MM(date));
System.out.println(DateUtils.formatYYYY_MM_DD_HH_MM_SS(date));
System.out.println(DateUtils.formatYYYYMMDD(date));
System.out.println(DateUtils.formatYYYYMMDDHHMMSS(date));
System.out.println(parseYYYY_MM_DD(DateUtils.formatYYYY_MM_DD(date)));
System.out.println(parseYYYY_MM_DD_HH(DateUtils.formatYYYY_MM_DD_HH(date)));
System.out.println(parseYYYY_MM_DD_HH_MM(DateUtils.formatYYYY_MM_DD_HH_MM(date)));
System.out.println(parseYYYY_MM_DD_HH_MM_SS(DateUtils.formatYYYY_MM_DD_HH_MM_SS(date)));
System.out.println(parseYYYYMMDD(DateUtils.formatYYYYMMDD(date)));
System.out.println(parseYYYYMMDDHHMMSS(DateUtils.formatYYYYMMDDHHMMSS(date)));
System.out.println(DateUtils.getSimpleDateTime());
}
public static Date getDate(String date) {
try {
for (Pattern pattern : threadLocalMap.keySet()) {
Matcher matcher = pattern.matcher(date);
if (matcher != null && matcher.find()) {
ThreadLocal<SimpleDateFormat> threadLocal = threadLocalMap.get(pattern);
return threadLocal.get().parse(date);
}
}
} catch (ParseException e){
throw new RuntimeException(e);
}
if(date != null && !"".equals(date)){
throw new RuntimeException("date.format.error");
}
return null;
}
private static Pattern normalPattern = Pattern.compile("^[\\d]{4}-[\\d]{2}-[\\d]{2}$");
public static Date getDate(String date, String name) {
try {
Matcher matcher = normalPattern.matcher(date);
if (matcher != null && matcher.find()) {
if(name != null && name.startsWith("end")) {
date += " 23:59:59";
return dateTimeFormats.get().parse(date);
}
return dateFormats.get().parse(date);
}
for (Pattern pattern : threadLocalMap.keySet()) {
matcher = pattern.matcher(date);
if (matcher != null && matcher.find()) {
ThreadLocal<SimpleDateFormat> threadLocal = threadLocalMap.get(pattern);
return threadLocal.get().parse(date);
}
}
} catch (ParseException e){
throw new RuntimeException(e);
}
if(date != null && !"".equals(date)){
throw new RuntimeException("date.format.error");
}
return null;
}
public static String getDateTimeWithoutSecond(Timestamp timestamp){
SimpleDateFormat sdf = dateTimeWithoutSecondFormats.get();
return sdf.format(new Date(timestamp.getTime()));
}
public static Date parseDateTimeWithoutSecond(String date){
SimpleDateFormat sdf = dateTimeWithoutSecondFormats.get();
try {
return sdf.parse(date);
} catch (ParseException e) {
throw new RuntimeException(e);
}
}
public static String getDateTimeWithoutSecond(Date date){
SimpleDateFormat sdf = dateTimeWithoutSecondFormats.get();
return sdf.format(date);
}
public static String getDate(Date date){
SimpleDateFormat sdf = dateFormats.get();
return sdf.format(date);
}
public static String currentDate(){
return dateTimeFormats.get().format(new Date());
}
/**
* yyyyMMdd
* @param date
* @return
*/
public static String getSimpleDate(Date date){
SimpleDateFormat sdf = simpleDateFormats.get();
return sdf.format(date);
}
public static String getSimpleDateTime(){
SimpleDateFormat sdf = simpleDateTimeFormats.get();
return sdf.format(new Date());
}
public static String getSimpleDateTime(Date date){
SimpleDateFormat sdf = simpleDateTimeFormats.get();
return sdf.format(date);
}
public static Date parseDate(String date){
SimpleDateFormat sdf = dateFormats.get();
try {
return sdf.parse(date);
} catch (ParseException e) {
e.printStackTrace();
}
return null;
}
public static String getDateTime(Date date){
SimpleDateFormat sdf = dateTimeFormats.get();
return sdf.format(date);
}
public static Date parseDateTime(String date){
SimpleDateFormat sdf = dateTimeFormats.get();
try {
return sdf.parse(date);
} catch (ParseException e) {
e.printStackTrace();
}
return null;
}
public static String formatYYYYMMDD(Date date){
SimpleDateFormat sdf = simpleDateFormats.get();
return sdf.format(date);
}
public static Date parseYYYYMMDD(String date){
SimpleDateFormat sdf = simpleDateFormats.get();
try {
return sdf.parse(date);
} catch (ParseException e) {
e.printStackTrace();
}
return null;
}
public static String formatYYYYMMDDHHMMSS(Date date){
SimpleDateFormat sdf = simpleDateTimeFormats.get();
return sdf.format(date);
}
public static Date parseYYYYMMDDHHMMSS(String date){
SimpleDateFormat sdf = simpleDateTimeFormats.get();
try {
return sdf.parse(date);
} catch (ParseException e) {
e.printStackTrace();
}
return null;
}
public static String formatYYYY_MM_DD(Date date){
SimpleDateFormat sdf = dateFormats.get();
return sdf.format(date);
}
public static Date parseYYYY_MM_DD(String date){
SimpleDateFormat sdf = dateFormats.get();
try {
return sdf.parse(date);
} catch (ParseException e) {
e.printStackTrace();
}
return null;
}
public static String formatYYYY_MM_DD_HH(Date date){
SimpleDateFormat sdf = dateTimeHourFormats.get();
return sdf.format(date);
}
public static Date parseYYYY_MM_DD_HH(String date){
SimpleDateFormat sdf = dateTimeHourFormats.get();
try {
return sdf.parse(date);
} catch (ParseException e) {
e.printStackTrace();
}
return null;
}
public static String formatYYYY_MM_DD_HH_MM(Date date){
SimpleDateFormat sdf = dateTimeWithoutSecondFormats.get();
return sdf.format(date);
}
public static Date parseYYYY_MM_DD_HH_MM(String date){
SimpleDateFormat sdf = dateTimeWithoutSecondFormats.get();
try {
return sdf.parse(date);
} catch (ParseException e) {
e.printStackTrace();
}
return null;
}
public static String formatYYYY_MM_DD_HH_MM_SS(Date date){
SimpleDateFormat sdf = dateTimeFormats.get();
return sdf.format(date);
}
public static Date parseYYYY_MM_DD_HH_MM_SS(String date){
SimpleDateFormat sdf = dateTimeFormats.get();
try {
return sdf.parse(date);
} catch (ParseException e) {
e.printStackTrace();
}
return null;
}
//计算多少天后的corn表达式,计时器处使用
public static String formatDateByPattern(Date date,String dateFormat){
SimpleDateFormat sdf =new SimpleDateFormat(dateFormat);
String formaTimeStr = null ;
if(date != null){
formaTimeStr = sdf.format(date);
}
return formaTimeStr;
}
public static String getCron(Date date){
String dateFormat = "ss mm HH dd MM ? yyyy";
return formatDateByPattern(date,dateFormat);
}
public static Date getCurrentBeforeNum(int num,Date date){
Calendar cal = Calendar.getInstance();
cal.setTime(date);
cal.add(Calendar.DATE,num);
return cal.getTime();
}
}