java中的时间计算和格式处理方法

//一天秒数
public final static long MILLISECONDS_OF_DAY = 1000 * 60 * 60 * 24;
public final static long MINUTES_OF_DAY = 1000 * 60 * 60 * 24;
public final static int days[] = {31,29,31,30,31,30,31,31,30,31,30,31};

public final static int WEEK_LENGTH=7;
public final static int YEAR_LENGTH=12;
public final static int NULL_DATE = 19000101;
public final static String NULLDATE="1900-01-01";

public final static int DATETIME_LENGTH = 14;
public final static int DATE_LENGTH = 8;
public final static int TIME_LENGTH = 6;
/**
* 将YYYY-MM-DD HH:NN:SS类型的字符串转化为Date类型
* @param date
* @return Date类型
* @throws ParseException 
*/
public static Date parse(String date) throws ParseException {
boolean isDate = date.indexOf('-') > 0;
boolean isTime = date.indexOf(':') > 0;


if (isDate && isTime) { //日期时间
SimpleDateFormat dateTimeF = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
return dateTimeF.parse(date);
} else if (isDate) {
SimpleDateFormat dateF = new SimpleDateFormat("yyyy-MM-dd");
return dateF.parse(date);
} else if (isTime) {
SimpleDateFormat timeF = new SimpleDateFormat("HH:mm:ss");
return timeF.parse(date);
}
return new Date(0);
}
/**
* 将YYYY-MM-DD HH:NN:SS.SSS类型的字符串转化为Date类型
* @param date
* @return Date类型
* @throws ParseException 
*/
public static Date parseMinSec(String date) throws ParseException {
boolean isDate = date.indexOf('-') > 0;
boolean isTime = date.indexOf(':') > 0;


if (isDate && isTime) { //日期时间
SimpleDateFormat dateTimeF = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
return dateTimeF.parse(date);
} else if (isDate) {
SimpleDateFormat dateF = new SimpleDateFormat("yyyy-MM-dd");
return dateF.parse(date);
} else if (isTime) {
SimpleDateFormat timeF = new SimpleDateFormat("HH:mm:ss.SSS");
return timeF.parse(date);
}
return new Date(0);
}
/**
* 转化为整型
* @param dateTime
* @return 整型的日期值
* @throws ParseException
*/
public static int toInteger(String dateTime)throws ParseException {
Calendar cal = Calendar.getInstance();
cal.setTime(parse(dateTime));
return cal.get(Calendar.YEAR) * 10000
+ (cal.get(Calendar.MONTH)+1) * 100 
+ cal.get(Calendar.DAY_OF_MONTH);
}

public static String mulFreq(String freq, int muls){
int num = Integer.parseInt(freq.substring(0, freq.length() - 1));
return Integer.toString(num) + freq.substring(freq.length() - 1, 1);
}

public static int toInteger(Date dateTime) {
Calendar cal = Calendar.getInstance();
cal.setTime(dateTime);
return cal.get(Calendar.YEAR) * 10000
+ (cal.get(Calendar.MONTH)+1) * 100 
+ cal.get(Calendar.DAY_OF_MONTH);
}
/**
* 返回某一频率下的两个日期间的次数
* @param start
* @param end
* @param freq
* @return int 次数
* @throws ParseException
*/
public static int indexOf(String start, String end, String freq) throws ParseException{
Date date1 = GenernalDate.parse(start);
Date date2 = GenernalDate.parse(end);
for(int i = 0;;i++) {
if( date1.getTime() >= date2.getTime() ) {
return i;
}
date1 = GenernalDate.add(date1, freq);
}
}
/**
* 返回两个日期相差年数
* @param start
* @param end
* @return 相差年数
* @throws ParseException
*/
public static int yearsOf(String start, String end)throws ParseException{
Calendar cal1 = Calendar.getInstance();
cal1.setTime(parse(start));
Calendar cal2 = Calendar.getInstance();
cal2.setTime(parse(end));
return cal2.get(Calendar.YEAR) - cal1.get(Calendar.YEAR) + 1;
}


/**
* 从日期时间字符串拆出日期
* @param dateTime
* @return 日期
* @throws ParseException
*/
public static String splitDate(String dateTime) throws ParseException {
return formatDate(parse(dateTime));
}


/**
* 从日期时间字符串拆出时间
* @param dateTime
* @return 时间
* @throws ParseException
*/
public static String splitTime(String dateTime) throws ParseException {
return formatTime(parse(dateTime));
}


/**
* 返回日期
* @param date
* @return Date
* @throws ParseException
*/
public static Date parseDate(String date) throws ParseException {
SimpleDateFormat dateF = new SimpleDateFormat("yyyy-MM-dd");
return dateF.parse(date);
}

public static Calendar parseCalendar(String date){
Calendar cal = Calendar.getInstance();
SimpleDateFormat dateF = new SimpleDateFormat("yyyy-MM-dd");
try{
cal.setTime(dateF.parse(date));
}catch(ParseException e){
throw new RuntimeException(e);
}
return cal;
}

/**
* 返回时间
* @param time
* @return Date 时间
* @throws ParseException
*/
public static Date parseTime(String time) throws ParseException {
SimpleDateFormat timeF = new SimpleDateFormat("HH:mm:ss");
return timeF.parse(time);
}


/**
* 两个日期相减,参数为String型
* @param startDate
* @param endDate
* @return int
*/
public static int offset(String startDate, String endDate) throws ParseException {
return offset(parse(startDate), parse(endDate));
}


/**
* 两个日期相减,参数为Date型
* @param startDate
* @param endDate
* @return int 日期差
*/
public static int offset(Date startDate, Date endDate) {
return (int) ((endDate.getTime() - startDate.getTime()) / MINUTES_OF_DAY);
}

/**
* 两个日期相减,参数为Date型
* @param startDate
* @param endDate
* @return int 日期差
*/
public static int offsetDay(Date startDate, Date endDate)throws ParseException {
  return offset(parse(formatDate(startDate)),parse(formatDate(endDate)));
}


/**
* 日期加天数
* @return 日期加天数后的Date日期
*/
public static Date addDays(Date date, int days) {
return new Date(date.getTime() + days * MINUTES_OF_DAY);
}


/**
* 日期加天数
* @param date
* @param days
* @return 日期加天数后的String日期
*/
public static String addDays(String date, int days) throws ParseException {
return formatDate(addDays(parse(date), days));
}


/**
* +月
* @param date 起始日期
* @param months 月数
* @return 日期加月数后的Date日期
* @throws ParseException
*/
public static Date addMonths(Date date, int months) {
Calendar cal = Calendar.getInstance();
cal.setTime(date);
cal.add(Calendar.MONTH, months);
return cal.getTime();
}


/**
* +月
* @param date 起始日期
* @param months 月数
* @return 日期加月数后的String日期
* @throws ParseException
*/
public static String addMonths(String date, int months) throws ParseException {
return formatDate(addMonths(parseDate(date), months));
}


/**
* +年
* @param date 起始日期
* @param years 年数
* @return 日期加年数后的Date日期
* @throws ParseException
*/
public static Date addYears(Date date, int years) {
Calendar cal = Calendar.getInstance();
cal.setTime(date);
cal.add(Calendar.YEAR, years);
return cal.getTime();

。。。。。。。。。。。。。。。。。

版权原因,完整文章,请参考如下:java中的时间计算和格式处理方法time

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值