Java中日期时间工具类
Java中的日期时间处理非常麻烦,故写了日期时间处理的工具类
1. DateTime工具类
package Cloud.Base;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Map;
public class DateTime
{
public static void PrintNow(String NodeName, int VehicleDirection, String LPR)
{
//获得当前时间
SimpleDateFormat Df = new SimpleDateFormat("YYYY-MM-dd HH:mm:ss.SSS");
String CurrentTime = Df.format(new Date());
//获得要打印的信息
String DirectionString = (1 == VehicleDirection) ? "Enter" : "Leave";
String Info = String.format("\r\n\033[36m %s [(%s)%20s] LPR:%s\033[m", CurrentTime, DirectionString, NodeName, LPR);
//打印进入的记录
Loger.Log.warn(Info);
}
public static Date Now()
{
return new Date();
}
public static Long NowLong()
{
SimpleDateFormat DateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
return Long.valueOf(DateFormat.format(Now()));
}
public static Long ParseLong(Date DateItem)
{
//判断参数是否合法
if (null == DateItem)
{
return 0L;
}
SimpleDateFormat DateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
return Long.valueOf(DateFormat.format(DateItem));
}
public static Long NowTimeLong()
{
SimpleDateFormat DateFormat = new SimpleDateFormat("HHmmss");
return Long.valueOf(DateFormat.format(DateTime.Now()));
}
public static Long ParseLong(String DateString)
{
//判断参数是否合法
if (true == StringUtility.IsNullOrEmpty(DateString))
{
return 0L;
}
//转换为日期时间型
Date DateItem = DateTime.Parse(DateString);
if (null == DateItem)
{
return 0L;
}
SimpleDateFormat DateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
return Long.valueOf(DateFormat.format(DateItem));
}
public static Date TodayLast()
{
Date Current = Now();
Current.setHours(23);
Current.setMinutes(59);
Current.setSeconds(59);
return Current;
}
public static Date Last(String Item)
{
Date Value = DateTime.Parse(Item);
if (null == Value)
{
Loger.Log.warn(Item);
return null;
}
Value.setHours(23);
Value.setMinutes(59);
Value.setSeconds(59);
return Value;
}
public static Date Early(String Item)
{
Date Value = DateTime.Parse(Item);
if (null == Value)
{
Loger.Log.warn(Item);
return null;
}
Value.setHours(0);
Value.setMinutes(0);
Value.setSeconds(0);
return Value;
}
public static String NowString()
{
//获得格式化的格式
String CurrentFormat = DateTimeStyle.GetFormater(DateTimeStyle.DateTime);
if (null == CurrentFormat)
{
Loger.Log.warn(Error.NULL_REFERENCE);
Loger.Log.warn(DateTimeStyle.DateTime);
return null;
}
SimpleDateFormat DateFormat = new SimpleDateFormat(CurrentFormat);
return DateFormat.format(Now());
}
public static String ToString(Date Date)
{
//获得格式化的格式
String CurrentFormat = DateTimeStyle.GetFormater(DateTimeStyle.DateTime);
if (null == CurrentFormat)
{
Loger.Log.warn(Error.NULL_REFERENCE);
Loger.Log.warn(DateTimeStyle.DateTime);
return null;
}
SimpleDateFormat DateFormat = new SimpleDateFormat(CurrentFormat);
return DateFormat.format(Date);
}
public static String ToOnlyDateString(Date Date)
{
//获得格式化的格式
String CurrentFormat = DateTimeStyle.GetFormater(DateTimeStyle.DateOnly);
if (null == CurrentFormat)
{
Loger.Log.warn(Error.NULL_REFERENCE);
Loger.Log.warn(DateTimeStyle.DateOnly);
return null;
}
SimpleDateFormat DateFormat = new SimpleDateFormat(CurrentFormat);
return DateFormat.format(Date);
}
public static String ToOnlyTimeString(Date Date)
{
//获得格式化的格式
String CurrentFormat = DateTimeStyle.GetFormater(DateTimeStyle.TimeOnly);
if (null == CurrentFormat)
{
Loger.Log.warn(Error.NULL_REFERENCE);
Loger.Log.warn(DateTimeStyle.TimeOnly);
return null;
}
SimpleDateFormat DateFormat = new SimpleDateFormat(CurrentFormat);
return DateFormat.format(Date);
}
public static Date Parse(String DateString)
{
//判断参数是否合法
if (null == DateString)
{
Loger.Log.warn(Error.INVALID_DATE_TIME_FORMAT);
Loger.Log.warn(Error.NULL_REFERENCE);
return null;
}
//获得格式化的格式
String CurrentFormat = DateTimeStyle.GetFormater(DateString);
if (null == CurrentFormat)
{
Loger.Log.warn(Error.NULL_REFERENCE);
Loger.Log.warn(DateString);
return null;
}
//对日期进行转换
SimpleDateFormat DateFormat = new SimpleDateFormat(CurrentFormat);
try
{
return DateFormat.parse(DateString);
}
catch (ParseException e)
{
Loger.Log.warn(CurrentFormat);
Loger.Log.warn(DateString);
Loger.Log.warn(e.toString());
e.printStackTrace();
}
return null;
}
public static String NowOnlyDateString()
{
//获得格式化的格式
String CurrentFormat = DateTimeStyle.GetFormater(DateTimeStyle.DateOnly);
if (null == CurrentFormat)
{
Loger.Log.warn(Error.NULL_REFERENCE);
Loger.Log.warn(DateTimeStyle.DateOnly);
return null;
}
SimpleDateFormat DateFormat = new SimpleDateFormat(CurrentFormat);
return DateFormat.format(new Date());
}
public static String NowOnlyTimeString()
{
//获得格式化的格式
String CurrentFormat = DateTimeStyle.GetFormater(DateTimeStyle.TimeOnly);
if (null == CurrentFormat)
{
Loger.Log.warn(Error.NULL_REFERENCE);
Loger.Log.warn(DateTimeStyle.TimeOnly);
return null;
}
SimpleDateFormat DateFormat = new SimpleDateFormat(CurrentFormat);
return DateFormat.format(new Date());
}
public static Date Infinite()
{
return Parse(InfiniteString());
}
public static String InfiniteString()
{
return "2999-12-12 23:59:59";
}
public static Date Add(Date Reference, int Seconds)
{
Calendar Now = Calendar.getInstance();
Now.setTime(Reference);
Now.add(Calendar.SECOND, Seconds);
return Now.getTime();
}
public static long AddLong(long LongDateTime, int AddValue)
{
Date Date = DateTime.Parse(String.valueOf(LongDateTime));
Date = DateTime.Add(Date, AddValue);
return DateTime.ParseLong(Date);
}
public static long SubDateSeconds(Date Date1, Date Date2)
{
//判断参数是否合法
if ((null == Date1) || (null == Date2))
{
Loger.Log.warn(Error.NULL_REFERENCE);
return 0;
}
long SubMillisecond = Date1.getTime() - Date2.getTime();
return SubMillisecond / 1000;
}
public static long SubDateSecondsOfDateLong(long Date1, long Date2)
{
//判断参数是否合法
if ((0 == Date1) || (0 == Date2))
{
Loger.Log.warn(Error.NULL_REFERENCE);
return 0;
}
//转换时间
Date D1 = DateTime.Parse(String.valueOf(Date1));
Date D2 = DateTime.Parse(String.valueOf(Date2));
//减时间
return SubDateSeconds(D1, D2);
}
public static Date CombineTime(String Time)
{
Calendar Now = Calendar.getInstance();
Now.setTime(new Date());
return Parse(String.format("%4d-%02d-%02d %s", Now.get(Calendar.YEAR), Now.get(Calendar.MONTH) + 1, Now.get(Calendar.DAY_OF_MONTH), Time));
}
public static long TimeToLong(String Time)
{
if (true == StringUtility.IsNullOrEmpty(Time))
{
Loger.Log.error(Error.INVALID_PARAMETER);
return 0;
}
try
{
return Long.valueOf(Time.replace(":", ""));
}
catch (Exception Ex)
{
Ex.printStackTrace();
return 0;
}
}
public static int FormatDateTime(Map<String, Object> InputData, String FieldName, boolean IgnoreIfFieldNotExist)
{
int Result = Error.SUCCESS;
//判断参数是否合法
if ((null == InputData) || (true == StringUtility.IsNullOrEmpty(FieldName)))
{
Loger.Log.warn(Result = Error.NULL_REFERENCE);
return Result;
}
//获取字段的值
String Value = null;
if (false == InputData.containsKey(FieldName))
{
if (false == IgnoreIfFieldNotExist)
{
Loger.Log.warn(Result = Error.INVALID_PARAMETER);
Loger.Log.warn(FieldName);
return Result;
}
return Result;
}
Value = InputData.get(FieldName).toString();
//转换为标准的日期时间类型
Date Date = DateTime.Parse(Value);
if (null == Date)
{
Loger.Log.warn(Result = Error.INVALID_PARAMETER);
Loger.Log.warn(Value);
return Result;
}
//转换为字符串
String DateString = DateTime.ToString(Date);
//回填
InputData.put(FieldName, DateString);
return Result;
}
public static int FormatDateTimeToLong(Map<String, Object> InputData, String FieldName, boolean IgnoreIfFieldNotExist)
{
int Result = Error.SUCCESS;
//判断参数是否合法
if ((null == InputData) || (true == StringUtility.IsNullOrEmpty(FieldName)))
{
Loger.Log.warn(Result = Error.NULL_REFERENCE);
return Result;
}
//获取字段的值
String Value = null;
if (false == InputData.containsKey(FieldName))
{
if (false == IgnoreIfFieldNotExist)
{
Loger.Log.warn(Result = Error.INVALID_PARAMETER);
Loger.Log.warn(FieldName);
return Result;
}
return Result;
}
Value = InputData.get(FieldName).toString();
//转换为标准的日期时间类型
Date Date = DateTime.Parse(Value);
if (null == Date)
{
Loger.Log.warn(Result = Error.INVALID_PARAMETER);
Loger.Log.warn(Value);
return Result;
}
//转换为长整形
long DateLong = DateTime.ParseLong(Date);
//回填
InputData.put(FieldName, DateLong);
return Result;
}
public static void main(String[] args)
{
System.out.println(DateTime.ToString(DateTime.Add(DateTime.Now(), 0 - 259200)));
}
}
2.DateTimeStyle工具类
package Cloud.Base;
import java.util.*;
public enum DateTimeStyle
{
INVALID, DateTime, DateTimeNoSecond, DateOnly, DateOnlyNoDay, TimeOnly, TimeOnlyNoSecond, DateTimeLong, DateTimeLongSSS;
private static Map<DateTimeStyle, String> FormaterList = new Hashtable<DateTimeStyle, String>();
static
{
FormaterList.put(DateTime, "yyyy-MM-dd HH:mm:ss");
FormaterList.put(DateTimeNoSecond, "yyyy-MM-dd HH:mm");
FormaterList.put(DateOnly, "yyyy-MM-dd");
FormaterList.put(DateOnlyNoDay, "yy-MM");
FormaterList.put(TimeOnly, "HH:mm:ss");
FormaterList.put(TimeOnlyNoSecond, "HH:mm");
FormaterList.put(DateTimeLong, "yyyyMMddHHmmss");
FormaterList.put(DateTimeLongSSS, "yyyyMMddHHmmssSSS");
}
public static String GetFormater(DateTimeStyle Style)
{
String Format = FormaterList.get(Style);
if (null == Format)
{
Loger.Log.warn(Error.NULL_REFERENCE);
Loger.Log.warn(Style);
return null;
}
return Format;
}
public static String GetFormater(String DateString)
{
//判断参数是否合法
if (null == DateString)
{
Loger.Log.warn(Error.NULL_REFERENCE);
Loger.Log.warn(Error.INVALID_PARAMETER);
return null;
}
//先获得样式
DateTimeStyle Style = GetStyle(DateString);
if (INVALID == Style)
{
Loger.Log.warn(Error.NULL_REFERENCE);
Loger.Log.warn(DateString);
return null;
}
String Format = FormaterList.get(Style);
if (null == Format)
{
Loger.Log.warn(Error.NULL_REFERENCE);
Loger.Log.warn(Style);
return null;
}
return Format;
}
public static DateTimeStyle GetStyle(String DateString)
{
int DateSpliterCount = 0;
int TimeSpliterCount = 0;
char DateSpliter = '-';
char TimeSpliter = ':';
//判断参数是否合法
if (null == DateString)
{
Loger.Log.warn(Error.NULL_REFERENCE);
Loger.Log.warn(Error.INVALID_PARAMETER);
return INVALID;
}
//获得-和:的个数
int Length = DateString.length();
for (int i = 0; i < Length; i++)
{
char Current = DateString.charAt(i);
if (DateSpliter == Current)
{
DateSpliterCount++;
}
if (TimeSpliter == Current)
{
TimeSpliterCount++;
}
}
//DateTime
if ((2 == DateSpliterCount) && (2 == TimeSpliterCount))
{
return DateTime;
}
//DateTimeNoSecond
if ((2 == DateSpliterCount) && (1 == TimeSpliterCount))
{
return DateTimeNoSecond;
}
//DateOnly
if ((2 == DateSpliterCount) && (0 == TimeSpliterCount))
{
return DateOnly;
}
//DateOnlyNoDay
if ((1 == DateSpliterCount) && (0 == TimeSpliterCount))
{
return DateOnlyNoDay;
}
//TimeOnly
if ((0 == DateSpliterCount) && (2 == TimeSpliterCount))
{
return TimeOnly;
}
//TimeOnlyNoSecond
if ((0 == DateSpliterCount) && (1 == TimeSpliterCount))
{
return TimeOnlyNoSecond;
}
//DateTimeLong
if ((0 == DateSpliterCount) && (0 == TimeSpliterCount) && (14 == Length))
{
return DateTimeLong;
}
//DateTimeLongSSS
if ((0 == DateSpliterCount) && (0 == TimeSpliterCount) && (17 == Length))
{
return DateTimeLongSSS;
}
Loger.Log.warn(Error.INVALID_PARAMETER);
Loger.Log.warn(DateString);
return INVALID;
}
}