?? datetimeutils.java
字號:
*/
public static Timestamp ZERO_TIMESTAMP = new Timestamp( (long) 00000000000000);
public static String NULL_TIMESTAMP_DISPLAY = "1970-01-01 00:00:00";
/**
* Value needed to create Timestamp representing
* "January 1, 1970 00:00:00".
* From the documentation, you would think this would be
* Timestamp(0), but empirical tests show it to be
* Timestamp(18000000).
*/
public static long NULL_TIME = (long) 18000000;
/**
* Timestamp representing "January 1, 1970 00:00:00".
*/
public static Timestamp NULL_TIMESTAMP = new Timestamp(NULL_TIME);
/**
* Timestamp representing "December 31, 2029, 23:59:59.9"
*/
public static Timestamp MAX_TIMESTAMP = Timestamp.valueOf(
"2029-12-31 23:59:59.999");
/**
* Return the String representing "January 1, 1970 00:00:00".
*/
public static String getTimestampDisplayNull() {
return NULL_TIMESTAMP_DISPLAY;
}
/**
* Return the String representing the current timestamp;
*/
public static String getTimestampDisplay() {
return getTimestamp(new Timestamp(System.currentTimeMillis()));
}
/**
* @deprecated Use getTimestampDisplay.
*/
public static String getTimestampText() {
return getTimestampDisplay();
}
/**
* Return null if timestamp is null or equals
* "January 1, 1970 00:00:00".
*/
public static boolean isNull(Timestamp timestamp) {
return ( (timestamp == null) || (timestamp.getTime() == NULL_TIME));
}
/**
* Factory method to return timestamp initialized to
* current system time.
* For timestamp as a String in the default format,
* use <code>getTimestamp().toString()</code>.
*/
public static Timestamp getTimestamp() {
return new Timestamp(System.currentTimeMillis());
}
/**
* Convert timestamp to String for given locale in given style.
* A null locale will return the default locale.
*/
public static String getTimestamp(Timestamp timestamp,
Locale locale, int style) {
Date date = (Date) timestamp;
if (locale == null) {
return (DateFormat.getDateTimeInstance(style, style).
format(date));
}
return (DateFormat.getDateTimeInstance(style, style, locale).
format(date));
}
/**
* Convert date to String for default locale in given style.
* A null locale will return the default locale.
*/
public static String getTimestamp(Timestamp timestamp,
int style) {
return getTimestamp(timestamp, (Locale)null, style);
}
/**
* Convert date to String for default locale in DEFAULT style.
* A null locale will return the default locale.
*/
public static String getTimestamp(Timestamp timestamp) {
return getTimestamp(timestamp, (Locale)null, DEFAULT);
}
/**
* Return Timestamp value using a String.
* Null or conversion error returns null.
* @param String representing Timestamp
*/
public static Timestamp toTimestamp(String string) {
if (string == null) {
return null;
}
else {
try {
return Timestamp.valueOf(string);
}
catch (Throwable t) {
return null;
}
}
}
/**
* Return a Timestamp based on the parameters.
* Any nulls or conversion error returns null.
* @param year The year
* @param month The month
* @param day The day
* @returns Timestamp for year-month-day
*/
public static Timestamp toTimestamp(String year, String month, String day) {
if ( (null == year) || (null == month) || (null == day)) {
return null;
}
StringBuffer sb = new StringBuffer();
// YEAR-MM-DD 00:00:00.0
sb.append(year);
sb.append(DATE_SEPARATOR);
sb.append(month);
sb.append(DATE_SEPARATOR);
sb.append(day);
sb.append(TIMESTAMP_TIME_ZERO);
return toTimestamp(sb.toString());
}
/**
* Return String value representing Timestamp.
* Null returns null.
* @param Timestamp
*/
public static String toString(Timestamp timestamp) {
if (timestamp == null) {
return null;
}
else {
return timestamp.toString();
}
}
// 當前日期加減n天后的日期,返回String (yyyy-mm-dd)
public static String nDaysAftertoday(int n) {
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
Calendar rightNow = Calendar.getInstance();
//rightNow.add(Calendar.DAY_OF_MONTH,-1);
rightNow.add(Calendar.DAY_OF_MONTH, +n);
return df.format(rightNow.getTime());
}
// 當前日期加減n天后的日期,返回Date
public static Date nDaysAfterNowDate(int n) {
Calendar rightNow = Calendar.getInstance();
//rightNow.add(Calendar.DAY_OF_MONTH,-1);
rightNow.add(Calendar.DAY_OF_MONTH, +n);
return rightNow.getTime();
}
// 給定一個日期型字符串,返回加減n天后的日期型字符串
public static String nDaysAfterOneDateString(String basicDate, int n) {
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
Date tmpDate = null;
try {
tmpDate = df.parse(basicDate);
}
catch (Exception e) {
// 日期型字符串格式錯誤
}
long nDay = (tmpDate.getTime() / (24 * 60 * 60 * 1000) + 1 + n) *
(24 * 60 * 60 * 1000);
tmpDate.setTime(nDay);
return df.format(tmpDate);
}
// 給定一個日期,返回加減n天后的日期,返回Date
public static Date nDaysAfterOneDate(Date basicDate, int n) {
long nDay = (basicDate.getTime() / (24 * 60 * 60 * 1000) + 1 + n) *
(24 * 60 * 60 * 1000);
basicDate.setTime(nDay);
return basicDate;
}
// 當前日期加減n個月后的日期,返回String (yyyy-mm-dd)
public static String nMonthsAftertoday(int n) {
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
Calendar rightNow = Calendar.getInstance();
//rightNow.add(Calendar.DAY_OF_MONTH,-1);
rightNow.add(Calendar.MONTH, +n);
return df.format(rightNow.getTime());
}
// 當前日期加減n個月后的日期,返回Date
public static Date nMonthsAfterNowDate(int n) {
Calendar rightNow = Calendar.getInstance();
//rightNow.add(Calendar.DAY_OF_MONTH,-1);
rightNow.add(Calendar.MONTH, +n);
return rightNow.getTime();
}
// 當前日期加減n個月后的日期,返回Date
public static Date nMonthsAfterOneDate(Date basicDate, int n) {
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
Calendar rightNow = Calendar.getInstance();
rightNow.setTime(basicDate);
rightNow.add(Calendar.MONTH, +n);
return rightNow.getTime();
}
// 當前日期加減n個月后的日期,返回String
public static String nMonthsAfterOneDateString(Date basicDate, int n) {
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
Calendar rightNow = Calendar.getInstance();
rightNow.setTime(basicDate);
rightNow.add(Calendar.MONTH, +n);
return df.format(rightNow.getTime());
}
// 計算兩個日期相隔的天數
public static int nDaysBetweenTwoDate(Date firstDate, Date secondDate) {
int nDay = (int) ( (secondDate.getTime() - firstDate.getTime()) /
(24 * 60 * 60 * 1000));
return nDay;
}
// 計算兩個日期相隔的天數
public static int nDaysBetweenTwoDate(String firstString, String secondString) {
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
Date firstDate = null;
Date secondDate = null;
try {
firstDate = df.parse(firstString);
secondDate = df.parse(secondString);
}
catch (Exception e) {
// 日期型字符串格式錯誤
}
int nDay = (int) ( (secondDate.getTime() - firstDate.getTime()) /
(24 * 60 * 60 * 1000));
return nDay;
}
//得到給定日期是星期幾(從星期天開始,1-7)
public static int getWeekNumOfDate(Date date) {
GregorianCalendar calendar = (GregorianCalendar) GregorianCalendar.
getInstance();
calendar.setTime(date);
return calendar.get(GregorianCalendar.DAY_OF_WEEK);
}
public static int getWeekNumOfDate(String date) {
GregorianCalendar calendar = (GregorianCalendar) GregorianCalendar.
getInstance();
calendar.setTime(DateTimeUtils.toDate(date));
return calendar.get(GregorianCalendar.DAY_OF_WEEK);
}
//得到給定日期所在星期的某一天的日期
//weekDay: from sunday to SATURDAY,1 to 7
public static Date getWeekDayDate(int weekDay, Date date) {
Calendar c = Calendar.getInstance();
c.setTime(date);
if (weekDay >= Calendar.SUNDAY && weekDay <= Calendar.SATURDAY) {
int delta = weekDay - c.get(Calendar.DAY_OF_WEEK);
c.add(Calendar.DAY_OF_WEEK, delta);
}
return new Date(c.getTimeInMillis());
}
public static GregorianCalendar getnDayDiff(GregorianCalendar cal, int n) {
GregorianCalendar g1 = new GregorianCalendar(cal.get(Calendar.YEAR),
cal.get(Calendar.MONTH),
cal.get(Calendar.DAY_OF_MONTH));
g1.add(Calendar.DAY_OF_MONTH, +n);
return g1;
}
public static GregorianCalendar getnMonthDiff(GregorianCalendar cal, int n) {
GregorianCalendar g1 = new GregorianCalendar(cal.get(Calendar.YEAR),
cal.get(Calendar.MONTH),
cal.get(Calendar.DAY_OF_MONTH));
g1.add(Calendar.MONTH, +n);
return g1;
}
public static GregorianCalendar getnYearDiff(GregorianCalendar cal, int n) {
GregorianCalendar g1 = new GregorianCalendar(cal.get(Calendar.YEAR),
cal.get(Calendar.MONTH),
cal.get(Calendar.DAY_OF_MONTH));
g1.add(Calendar.YEAR, +n);
return g1;
}
public static String long2DateString(String longVal) {
if (Long.parseLong(longVal) <= 0) {
return "";
}
return dateToShortString(long2Date(longVal));
}
public static String long2DateString(String longVal, String format) {
if (Long.parseLong(longVal) <= 0) {
return "";
}
return dateToShortString(long2Date(longVal), format);
}
/**
* 取得間隔的天數
* @param begin long
* @param end long
* @return int
*/
public static int getDayDiff(long begin, long end) {
long interval = end - begin;
if (interval > 0) {
return (int) (interval / 3600 / 1000 / 24);
}
return 0;
}
public static int getDayDiff(String beginStr, String endStr) {
try {
long begin = Long.parseLong(beginStr);
long end = Long.parseLong(endStr);
return getDayDiff(begin, end);
}
catch (Exception e) {
}
return 0;
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -