java常用組件之DateUtils

/**
*
*/
package cn.ccb.jstsccf.common.utils;

import java.sql.Timestamp;
import java.text.ParseException;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import org.apache.commons.lang.time.DateFormatUtils;

/**
* @author ghl
*
*/
public class DateUtils {
/**
* 當前時區的時間偏移的毫秒數
*/
static public final long ZONE_OFFSET_MillisSecond = Calendar.getInstance()
.get(Calendar.ZONE_OFFSET);

/**
* 一天內的毫秒數
* 24 * 60 * 60 * 1000
*/
static public final long MillisSecondOfDay = 24 * 60 * 60 * 1000;

/**
* 1小時內的毫秒數
* 60 * 60 * 1000
*/
static public final long MillisSecondOfHour = 60 * 60 * 1000;

/**
* 1分鐘內的毫秒數
* 60 * 1000
*/
static public final long MillisSecondOfMinutes = 60 * 1000;

/**
* 1秒內的毫秒數
*/
static public final long MillisSecondOfSecond = 1000;

/**
* 短日期格式 yyyyMMdd
*/
public static final String DATE_YYYYMMDD = "yyyyMMdd";

/**
* 短日期格式 yyyy-MM-dd
*/
public static final String DATE_FORMAT = "yyyy-MM-dd";

/**
* 長日期格式 yyyy-MM-dd HH:mm
*/
public static final String DATETIME_FORMAT_S = "yyyy-MM-dd HH:mm";

/**
* 長日期格式 yyyy-MM-dd HH:mm:ss
*/
public static final String DATETIME_FORMAT = "yyyy-MM-dd HH:mm:ss";

private static short[] maxday = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30,
31, 30, 31 };

static String checkDate(short y, short m, short d) {
if (y < 0 || y > 9999)
return "year must beteen 0 and 9999";
if (m > 12 || m < 0)
return "month must between 1 and 12 ";
if (m == 2 && (y % 200 == 0 || (y % 4 == 0 && y % 100 != 0))) {
if (d > 29 || d < 0)
return "day must between 1 and 29 ";
} else {
if (d > maxday[m] || d < 0)
return "day must between 1 and " + maxday[m];
}
return null;
}

/**
* 到一天的最開始時間。 即消除時間的部分到一天 的 00:00:00.000
*
* @param date
*/
public static void gotoDateBeginTime(Date date) {
long t = date.getTime();
date.setTime(t - (t + ZONE_OFFSET_MillisSecond) % MillisSecondOfDay);
}

/**
* 返回一天的最開始時間。 即消除時間的部分, 到一天 的 00:00:00.000
*
* @param date
* @return 返回一天的最開始時間。 即消除時間的部分, 到一天 的 00:00:00.000
*/
public static Date getDateBeginTime(Date date) {
Date result = (Date) date.clone();
gotoDateBeginTime(result);
return result;
}

/**
* 到一天的結束時間。 即到一天 的 23.59.59.999
*
* @param date
*/
public static void gotoDateEndTime(Date date) {
long t = date.getTime();
t = t - (t + ZONE_OFFSET_MillisSecond) % MillisSecondOfDay
+ MillisSecondOfDay - 1;
date.setTime(t);
}

/**
* 返回一天的結束時間。 即到一天 的 23.59.59.999
*
* @param date
* @return 返回一天的結束時間。 即到一天 的 23.59.59.999
*/
public static Date getDateEndTime(Date date) {
Date result = (Date) date.clone();
gotoDateEndTime(result);
return result;
}

/**
* @param value
* @return 返回對應的小時數
*/
public static final int getHour(Date value) {

return getHour(value.getTime());

}

public static final int getHour(long time) {
return (int) (((time + ZONE_OFFSET_MillisSecond) % MillisSecondOfDay) / MillisSecondOfHour);
}

/**
* @param value
* @return 返回對應的分鐘數
*/
public static int getMinutes(Date value) {
return getMinutes(value.getTime());
}

public static int getMinutes(long time) {
return (int) ((time % MillisSecondOfHour) / MillisSecondOfMinutes);
}

/**
* @param value
* @return 返回對應的秒數
*/
public static int getSecond(Date value) {
return getSecond(value.getTime());
}

/**
*
* @param time
* @return 返回對應的秒數
*
*/
public static int getSecond(long time) {
return (int) ((time % MillisSecondOfMinutes) / MillisSecondOfSecond);
}

