package com.yang.simp.utils;
import java.io.Serializable;
import java.text.ParseException;
import java.util.Calendar;
import java.util.Comparator;
import java.util.Date;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.apache.commons.lang3.time.DateUtils;
public final class DateTime implements Serializable, Comparable<DateTime>,
Comparator<DateTime> {
private static final long serialVersionUID = -8984010892578112458L;
/**
* 鏍囧噯GTM鏃堕棿(1970-01-01 00:00:00)
*/
public static final DateTime STANDARD_GTM_TIME = new DateTime(0);
/**
* 榛樿鏃ユ湡鏍煎紡(yyyy-MM-dd HH:mm:ss)
*/
public static final String DEFAULT_DATETIME_FORMAT = "yyyy-MM-dd HH:mm:ss";
/**
* 姣忓ぉ姣鏁?
*/
public static final long MILLISECONDS_PER_DAY = 1000 * 60 * 60 * 24;
/**
* 姣忓ぉ绉掓暟
*/
public static final long SECONDS_PER_DAY = 60 * 60 * 24;
/**
* 姣忓ぉ鍒嗛挓鏁?
*/
public static final long MINUTES_PER_DAY = 60 * 24;
/**
* 姣忓ぉ灏忔椂鏁?
*/
public static final long HOURS_PER_DAY = 24;
/**
* 姣忓皬鏃舵绉掓暟
*/
public static final long MILLISECONDS_PER_HOUR = 1000 * 60 * 60;
/**
* 姣忓皬鏃剁鏁?
*/
public static final long SECONDS_PER_HOUR = 60 * 60;
/**
* 姣忓皬鏃跺垎閽熸暟
*/
public static final long MINUTES_PER_HOUR = 60;
/**
* 姣忓垎閽熸绉掓暟
*/
public static final long MILLISECONDS_PER_MINUTE = 1000 * 60;
/**
* 姣忓垎閽熺鏁?
*/
public static final long SECONDS_PER_MINUTE = 60;
/**
* 姣忕姣鏁?
*/
public static final long MILLISECONDS_PER_SECOND = 1000;
/**
* Calendar Instance
*/
private Calendar _c = Calendar.getInstance();
/**
* 浠ュ綋鍓嶆椂闂村垱寤篋ateTime瀵硅薄
*/
public DateTime() {
_c.setTime(new Date());
}
/**
* 鏍规嵁Calendar瀵硅薄鍒涘缓DateTime瀵硅薄
*
* @param c
*/
public DateTime(Calendar c) {
_c = c;
}
/**
* 鏍规嵁java.util.Date瀵硅薄鍒涘缓DateTime瀵硅薄
*
* @param c
*/
public DateTime(Date date) {
_c.setTime(date);
}
/**
* 鏍规嵁java.sql.Date瀵硅薄鍒涘缓DateTime瀵硅薄
*
* @param c
*/
public DateTime(java.sql.Date sqlDate) {
_c.setTime(sqlDate);
}
/**
* 鏍规嵁timestamp鍒涘缓DateTime瀵硅薄
*
* @param timestamp
*/
public DateTime(long timestamp) {
_c.setTime(new Date(timestamp));
}
/**
* 鏍规嵁骞? 鏈? 鏃ュ垱寤篋ateTime瀵硅薄
*
* @param year
* 骞翠唤
* @param month
* 鏈堜唤 (鍖哄埆浜嶤alendar, 姝ゅ璁剧疆1鏈堟椂涓?)
* @param day
* 鏃?(姝ゅ璁剧疆1鍙锋椂涓?)
*/
public DateTime(int year, int month, int day) {
_c.set(Calendar.YEAR, year);
_c.set(Calendar.MONTH, month - 1);
_c.set(Calendar.DAY_OF_MONTH, day);
_c.set(Calendar.HOUR_OF_DAY, 0);
_c.set(Calendar.MINUTE, 0);
_c.set(Calendar.SECOND, 0);
_c.set(Calendar.MILLISECOND, 0);
}
/**
* 鏍规嵁骞? 鏈? 鏃? 灏忔椂, 鍒嗛挓, 绉? 姣鍒涘缓DateTime瀵硅薄
*
* @param year
* 骞翠唤
* @param month
* 鏈堜唤 (鍖哄埆浜嶤alendar, 姝ゅ璁剧疆1鏈堟椂涓?)
* @param day
* 鏃?(姝ゅ璁剧疆1鍙锋椂涓?)
* @param hour
* 灏忔椂
* @param minute
* 鍒嗛挓
* @param second
* 绉?
* @param millisecond
* 姣
*/
public DateTime(int year, int month, int day, int hour, int minute,
int second, int millisecond) {
_c.set(Calendar.YEAR, year);
_c.set(Calendar.MONTH, month - 1);
_c.set(Calendar.DAY_OF_MONTH, day);
_c.set(Calendar.HOUR_OF_DAY, hour);
_c.set(Calendar.MINUTE, minute);
_c.set(Calendar.SECOND, second);
_c.set(Calendar.MILLISECOND, millisecond);
}
/**
* 鏍规嵁褰撳墠鏃堕棿鍒涘缓DateTime瀵硅薄
*
* @return
*/
public static DateTime now() {
return new DateTime();
}
public static String format(Date date) {
return DateFormatUtils.format(date, DEFAULT_DATETIME_FORMAT);
}
public static String format(Date date, String format) {
return DateFormatUtils.format(date, format);
}
public static String format(java.sql.Date date) {
return DateFormatUtils.format(date, DEFAULT_DATETIME_FORMAT);
}
public static String format(java.sql.Date date, String format) {
return DateFormatUtils.format(date, format);
}
public static String format(long millisecond) {
return DateFormatUtils.format(millisecond, DEFAULT_DATETIME_FORMAT);
}
public static String format(long millisecond, String format) {
return DateFormatUtils.format(millisecond, format);
}
@Override
public String toString() {
return toString(DEFAULT_DATETIME_FORMAT);
}
/**
* 鏍规嵁鎸囧畾鏍煎紡鏍煎紡鍖栨椂闂?
*
* @param format
* @return
*/
public String toString(String format) {
if (_c == null) {
return null;
}
return DateFormatUtils.format(_c, format);
}
/**
* 璁剧疆DateTime瀵硅薄鐨勫勾, 鏈? 鏃?
*
* @param year
* 骞翠唤
* @param month
* 鏈堜唤 (鍖哄埆浜嶤alendar, 姝ゅ璁剧疆1鏈堟椂涓?)
* @param day
* 鏃?(姝ゅ璁剧疆1鍙锋椂涓?)
* @return
*/
public DateTime set(int year, int month, int day) {
this.setYear(year);
this.setMonth(month);
this.setDay(day);
return this;
}
/**
* 璁剧疆DateTime瀵硅薄鐨勫勾, 鏈? 鏃? 灏忔椂, 鍒嗛挓, 绉? 姣
*
* @param year
* 骞翠唤
* @param month
* 鏈堜唤 (鍖哄埆浜嶤alendar, 姝ゅ璁剧疆1鏈堟椂涓?)
* @param day
* 鏃?(姝ゅ璁剧疆1鍙锋椂涓?)
* @param hour
* 灏忔椂
* @param minute
* 鍒嗛挓
* @param second
* 绉?
* @param millisecond
* 姣
* @return
*/
public DateTime set(int year, int month, int day, int hour, int minute,
int second, int millisecond) {
this.setYear(year);
this.setMonth(month);
this.setDay(day);
this.setHour(hour);
this.setMinute(minute);
this.setSecond(second);
this.setMilliSecond(millisecond);
return this;
}
/**
* 璁剧疆DateTime瀵硅薄鐨勫皬鏃? 鍒嗛挓, 绉? 姣
*
* @param hour
* 灏忔椂
* @param minute
* 鍒嗛挓
* @param second
* 绉?
* @param millisecond
* 姣
* @return
*/
public DateTime set(int hour, int minute, int second, int millisecond) {
this.setHour(hour);
this.setMinute(minute);
this.setSecond(second);
this.setMilliSecond(millisecond);
return this;
}
/**
* 鑾峰彇褰撳墠鏃堕棿鎴?
*
* @return
*/
public long getTime() {
return _c.getTime().getTime();
}
/**
* 杞崲涓?java.util.Date 瀵硅薄
*
* @return
*/
public Date getDate() {
return _c.getTime();
}
/**
* 杞崲涓?java.sql.Date 瀵硅薄
*
* @return
*/
public Date getSqlDate() {
return new java.sql.Date(_c.getTime().getTime());
}
/**
* 鑾峰彇骞翠唤
*
* @return
*/
public int getYear() {
return _c.get(Calendar.YEAR);
}
/**
* 璁剧疆骞翠唤
*
* @param year
* @return
*/
public DateTime setYear(int year) {
_c.set(Calendar.YEAR, year);
return this;
}
/**
* 鑾峰彇鏈堜唤 (1鏈堜负1)
*
* @return
*/
public int getMonth() {
return _c.get(Calendar.MONTH) + 1;
}
/**
* 璁剧疆鏈堜唤 (1鏈堜负1)
*
* @param month
* @return
*/
public DateTime setMonth(int month) {
_c.set(Calendar.MONTH, month - 1);
return this;
}
/**
* 鑾峰彇澶?
*
* @return
*/
public int getDay() {
return _c.get(Calendar.DAY_OF_MONTH);
}
/**
* 璁剧疆澶?
*
* @param day
* @return
*/
public DateTime setDay(int day) {
_c.set(Calendar.DATE, day);
return this;
}
/**
* 鑾峰彇灏忔椂
*
* @return
*/
public int getHour() {
return _c.get(Calendar.HOUR_OF_DAY);
}
/
评论3
最新资源