java中自己用到的一些時間處理類
1、獲取當前時間DateUtil.getCurrentTime();
2、
String gTime = newSimpleDateFormat("HH:mm:ss").format(new Date(Long.valueOf(time)));
Date ftime = newSimpleDateFormat("HH:mm:ss").parse(gTime);
3、判斷指定時間是否在當前時間之後
SimpleDateFormat sdf= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
sdf.parse(applyDTO.getJssj()).after(new Date())
1、 根據開始和結束時間獲取本週每一天的日期集合(比如開始時間2017-02-08週三結束時間2017-02-09週四最後返回值為 [null,null,2017-02-08,2017-02-09,null,null,null,] 對應週一到週日,沒有為null)
publicList<String> getThisWeek(String startTime,String endTime) throwsParseException{
//時間處理
DateFormatdf = new SimpleDateFormat("yyyy-MM-dd");
Calendarcalendar = Calendar.getInstance();
intday = 0;
DatestartDate = df.parse(startTime);
DateendDate = df.parse(endTime);
DatestartDateCopy = startDate;
DateendDateCopy = endDate;
//開始結束時間不足一週的,補足一週
calendar.setTime(startDate);
intstartNumber = calendar.get(Calendar.DAY_OF_WEEK);
if(startNumber!=2){
day= calendar.get(Calendar.DATE);
if(startNumber!=1){
calendar.set(Calendar.DATE,day-(startNumber-2));
}else{
calendar.set(Calendar.DATE,day-6);
}
startDate= calendar.getTime();
}
calendar.setTime(endDate);
intendNumber = calendar.get(Calendar.DAY_OF_WEEK);
if(endNumber!=1){
day= calendar.get(Calendar.DATE);
calendar.set(Calendar.DATE,day+(8-endNumber));
endDate= calendar.getTime();
}
//獲取週一的前一天
calendar.setTime(startDate);
day= calendar.get(Calendar.DATE);
calendar.set(Calendar.DATE,day - 1);
DateupDate = calendar.getTime();
//獲取週日的後一天
calendar.setTime(endDate);
day= calendar.get(Calendar.DATE);
calendar.set(Calendar.DATE,day + 1);
DatedownDate = calendar.getTime();
//迴圈獲取本週日期,被補足的時間應該為空
calendar.setTime(upDate);
day= calendar.get(Calendar.DATE);
calendar.set(Calendar.DATE,day + 1);
Datetime = calendar.getTime();
List<String>dateList = new ArrayList<String>();
while(time.before(downDate)) {
if(time.before(startDateCopy)||time.after(endDateCopy)){
dateList.add(null);
}else{
dateList.add(df.format(time));
}
day= calendar.get(Calendar.DATE);
calendar.set(Calendar.DATE,day + 1);
time= calendar.getTime();
}
returndateList;
}
2、根據一個時間獲取該週週一和週日日期
SimpleDateFormatsdf=new SimpleDateFormat("yyyy-MM-dd",Locale.CHINA);
if(StringUtils.isEmpty(startTime)){
startTime= sdf.format(new Date());
}
Map<String,String>param = new HashMap<String, String>();
//根據傳過來的日期或者今天時間獲得每週的開始時間和結束時間
Calendar cal=Calendar.getInstance();
cal.setTime(sdf.parse(startTime));
cal.setFirstDayOfWeek(Calendar.MONDAY);//設定一週的起始日
cal.set(Calendar.DAY_OF_WEEK,Calendar.MONDAY);
Stringkssj=sdf.format(cal.getTime());//當前周的週一日期
cal.set(Calendar.DAY_OF_WEEK,Calendar.SUNDAY);
Stringjssj=sdf.format(cal.getTime());//當前周的週日日期
param.put("userId",SecurityFacade.getCurUserId());
param.put("startTime",kssj);
param.put("overTime",jssj);
4、判斷兩個日期那個在前,哪個在後
nowDate.before(checkDate)
/**
* 判斷指定時間與當期日期的先後關係
* 返回true表示在當前日期之前
* @throws ParseException
*/
publicboolean checkTime(String time) throws ParseException{
DateFormatdf = new SimpleDateFormat("yyyy-MM-dd");
DatecheckDate = df.parse(time);
DatenowDate = new Date();
if(nowDate.before(checkDate)){
returnfalse;
}else{
returntrue;
}
}
7、獲取當前時間
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-ddHH:mm:ss");
String a = df.format(new Date());//將當前時間轉換為模板樣式的時間
df.parse(String)//將時間轉換成為日期
SimpleDateFormatsdf=new SimpleDateFormat("yyyy-MM-dd",Locale.CHINA);
Stringtime = get(request, "time");
if(StringUtils.isEmpty(time)){
time= sdf.format(new Date());
}
8、獲取時間:
前臺(頁面和js)獲取當前時間禁止使用var now = new Date();當前時間一律取後臺伺服器時間
9、時間顯示多.0
資料庫datetime型別,獲取到的時候前臺顯示後面
會多個2016-11-28 17:45.35.0,這個時候在該接收的DTO中
get方法裡面新增下面語句,進行擷取
if(inspectRecordDate.length()> 18){
returninspectRecordDate.substring(0, 19);
}else{
returninspectRecordDate;
}
12、根據開始時間和結束時間返回時間段內的時間集合
/**
*
*
*@param beginDate
*@param endDate
*@return List
*@throws ParseException
*/
privateList<String> getDatesBetweenTwoDate1(String beginDate, String endDate)throws ParseException {
List<String>lDate = new ArrayList<String>();
lDate.add(beginDate);
SimpleDateFormatsdf = new SimpleDateFormat("yyyy-MM-dd");
Calendarcal = Calendar.getInstance();
Datestart = sdf.parse(beginDate);
Dateend = sdf.parse(endDate);
cal.setTime(start);
booleanbContinue = true;
while(bContinue) {
cal.add(Calendar.DAY_OF_MONTH,1);
if(end.after(cal.getTime())) {
lDate.add(sdf.format(cal.getTime()));
} else{
break;
}
}
if(!beginDate.equals(endDate)){
lDate.add(endDate);
}
returnlDate;
}
13、獲取當前日期的下一天
publicString getNextDate(String dateStr){
SimpleDateFormatdf = new SimpleDateFormat("yyyy-MM-dd");
Stringstr = "";
try {
Datedate = df.parse(dateStr);
Calendarcalendar = new GregorianCalendar();
calendar.setTime(date);
calendar.add(calendar.DATE,1);
date= calendar.getTime();
str =df.format(date);
} catch(ParseException e) {
e.printStackTrace();
}
returnstr;
}
/**
14、計算兩個日期之間相差的天數
*
* @param begindate
* 較小的時間
* @param enddate
* 較大的時間
* @return 相差天數
* @throws Exception
*/
publicint daysBetween(String begindate, String enddate) throws Exception {
SimpleDateFormatsdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Datesmdate = sdf.parse(begindate);
Datebdate = sdf.parse(enddate);
Calendarcal = Calendar.getInstance();
cal.setTime(smdate);
longtime1 = cal.getTimeInMillis();
cal.setTime(bdate);
longtime2 = cal.getTimeInMillis() + 1000;
longbetween_days = (time2 - time1) / (1000 * 3600 * 24);
returnInteger.parseInt(String.valueOf(between_days));
}
/**
15、計算兩個日期之間相差的秒數
*
* @param kssj
* 較小的時間
* @param jssj
* 較大的時間
* @return 相差秒數
* @throws Exception
*/
publiclong secondsBetween(String kssj, String jssj) throws Exception {
SimpleDateFormatsdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Datesmdate = sdf.parse(kssj);
Datebdate = sdf.parse(jssj);
Calendarcal = Calendar.getInstance();
cal.setTime(smdate);
longtime1 = cal.getTimeInMillis();
cal.setTime(bdate);
longtime2 = cal.getTimeInMillis() + 1000;
return(time2 - time1);
}
16、計算當前日期是周幾
public static int dayForWeek(String pTime) throws Exception {
format = new SimpleDateFormat("yyyy-MM-dd");
Calendar c = Calendar.getInstance();
c.setTime(format.parse(pTime));
int dayForWeek = 0;
if(c.get(Calendar.DAY_OF_WEEK) == 1){
dayForWeek = 7;
}else{
dayForWeek = c.get(Calendar.DAY_OF_WEEK) - 1;
}
return dayForWeek;
}
17、Date1.before(Date2)Date1.after(Date2),
Date1.after(Date2),當Date1大於Date2時,返回TRUE,當小於等於時,返回false;
Date1.before(Date2),當Date1小於Date2時,返回TRUE,當大於等於時,返回false;
如果業務資料存在相等的時候,而且相等時也需要做相應的業務判斷或處理時,請注意。
如果有這樣的需求,在某個日期內的業務check,那麼你需要使用:!Date1.after(Date2)
18、判斷time是否在from,to之內
/**
* 判斷time是否在from,to之內
* 判斷某個時間是否是在條件的起始時間與結束時間之內
* @param time 指定日期
* @param from 開始日期
* @param to 結束日期
* @return
*/
public boolean belongCalendar(Date time, Datefrom, Date to) {
Calendar date = Calendar.getInstance();
date.setTime(time);
Calendar after =Calendar.getInstance();
after.setTime(from);
Calendar before =Calendar.getInstance();
before.setTime(to);
if (date.after(after) &&date.before(before)) {
return true;
} else {
return false;
}
}