Thursday, September 2, 2010

A common operation tools on the date

public class DateUtils (/ ** * get the current time and date string * / public static String getCurrentDateStr (DateFormatType dateFormatType) (Date date = getCurrentDat ...
public class DateUtils (
/ **

* Get the current time and date string

* /

public static String getCurrentDateStr (DateFormatType dateFormatType) (

Date date = getCurrentDate ();



return (String) OpearationDate (date, dateFormatType.getValue ());

)

/ **

* Time, date format string

* /

public static String formatDate (Date date, DateFormatType dateFormatType) (

return (String) OpearationDate (date, dateFormatType.getValue ());

)

/ **

* From the string parsing into the time, date

* /

public static Date parseDate (String dateStr, DateFormatType dateFormatType) (

return (Date) OpearationDate (dateStr, dateFormatType.getValue ());

)

/ **

* Get the current system time (original format)

* /

public static Date getCurrentDate () (

Date date = new Date (System.currentTimeMillis ());

return date;

)

/ **

* Get the current date of the year, month, day, hour, minute, second

* /

public static int getCurrentTime (TimeFormatType timeFormatType) (

return getTime (getCurrentDate (), timeFormatType);

)

/ **

* Get the specified date of the year, month, day, hour, minute, second

* /

public static int getTime (Date date, TimeFormatType timeFormatType) (

try (

Calendar c = Calendar.getInstance ();

c.setTime (date);

int type = timeFormatType.getValue ();

int i = c.get (type);

return type == 2? i + 1: i;

) Catch (Exception e) (

throw new RuntimeException ("get failed", e);

)

)

/ **

* Get the number of milliseconds specified date

* /

public static long getMillis (Date date) (

java.util.Calendar c = java.util.Calendar.getInstance ();

c.setTime (date);

return c.getTimeInMillis ();

)

/ **

* Date added together, by operation

*

* Results are returned units: days

* /

public static int operationDate (Date date, Date diffDate, DateOperationType dateOperationType) (

long add = getMillis (date) + getMillis (diffDate);

long diff = getMillis (date) - getMillis (diffDate);

return (int) ((dateOperationType.getValue ()? add: diff) / (24 * 3600 * 1000));

)

/ **

* Add date month, reducing operation

* /

public static Date operationDateOfMonth (Date date, int month, DateOperationType dateOperationType) (

Calendar c = Calendar.getInstance ();

c.setTime (date);

c.add (Calendar.MONTH, dateOperationType.getValue ()? month: month - (month * 2));

return c.getTime ();

)

/ **

* Date of days add up, by operation

* /

public static Date operationDateOfDay (Date date, int day, DateOperationType dateOperationType) (

Calendar c = Calendar.getInstance ();

c.setTime (date);

long millis = c.getTimeInMillis ();

long millisOfday = day * 24 * 3600 * 1000;

long sumMillis = dateOperationType.getValue ()? (millis + millisOfday): (millis - millisOfday);

c.setTimeInMillis (sumMillis);


return c.getTime ();
)

private static Object OpearationDate (Object object, String formatStr) (

if (object == null | | null == formatStr | | "". equals (formatStr)) (

throw new RuntimeException ("argument can not be empty");

)

SimpleDateFormat format = new SimpleDateFormat (formatStr);

try (

if (object instanceof Date)

return format.format (object);

else

return format.parse (object.toString ());

) Catch (Exception e) (

throw new RuntimeException ("operation failed", e);

)

)

public enum DateOperationType (

/ **

* Add operation

* /

ADD (true),

/ **

* Reduction operation

* /

DIFF (false);

private final boolean value;

DateOperationType (boolean operation) (

this.value = operation;

)

public boolean getValue () (

return value;

)

)

public enum TimeFormatType (

YEAR (1), MONTH (2), DAY (5), HOUR (11), MINUTE (12), SECOND (13);

private final int value;

TimeFormatType (int formatStr) (

this.value = formatStr;

)

public int getValue () (

return value;

)

)

public enum DateFormatType (

/ **

* Format: yyyy-MM-dd HH: mm: ss

* /

DATE_FORMAT_STR ("yyyy-MM-dd HH: mm: ss"),

/ **

* Format: yyyyMMddHHmmss

* /

SIMPLE_DATE_TIME_FORMAT_STR ("yyyyMMddHHmmss"),

/ **

* Format: yyyy-MM-dd

* /

SIMPLE_DATE_FORMAT_STR ("yyyy-MM-dd"),

/ **

* Format: yyyy / MM / dd

* /

SIMPLE_DATE_FORMAT_VIRGULE_STR ("yyyy / MM / dd"),

/ **

* Format: HH: mm: ss

* /

HOUR_MINUTE_SECOND ("HH: mm: ss"),

/ **

* Format: HH: mm

* /

HOUR_MINUTE ("HH: mm");

private final String value;

DateFormatType (String formatStr) (

this.value = formatStr;

)

public String getValue () (

return value;

)

)

)

No comments:

Post a Comment