日付に関する一般的な操作ツールクラスを共有
プロジェクト開発では、日付や時間に関する様々な操作がしばしば行われており、JDK関連の個人感覚を直接使用すると不便である.だから、プロジェクトでよく使われる操作について、ツールクラスを書きました.
私はまだ菜鳥ですが、下手なところはよろしくお願いします.
public class DateUtils {
/**
*
*/
public static String getCurrentDateStr(DateFormatType dateFormatType) {
Date date = getCurrentDate();
return (String) OpearationDate(date, dateFormatType.getValue());
}
/**
* 、
*/
public static String formatDate(Date date, DateFormatType dateFormatType) {
return (String) OpearationDate(date, dateFormatType.getValue());
}
/**
* 、
*/
public static Date parseDate(String dateStr, DateFormatType dateFormatType) {
return (Date) OpearationDate(dateStr, dateFormatType.getValue());
}
/**
* ( )
*/
public static Date getCurrentDate() {
Date date = new Date(System.currentTimeMillis());
return date;
}
/**
* 、 、 、 、 、
*/
public static int getCurrentTime(TimeFormatType timeFormatType) {
return getTime(getCurrentDate(), timeFormatType);
}
/**
* 、 、 、 、 、
*/
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(" ", e);
}
}
/**
*
*/
public static long getMillis(Date date) {
java.util.Calendar c = java.util.Calendar.getInstance();
c.setTime(date);
return c.getTimeInMillis();
}
/**
* 、
*
* :
*/
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));
}
/**
* 、
*/
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();
}
/**
* 、
*/
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(" ");
}
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(" ", e);
}
}
public enum DateOperationType {
/**
*
*/
ADD(true),
/**
*
*/
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 {
/**
* :yyyy-MM-dd HH:mm:ss
*/
DATE_FORMAT_STR("yyyy-MM-dd HH:mm:ss"),
/**
* :yyyyMMddHHmmss
*/
SIMPLE_DATE_TIME_FORMAT_STR("yyyyMMddHHmmss"),
/**
* :yyyy-MM-dd
*/
SIMPLE_DATE_FORMAT_STR("yyyy-MM-dd"),
/**
* :yyyy/MM/dd
*/
SIMPLE_DATE_FORMAT_VIRGULE_STR("yyyy/MM/dd"),
/**
* :HH:mm:ss
*/
HOUR_MINUTE_SECOND("HH:mm:ss"),
/**
* :HH:mm
*/
HOUR_MINUTE("HH:mm");
private final String value;
DateFormatType(String formatStr) {
this.value = formatStr;
}
public String getValue() {
return value;
}
}
}
私はまだ菜鳥ですが、下手なところはよろしくお願いします.