在Java進行項目開發中相信都經常遇到處理日期和時間的問題,根據經驗和過往開發整理一下一些比較經常用到方法和代碼片段,更多可自行豐富修改,方便日後參考使用,嘿嘿,懶人計劃,硬核如下...
一,各代碼片段
//解析符合格式的日期字符串,返回日期類型
//日期格式: yyyy-MM-dd HH:mm:ss 或 yyyy/MM/dd HH:mm:ss
package com.xx.yy.zz.util;
import java.util.Date;
import java.text.SimpleDateFormat;
public static Date parseDate(String dateTime){
if (null == dateTime || dateTime.trim().isEmpty()){
return null;
}
SimpleDateFormat sdFromat1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
SimpleDateFormat sdFromat2 = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
if (dateTime.indexOf("/") > 0) {
return sdFromat2.parse(dateTime);
} else {
return sdFromat1.parse(dateTime);
}
}
//獲取日期的的年月日時分秒毫秒yyyyMMddHHmmssSSS字符串
//如果參數爲null則日期爲當前日期
package com.xx.yy.zz.util;
import java.util.Date;
import java.text.SimpleDateFormat;
public static String getStrDateYyyyMMddHHmmssSSS(Date date){
if (null == date) {
date = Calendar.getInstance().getTime();
}
SimpleDateFormat sdFromat = new SimpleDateFormat("yyyyMMddHHmmssSSS");
return sdFromat.format(date);
}
//獲取日期的的年月日時分秒yyyy-MM-dd HH:mm:ss字符串
//如果參數爲null則日期爲當前日期
package com.xx.yy.zz.util;
import java.util.Date;
import java.text.SimpleDateFormat;
public static String getStrDateYyyyMMddHHmmss(Date date){
if (null == date) {
date = Calendar.getInstance().getTime();
}
SimpleDateFormat sdFromat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
return sdFromat.format(date);
}
//獲取日期的的年月日yyyy-MM-dd字符串
//如果參數爲null則日期爲當前日期
package com.xx.yy.zz.util;
import java.util.Date;
import java.text.SimpleDateFormat;
public static String getStrDateYyyyMMdd(Date date){
if (null == date) {
date = Calendar.getInstance().getTime();
}
SimpleDateFormat sdFromat = new SimpleDateFormat("yyyy-MM-dd");
return sdFromat.format(date);
}
//判斷指定日期是不是當月的最後一天
//入參爲空就是當天,
//返回true=是月末最後一天,false=非月末最後一天
package com.xx.yy.zz.util;
import java.util.Date;
import java.util.Calendar;
public static boolean isLastDayOfMonth(Date date){
Calendar calendar = Calendar.getInstance();
if (null == date) {
calendar.setTime(new Date());
} else {
calendar.setTime(date);
}
calendar.set(Calendar.DATE,(calendar.get(Calendar.DATE))+1);
if (1 == calendar.get(Calendar.DAY_OF_MONTH)) {
return true;
} else {
return false;
}
}
//獲取指定日期所屬月的最後一天的日期和時間
//如果參數爲null則日期爲當前日期
package com.xx.yy.zz.util;
import java.util.Date;
import java.util.Calendar;
public static Date getLastDateOfMonth(Date date){
if (null == date){
date = Calendar.getInstance().getTime();
}
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.set(Calendar.DATE, 1);
//roll方法獲取最後一天,但時間跟當前日期的時間一樣
calendar.roll(Calendar.DATE, -1);
String tempDTime = getStrDateYyyyMMdd(calendar.getTime());
tempDTime = tempDTime + " 23:59:59";
return parseDate(tempDTime);
}
//判斷targetDate是否在起始和結束時間範圍(含)內
//返回, true=是在範圍內,false=不在範圍內
package com.xx.yy.zz.util;
import java.util.Date;
public static boolean isMiddleOfTwoDate(Date startDate, Date endDate, Date targetDate){
if (null == startDate || null == endDate || null == targetDate){
return false;
}
long startDateNum = startDate.getTime();
long endDateNum = endDate.getTime();
long targetDateNum = targetDate.getTime();
if(startDateNum <= targetDateNum && targetDateNum <= endDateNum){
return true;
}
return false;
}
//計算兩個日期之間間隔有多少天,
//注意:如果包含頭或尾的一天則自行加一,本方法僅計算兩個日期中間的天數
package com.xx.yy.zz.util;
import java.text.DecimalFormat;
import java.text.Format;
import java.util.Calendar;
import java.util.Date;
public static long getDaysOfTwoDate(Date startDate, Date endDate){
if (null == startDate || null == endDate){
return 0;
}
Calendar calendar = Calendar.getInstance();
calendar.setTime(startDate);
long num1 = calendar.getTimeInMillis();
calendar.setTime(endDate);
long num2 = calendar.getTimeInMillis();
long diff = Math.abs(num2 - num1);
double days = (diff / 1000) / (1*24*60*60);
//僅僅計算整數部分
DecimalFormat numFormat = new DecimalFormat("#");
return Long.valueOf(numFormat.format(days));
}
//序列日期相關的Bean
package com.xx.yy.zz.model;
import java.util.Date;
public class SnDateVo {
/** 月或周的序列 */
private Integer dateSn;
/** 月或周的序列名稱 */
private String dateSnName;
/** 開始日期 */
private Date startDate;
/** 結束日期 */
private Date endDate;
/** 開始和結束日期內的天數 */
private Integer dayNumber;
// ... 構造函數/setter/getter省略...
}
//計算並返回兩個日期之間各個月份的數據,
//注意: 各個月從1日的00:00:00到本月最後一天的23:59:59
//原理根據時間按秒數不斷加一使時間向前滾動來計算
package com.xx.yy.zz.util;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import com.xx.yy.zz.model.SnDateVo;
public static List<SnDateVo> getMonthData(Date startDate, Date endDate){
List<SnDateVo> monthDatas = new ArrayList<SnDateVo>();
if (null == startDate || null == endDate){
return monthDatas;
}
//第一個月
String monthSn = "X月份";
Calendar calendar = Calendar.getInstance();
calendar.setTime(startDate);
int startMonth = calendar.get(Calendar.MONTH);
SnDateVo dateVo = new SnDateVo();
dateVo.setDateSn(startMonth + 1); //用戶看的需要加1符合實際
dateVo.setDateSnName(monthSn.replace("X",(startMonth + 1)+"");
dateVo.setStartDate(startDate);
Date tempDate = getLastDateOfMonth(startDate);
boolean isEnd = false;
if (tempDate.before(endDate)) {
isEnd = false;
dateVo.setEndDate(tempDate);
calendar.setTime(tempDate);
} else {
isEnd = true;
dateVo.setEndDate(endDate);
}
int tempDays = Integer.valueOf(getDaysOfTwoDate(dateVo.getStartDate(),dateVo.getEndDate()) + "") + 1;
dateVo.setDayNumber(tempDays);
monthDatas.add(dateVo);
//第二個以及之後的月份
if (isEnd){
return monthDatas;
}
int tempSn = 0;
int sn = 1;
while(true){
SnDateVo xDateVo = new SnDateVo();
tempSn = startMonth + 1 + sn;
xDateVo.setDateSn(startMonth + 1);
xDateVo.setDateSnName(monthSn.replace("X",tempSn+"");
calendar.add(Calendar.SECOND, 1); //時間秒數加一
tempDate = getLastDateOfMonth(calendar.getTime());
if (tempDate.before(endDate)) {
sn = sn +1;
xDateVo.setEndDate(tempDate);
tempDays = Integer.valueOf(getDaysOfTwoDate(xDateVo.getStartDate(),xDateVo.getEndDate()) + "") + 1;
xDateVo.setDayNumber(tempDays);
monthDatas.add(xDateVo);
calendar.setTime(tempDate); //本次最後的日期設置到日期對象中
} else {
xDateVo.setEndDate(endDate);
tempDays = Integer.valueOf(getDaysOfTwoDate(xDateVo.getStartDate(),xDateVo.getEndDate()) + "") + 1;
xDateVo.setDayNumber(tempDays);
monthDatas.add(xDateVo);
break;
}
}
return monthDatas;
}
//計算並返回兩個日期之間各個周的數據,
//注意: 按我國用戶習慣各個周從週一的00:00:00到本週日的23:59:59結束
//原理根據時間按秒數不斷加一使時間向前滾動來計算
package com.xx.yy.zz.util;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import com.xx.yy.zz.model.SnDateVo;
public static List<SnDateVo> getWeekData(Date startDate, Date endDate){
List<SnDateVo> weekDatas = new ArrayList<SnDateVo>();
if (null == startDate || null == endDate){
return weekDatas;
}
//第一個周
String weekSn = "第X周";
int startWeek = 1;
SnDateVo dateVo = new SnDateVo();
dateVo.setDateSn(startWeek);
dateVo.setDateSnName(weekSn.replace("X",startWeek+"");
dateVo.setStartDate(startDate);
Calendar calendar = Calendar.getInstance();
calendar.setTime(startDate);
Date tempDate = calendar.getTime();
//注意Java中定義一週從週日的0時開始,對應數值1,週六的23:59:59爲最後一天;
//我國用戶習慣定義一週從週一的0時開始,對應數值2,週日的23:59:59爲最後一天;
boolean isEnd = false;
int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK);
if (1 == dayOfWeek) {
tempDate = parseDate(getStrDateYyyyMMdd(calendar.getTime()) + " 23:59:59");
} else {
calendar.add(Calendar.DAY_OF_WEEK,(7 - dayOfWeek + 1));
tempDate = parseDate(getStrDateYyyyMMdd(calendar.getTime()) + " 23:59:59");
}
if (tempDate.before(endDate)) {
isEnd = false;
dateVo.setEndDate(tempDate);
calendar.setTime(tempDate);
} else {
isEnd = true;
dateVo.setEndDate(endDate);
}
int tempDays = Integer.valueOf(getDaysOfTwoDate(dateVo.getStartDate(),dateVo.getEndDate()) + "") + 1;
dateVo.setDayNumber(tempDays);
weekDatas.add(dateVo);
//第二個以及之後的周
if (isEnd){
return weekDatas;
}
int tempDayOfWeek = 0;
int tempSn = 0;
int sn = 1;
while(true){
SnDateVo xDateVo = new SnDateVo();
tempSn = startWeek + sn;
xDateVo.setDateSn(tempSn);
xDateVo.setDateSnName(weekSn.replace("X",tempSn+"");
calendar.add(Calendar.SECOND, 1); //時間秒數加一
xDateVo.setStartDate(calendar.getTime());
tempDayOfWeek = calendar.get(Calendar.DAY_OF_WEEK);
calendar.add(Calendar.DAY_OF_WEEK,(7 - tempDayOfWeek + 1));
tempDate = parseDate(getStrDateYyyyMMdd(calendar.getTime()) + " 23:59:59");
if (tempDate.before(endDate)) {
sn = sn + 1;
xDateVo.setEndDate(tempDate);
tempDays = Integer.valueOf(getDaysOfTwoDate(xDateVo.getStartDate(),xDateVo.getEndDate()) + "") + 1;
xDateVo.setDayNumber(tempDays);
weekDatas.add(xDateVo);
calendar.setTime(tempDate); //本次最後的日期設置到日期對象中
} else {
xDateVo.setEndDate(endDate);
tempDays = Integer.valueOf(getDaysOfTwoDate(xDateVo.getStartDate(),xDateVo.getEndDate()) + "") + 1;
xDateVo.setDayNumber(tempDays);
weekDatas.add(xDateVo);
break;
}
}
return weekDatas;
}
二,後記,
基本我們常用到的,當然還有其他用到的方法,後續有空再補充... 歡迎拍磚討論...