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));
}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值