java Date 系统时间处理类
更多源码进入:在一起 技术分享
class="java">
package com.xfx.bee.utils;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
/**
* 系统时间处理类
* [url=http://www.zyiqibook.com]在一起 学习交流分享网 功能源码分享[/url]
*
*/
public class DateUtils {
private static final String[] weekDaysName = { "星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六" };
/**
* 验证时间是不是在一天之内
*/
public static boolean validInOneDate(Date dates) {
Calendar c = Calendar.getInstance();
c.set(Calendar.DAY_OF_MONTH,c.get(Calendar.DAY_OF_MONTH) - 1);
return c.getTime().before(dates);
}
/**
* 验证时间是不是在两天之内
*/
public static boolean validInTwoDate(Date dates) {
Calendar c = Calendar.getInstance();
c.set(Calendar.DAY_OF_MONTH,c.get(Calendar.DAY_OF_MONTH) - 2);
return c.getTime().before(dates);
}
/**
* string 转换为 Date
*/
public static Date convertStringToDate(String strDate)throws ParseException {
Date aDate = null;
SimpleDateFormat df;
df = new SimpleDateFormat("yyyy-MM-dd HH:mm");
try {
aDate = df.parse(strDate);
} catch (ParseException pe) {
throw new ParseException(pe.getMessage(), pe.getErrorOffset());
}
return aDate;
}
/**
* 页面查询条件格式化起始日期
*/
public static Date formatBeginDate(Date beginDate){
Calendar beginCal = Calendar.getInstance();
beginCal.setTime(beginDate);
beginCal.set(Calendar.HOUR_OF_DAY, 0);
beginCal.set(Calendar.MINUTE, 0);
beginCal.set(Calendar.SECOND, 0);
// 查询开始时间
Date begin = beginCal.getTime();
return begin;
}
/**
* 页面查询条件格式化截止日期
*/
public static Date formatEndDate(Date endDate){
Calendar endCal = Calendar.getInstance();
Date nowtime = new Date();
if(endDate==null || nowtime.before(endDate)){
endCal.setTime(nowtime);
endCal.set(Calendar.HOUR_OF_DAY, 23);
endCal.set(Calendar.MINUTE, 59);
endCal.set(Calendar.SECOND, 59);
}else{
endCal.setTime(endDate);
endCal.set(Calendar.HOUR_OF_DAY, 23);
endCal.set(Calendar.MINUTE, 59);
endCal.set(Calendar.SECOND, 59);
}
Date end = endCal.getTime();
return end;
}
/**
* 根据日期获得星期
*/
public static String getWeekOfDate(Date date) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
int intWeek = calendar.get(Calendar.DAY_OF_WEEK) - 1;
return weekDaysName[intWeek];
}
/**
* 计算两个日期之间相差的天数
* @param smdate 较小的时间
* @param bdate 较大的时间
*/
public static String daysBetween(Date smdate,Date bdate) throws ParseException{
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
smdate=sdf.parse(sdf.format(smdate));
bdate=sdf.parse(sdf.format(bdate));
Calendar cal = Calendar.getInstance();
cal.setTime(smdate);
long time1 = cal.getTimeInMillis();
cal.setTime(bdate);
long time2 = cal.getTimeInMillis();
long between_days=(time2-time1)/(1000*3600*24);
return String.valueOf(between_days);
}
public static void main(String[] args) throws Exception {
String date1="2013-06-24 10:30:30"; //
String date2="2013-06-26 12:30:31";
java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
java.util.Date start = sdf.parse(date1);
java.util.Date end = sdf.parse(date2);
System.out.println(daysBetween(start,end));
java.util.Date end2 = sdf.parse("2013-09-23 12:30:31");
System.out.println(validInTwoDate(end2));
}
}