/**
* @param value
* @return 毫秒數 (.nnn)
*/
public static int getMSecond(Date value) {
long t = value.getTime();
return (int) (t % MillisSecondOfSecond);
}

public static Timestamp now() {
return new Timestamp(System.currentTimeMillis());
}

/**
* 添加小時數
*
* @param time
* @param hours
* @return 添加小時數
*/
public static long addHour(long time, int hours) {
return time + hours * MillisSecondOfHour;
}

/**
* @param time
* @param minutes
* @return 添加分鐘數
*/
public static long addMinutes(long time, int minutes) {
return time + minutes * MillisSecondOfHour;
}

/**
* @param time1
* @param time2
* @return time1 - time2 的小時數
*/
public static long diffHour(long time1, long time2) {
return (time1 - time2) / MillisSecondOfHour;
}

/**
* @param time1
* @param time2
* @return time1 - time2 的天數
*/
public static long diffDay(long time1, long time2) {
return (time1 - time2) / MillisSecondOfDay;
}

/**
* @param time1
* @param time2
* @return time1 - time2 的分鐘數
*/
public static long diffMinites(long time1, long time2) {
return (time1 - time2) / MillisSecondOfMinutes;
}

/**
* 把字符日期格式轉化爲日期類型
*
* @param s
* @return
*/
public static Date parserDate(String s) throws ParseException {
if (s == null || s.trim().length() == 0) {
return null;
}
s = s.trim();
if (s.length() == 10) // YYYY-MM-DD
{
return parseDate(s, DATE_FORMAT);
} else if (s.length() == 8) // YYYYMMDD
{
return parseDate(s, DATE_YYYYMMDD);
} else if (s.length() == 16) // YYYY-MM-DD HH:MM
{
return parseDate(s, DATETIME_FORMAT_S);
} else if (s.length() == 19) // YYYY-MM-DD HH:MM.SS
{
return parseDate(s, DATETIME_FORMAT);
} else if (s.length() > 19) // YYYY-MM-DD HH:MM.SS.fffffff
{
return Timestamp.valueOf(s);
} else {
throw new ParseException("Unknow Date Format of " + s, 0);
}
}

/**
* 把字符日期按要求的格式轉化爲日期類型
*
* @param str
* @param parsePatterns
* @return
*/
public static Date parseDate(String str, String parsePattern){
return parseDate(str, new String[]{parsePattern});
}

/**
* 把字符日期按要求的格式轉化爲日期類型
*
* @param str
* @param parsePatterns
* @return
*/
public static Date parseDate(String str, String parsePatterns[]) {
if (str == null || parsePatterns == null)
throw new IllegalArgumentException(
"Date and Patterns must not be null");
SimpleDateFormat parser = null;
ParsePosition pos = new ParsePosition(0);
for (int i = 0; i < parsePatterns.length; i++) {
if (i == 0)
parser = new SimpleDateFormat(parsePatterns[0]);
else
parser.applyPattern(parsePatterns[i]);
pos.setIndex(0);
Date date = parser.parse(str, pos);
if (date != null && pos.getIndex() == str.length())
return date;
}

throw new RuntimeException("Unable to parse the date: " + str);
}

/**
* 把日期格式成指定格式的日期字符串
*
* @param pattern
* @return
*/
public static String format(Date date, String pattern) {
return DateFormatUtils.format(date, pattern);
}

/**
* 把當前日期格式成指定格式的日期字符串
*
* @param pattern
* @return
*/
public static String getCurrent(String pattern) {
return format(new Date(), pattern);
}

/**
* 把距1900-01-01的天數轉化爲日期
*
* @param s
* @return
* @throws ParseException
*/
public static Date changNumToDate(String s) throws ParseException {
String rtn = "1900-01-01";
SimpleDateFormat fm = new SimpleDateFormat("yyyy-mm-dd");
Date date= fm.parse(rtn);
long l1 = date.getTime();
l1 = l1+((Long.parseLong(s)-2)*24*3600*1000);
date.setTime(l1);
return date;
}

/**
* 增加或減少天數
*
* @param codeName
* @return
*/
public static String addDay(String dateStr, int addDays) {
Date compDate = parseDate(dateStr, DATE_YYYYMMDD);

Calendar cc = Calendar.getInstance();
cc.setTime(compDate);
cc.add(Calendar.DAY_OF_MONTH, addDays);

return format(cc.getTime(), DATE_YYYYMMDD);
}

public static void main(String[] args) {
System.out.println(getCurrent(DATETIME_FORMAT));
}

}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章