首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

java Date时间格式工具类DateUtil

每日英文

Whatever you are facing today, remember to give yourself some credit for making it this far. You are stronger than you know.

无论你今天要面对什么,既然走到了这一步,就奋 斗下去,给自己一些肯定,你比自己想象中要坚强。

卯卯有话说

人生须有四得,既沉得住气,变得了脸,弯得下腰,抬得起头。人生须有四种经历,耐得住寂寞,经得起考验,承受了压力,面对了孤独。

正文

importjava.text.ParseException;

importjava.text.ParsePosition;

importjava.text.SimpleDateFormat;

importjava.util.Calendar;

importjava.util.Date;

importjava.util.GregorianCalendar;

importorg.apache.commons.lang.time.DateFormatUtils;

/**

* 所有注释以2018-05-03日期为准

*/

public classDateUtils{

public static finalStringymdhms="yyyy-MM-dd HH:mm:ss";

public static finalStringymd="yyyy-MM-dd";

public static finalStringyymmdd="yy-MM-dd";

public static finalStringmd="MM-dd";

public static finalStringymdhm="yyyy-MM-dd HH:mm";

public static finalStringmdhm="MM-dd HH:mm";

privateDateUtil() {

}

/**

* 获得年月日 格式:2018-05-03

*@paramdate

*@return

*/

public staticStringgetYmd(Datedate) {

returndate ==null?"":DateFormatUtils.format(date,"yyyy-MM-dd");

}

/**

* 获得年月日 格式:18-05-03

*@paramdate

*@return

*/

public staticStringgetYymmdd(Datedate) {

returndate ==null?"":DateFormatUtils.format(date,"yy-MM-dd");

}

/**

* 获得月日 格式:05-03

*@paramdate

*@return

*/

public staticStringgetMd(Datedate) {

returndate ==null?"":DateFormatUtils.format(date,"MM-dd");

}

/**

* 获得年月日 时分秒 格式:2018-05-03 14:32:36

*@paramdate

*@return

*/

public staticStringgetYmhmsd(Datedate) {

returndate ==null?"":DateFormatUtils.format(date,"yyyy-MM-dd HH:mm:ss");

}

/**

* 获得年月日 时分 格式:2018-05-03 14:32

*@paramdate

*@return

*/

public staticStringgetYmdhm(Datedate) {

returndate ==null?"":DateFormatUtils.format(date,"yyyy-MM-dd HH:mm");

}

/**

* 获得月日 时分 格式:05-03 14:32

*@paramdate

*@return

*/

public staticStringgetMdhm(Datedate) {

returndate ==null?"":DateFormatUtils.format(date,"MM-dd HH:mm");

}

/**

* 转换yyyy-MM-dd格式的字符串成Date格式

*@paramdate

*@return

*/

public staticDateparseYmd(Stringdate) {

SimpleDateFormat format=newSimpleDateFormat("yyyy-MM-dd");

ParsePosition pos=newParsePosition();

returnformat.parse(date,pos);

}

/**

* 转换yyyy-MM-dd HH:mm格式的字符串成Date格式

*@paramdate

*@return

*/

public staticDateparseYmdhm(Stringdate) {

SimpleDateFormat format=newSimpleDateFormat("yyyy-MM-dd HH:mm");

ParsePosition pos=newParsePosition();

returnformat.parse(date,pos);

}

/**

* 转换MM-dd HH:mm格式的字符串成Date格式

*@paramdate

*@return

*/

public staticDateparsemdhm(Stringdate) {

SimpleDateFormat format=newSimpleDateFormat("MM-dd HH:mm");

ParsePosition pos=newParsePosition();

returnformat.parse(date,pos);

}

/**

* 转换yyyy-MM-dd HH:mm:ss格式的字符串成Date格式

*@paramdate

*@return

*/

public staticDateparseYmdhms(Stringdate) {

SimpleDateFormat format=newSimpleDateFormat("yyyy-MM-dd HH:mm:ss");

ParsePosition pos=newParsePosition();

returnformat.parse(date,pos);

}

/**

* 格式化Date类型为格式yyyy-MM-dd HH:mm:ss的字符串

*@paramdate

*@return

*/

public staticStringformat(Datedate) {

try{

returnDateFormatUtils.format(date,"yyyy-MM-dd HH:mm:ss");

}catch(Exceptionvar2) {

return null;

}

}

/**

* 格式化Date类型为pattern格式的字符串

*@paramdate

*@return

*/

public staticStringformat(Datedate,Stringpattern) {

try{

returnDateFormatUtils.format(date,pattern);

}catch(Exceptionvar3) {

return null;

}

}

/**

* 指定格式的字符串日期进行累加年月日 例如:GetSysDate("yyyy-MM-dd","1990-01-01",1,2,3) 结果为 1991-03-04

*@paramformatStrDate的字符串是什么格式

*@paramStrDate时间字符串

*@paramyear年

*@parammonth月

*@paramday日

*@return

*/

public staticStringGetSysDate(Stringformat,StringStrDate,intyear,intmonth,intday) {

Calendar cal=Calendar.getInstance();

SimpleDateFormat sFmt=newSimpleDateFormat(format);

cal.setTime(sFmt.parse(StrDate,newParsePosition()));

if(day !=) {

cal.add(5,day);

}

if(month !=) {

cal.add(2,month);

}

if(year !=) {

cal.add(1,year);

}

returnsFmt.format(cal.getTime());

}

/**

* 获得两个时间字符串之间的天数差 时间字符串的格式为yyyy-MM-dd

*@parambegin

*@paramend

*@return

*/

public static longgetInterval(Stringbegin,Stringend) {

try{

Date e=parse(begin,"yyyy-MM-dd");

Date endTime=parse(end,"yyyy-MM-dd");

longday=0L;

day= (e.getTime() -endTime.getTime()) /86400000L;

returnday;

}catch(Exceptionvar6) {

throw newRuntimeException(var6);

}

}

/**

* 获得两个时间之间的天数差

*@parambeginTime

*@paramendTime

*@return

*/

public static longgetInterval(DatebeginTime,DateendTime) {

try{

longe=0L;

e= (beginTime.getTime() - endTime.getTime()) /86400000L;

returne;

}catch(Exceptionvar4) {

throw newRuntimeException(var4);

}

}

/**

* 根据秒进行相加差值并返回 比如 System.out.println(DateUtil.getDelay("2018-05-03",86400,"yyyy-MM-dd",2)); 结果:2018-05-05

*@paramnow

*@paramdelay

*@parampattern

*@paramtime

*@return

*/

public staticStringgetDelay(Stringnow,Integerdelay,Stringpattern,Integertime) {

String delayDate=null;

try{

Date e=parse(now,pattern);

longdelayTime=e.getTime() /1000L+ (long)(delay.intValue() * time.intValue());

e.setTime(delayTime*1000L);

delayDate=DateFormatUtils.format(e,pattern);

returndelayDate;

}catch(Exceptionvar8) {

return null;

}

}

/**

* 获得上一个月的第一天 例如:DateUtil.getPreMonthFirst("2018-05-03","yyyy-MM-dd") 结果:2018-04-01

*@paramnow

*@parampattern

*@return

*/

public staticStringgetPreMonthFirst(Stringnow,Stringpattern) {

Calendar c=Calendar.getInstance();

c.setTime(parse(now,pattern));

c.set(5,1);

c.add(2,-1);

returnDateFormatUtils.format(c,"yyyy-MM-dd");

}

/**

* 给目标时间添加天数 例如:DateUtil.getDelayDay("2018-05-03 15:01",5,"yyyy-MM-dd HH:mm") 结果:2018-05-08 15:01

*@paramnow

*@paramdelay

*@parampattern

*@return

*/

public staticStringgetDelayDay(Stringnow,Integerdelay,Stringpattern) {

returngetDelay(now,delay,pattern,Integer.valueOf(86400));

}

/**

* 给目标时间添加小时数 例如:DateUtil.getDelayHour("2018-05-03 15:01",5,"yyyy-MM-dd HH:mm") 结果:2018-05-03 20:01

*@paramnow

*@paramdelay

*@parampattern

*@return

*/

public staticStringgetDelayHour(Stringnow,Integerdelay,Stringpattern) {

returngetDelay(now,delay,pattern,Integer.valueOf(3600));

}

/**

* 给目标时间添加分钟数 例如:DateUtil.getDelayMinute("2018-05-03 15:01",5,"yyyy-MM-dd HH:mm") 结果:2018-05-03 15:06

*@paramnow

*@paramdelay

*@parampattern

*@return

*/

public staticStringgetDelayMinute(Stringnow,Integerdelay,Stringpattern) {

returngetDelay(now,delay,pattern,Integer.valueOf(60));

}

/**

* 根据pattern的格式来格式化date

*@paramdate

*@parampattern

*@return

*/

public staticDateparse(Stringdate,Stringpattern) {

SimpleDateFormat format=newSimpleDateFormat(pattern);

ParsePosition pos=newParsePosition();

returnformat.parse(date,pos);

}

/**

* 格式化Date为yyyy-MM-dd

*@paramdate

*@return

*/

public staticDateparse(Stringdate) {

SimpleDateFormat format=newSimpleDateFormat("yyyy-MM-dd");

ParsePosition pos=newParsePosition();

returnformat.parse(date,pos);

}

/**

* 获得今天星期几 例如:DateUtil.getWeek(new Date()) 结果:星期四

*@paramdate

*@return

*/

public staticStringgetWeek(Datedate) {

Calendar c=Calendar.getInstance();

c.setTime(date);

returnDateFormatUtils.format(c,"EEEE");

}

/**

* 格式为yyyy-MM-dd的时间字符串添加天数 例如:DateUtil.getAddCountDay(1,"2018-05-03") 结果:2018-05-04

*@paramcount

*@paramday

*@return

*/

public staticStringgetAddCountDay(intcount,Stringday) {

SimpleDateFormat simpleDate2=newSimpleDateFormat("yyyy-MM-dd");

simpleDate2.setLenient(false);

try{

Date myDate=simpleDate2.parse(day);

Long e=Long.valueOf(myDate.getTime() /1000L+ (long)(86400* count));

myDate.setTime(e.longValue() *1000L);

returnsimpleDate2.format(myDate);

}catch(ParseExceptionvar5) {

returnday;

}

}

/**

* 时间添加天数 例如:DateUtil.getAddCountDay(1,new Date()) 结果:Fri May 04 15:06:55 CST 2018

*@paramcount

*@paramdate

*@return

*/

public staticDategetAddCountDay(intcount,Datedate) {

Long myTime=Long.valueOf(date.getTime() /1000L+ (long)(86400* count));

date.setTime(myTime.longValue() *1000L);

returndate;

}

/**

* 对比两个时间的时间差 例如:DateUtil.contrastStringDate("2018-05-03","2018-05-04") 结果:1

*@paramdate1

*@paramdate2

*@return

*/

public static intcontrastStringDate(Stringdate1,Stringdate2) {

intday=;

SimpleDateFormat sf=newSimpleDateFormat("yyyy-MM-dd");

newGregorianCalendar();

newGregorianCalendar();

newDate();

newDate();

try{

Date xxx1=sf.parse(date1);

Date xxx2=sf.parse(date2);

day= (int)((xxx2.getTime() -xxx1.getTime()) /3600L/24L/1000L);

}catch(ParseExceptionvar9) {

}

returnday;

}

/**

* 计算两个时间点的分钟差值

*@paramstart

*@paramend

*@return

*/

public static longhasMinute(Datestart,Dateend) {

try{

System.out.println(start +"======="+ end);

longe= end.getTime() - start.getTime();

System.out.println(end.getTime() +"========"+ start.getTime());

e/=60000L;

returne;

}catch(Exceptionvar4) {

return0L;

}

}

/**

* 获得这个月最后一天的日期 例如:DateUtil.getLastDayOfMonth("2018-05-03") 结果:2018-05-31

*@paramdate

*@return

*/

public staticStringgetLastDayOfMonth(Stringdate) {

String[]ss= date.split("-");

Calendar cal=Calendar.getInstance();

cal.set(1,Integer.valueOf(ss[]).intValue());

cal.set(2,Integer.valueOf(ss[1]).intValue() -1);

intmaxDay=cal.getActualMaximum(5);

returnss[] +"-"+ss[1] +"-"+maxDay;

}

/**

* 不知道这是啥玩意 有待研究 哈哈哈

*@paramyear

*@paramweek

*@parammonth

*@return

*/

public staticStringgetDateByWeek(intyear,intweek,intmonth) {

String ret="";

Calendar c=Calendar.getInstance();

c.set(year,,1);

intday_week=c.get(7);//星期几

c.add(5,7* week - (day_week-1));//添加这么多天

System.out.println(c.get(2));//第几月

System.out.println(c.get(7));//星期几

System.out.println(c.get(5));//日期

if(month -c.get(2) !=1) {

ret=ret+"1-";

c.add(5,6);

ret=ret+c.get(5);

}else{

intsun_week=c.get(5);

ret=ret+sun_week+"-";

c.add(5,6);

intsat_week=c.get(5);

if(sat_week>sun_week) {

ret=ret+sat_week;

}else{

ret=ret+ (6-sat_week+sun_week);

}

}

returnret;

}

/**

* 获得时间一与时间二直接的月差值

*@paramdate1

*@paramdate2

*@return

*/

public static intgetMonthInterval(Stringdate1,Stringdate2) {

try{

SimpleDateFormat e=newSimpleDateFormat("yyyy-MM");

Calendar cal1=Calendar.getInstance();

cal1.setTime(e.parse(date1));

Calendar cal2=Calendar.getInstance();

cal2.setTime(e.parse(date2));

return(cal2.get(1) -cal1.get(1)) *12+ (cal2.get(2) -cal1.get(2));

}catch(Exceptionvar5) {

var5.printStackTrace();

return;

}

}

/**

* 给yyyy-MM格式的时间添加n个月

*@paramdate

*@paramn

*@return

*/

public staticStringgetAddCountMonth(Stringdate,intn) {

try{

SimpleDateFormat e=newSimpleDateFormat("yyyy-MM");

Calendar c=Calendar.getInstance();

c.setTime(e.parse(date));

c.add(2,n);

returne.format(c.getTime());

}catch(Exceptionvar4) {

var4.printStackTrace();

return null;

}

}

/**

* yyyy-MM-dd的时间字符串 获得今天星期几

*@paramdate

*@return

*/

public static intgetThisDateOfWeek(Stringdate) {

try{

SimpleDateFormat e=newSimpleDateFormat("yyyy-MM-dd");

Calendar cd=Calendar.getInstance();

cd.setTime(e.parse(date));

intdayOfWeek=cd.get(7) -1;

returndayOfWeek;

}catch(ParseExceptionvar4) {

var4.printStackTrace();

return-1;

}

}

/**

* 获得这个月的第一天的日期

*@paramdate

*@return

*/

public staticStringgetFirstDayOfMonth(Stringdate) {

String[]ss= date.split("-");

Calendar cal=Calendar.getInstance();

cal.set(1,Integer.valueOf(ss[]).intValue());

cal.set(2,Integer.valueOf(ss[1]).intValue() -1);

intminDay=cal.getActualMinimum(5);

returnminDay

}

/**

* 获得今年第一天的日期

*@return

*/

public staticDategetCurrYearFirst() {

Calendar currCal=Calendar.getInstance();

intcurrentYear=currCal.get(1);

returngetYearFirst(currentYear);

}

/**

* 获得指定年的第一天的日期

*@paramyear

*@return

*/

public staticDategetYearFirst(intyear) {

Calendar calendar=Calendar.getInstance();

calendar.clear();

calendar.set(1,year);

Date currYearFirst=calendar.getTime();

returncurrYearFirst;

}

/**

* 获得指定年的最后一天

*@paramyear

*@return

*/

public staticDategetCurrYearLast(intyear) {

Calendar calendar=Calendar.getInstance();

calendar.clear();

calendar.set(1,year);

calendar.roll(6,-1);

Date currYearLast=calendar.getTime();

returncurrYearLast;

}

/**

* 判断传入的值是不是今年的第一天

*@paramdate

*@return

*/

public static booleanjudgeIsFirstDayOfYear(Stringdate) {

Date firstDay=getYearFirst(Integer.parseInt(format(parse(date),"yyyy")));

returnformat(parse(date),"yyyy-MM-dd").equals(format(firstDay,"yyyy-MM-dd"));

}

/**

* 判断传入的时间是不是年的最后一天

*@paramdate

*@return

*/

public static booleanjudgeIsLastDayOfYear(Stringdate) {

Date lastDay=getCurrYearLast(Integer.parseInt(format(parse(date),"yyyy")));

returnformat(parse(date),"yyyy-MM-dd").equals(format(lastDay,"yyyy-MM-dd"));

}

/**

* 判断传入的时间是不是月的第一天

*@paramdate

*@return

*/

public static booleanjudgeIsFirstDayOfMonth(Stringdate) {

String firstDay=getFirstDayOfMonth(date);

returndate.equals(firstDay);

}

/**

* 判断传入的时间是不是月的最后一天

*@paramdate

*@return

*/

public static booleanjudgeIsLastDayOfMonth(Stringdate) {

String lastDay=getLastDayOfMonth(date);

returndate.equals(lastDay);

}

/**

* 获得今天开始的时间 比如:DateUtil.getYmhmsd(DateUtil.getStartTimeOfDay(new Date())) 结果:2018-05-03 00:00:00

*@paramdate

*@return

*/

public staticDategetStartTimeOfDay(Datedate) {

Calendar day=Calendar.getInstance();

day.setTime(date);

day.set(11,);

day.set(12,);

day.set(13,);

day.set(14,);

returnday.getTime();

}

/**

* 获得今天结束时间 例如:DateUtil.getYmhmsd(DateUtil.getEndTimeOfDay(new Date())) 结果:2018-05-03 23:59:59

*@paramdate

*@return

*/

public staticDategetEndTimeOfDay(Datedate) {

Calendar day=Calendar.getInstance();

day.setTime(date);

day.set(11,23);

day.set(12,59);

day.set(13,59);

day.set(14,999);

returnday.getTime();

}

/**

* 获得今天开始的时间

*@return

*/

public staticDategetTimesmorning() {

Calendar cal=Calendar.getInstance();

cal.set(11,);

cal.set(13,);

cal.set(12,);

cal.set(14,);

returncal.getTime();

}

/**

* 获得昨天开始的时间

*@return

*/

public staticDategetYesterdaymorning() {

Calendar cal=Calendar.getInstance();

cal.setTimeInMillis(getTimesmorning().getTime() -86400000L);

returncal.getTime();

}

/**

* 获得今天开始减去七天 也就是上周的今天的开始时间 比如:DateUtil.getYmhmsd(DateUtil.getWeekFromNow()) 结果:2018-04-26 00:00:00

*@return

*/

public staticDategetWeekFromNow() {

Calendar cal=Calendar.getInstance();

cal.setTimeInMillis(getTimesmorning().getTime() -604800000L);

returncal.getTime();

}

/**

* 获得今天晚上的时间

*@return

*/

public staticDategetTimesnight() {

Calendar cal=Calendar.getInstance();

cal.set(11,24);

cal.set(13,);

cal.set(12,);

cal.set(14,);

returncal.getTime();

}

/**

* 获得这周开始的时间

*@return

*/

public staticDategetStartOfThisWeek() {

GregorianCalendar currentDate=newGregorianCalendar();

currentDate.setFirstDayOfWeek(2);

currentDate.set(11,);

currentDate.set(12,);

currentDate.set(13,);

currentDate.set(7,2);

returncurrentDate.getTime();

}

/**

* 获得这周结束的时间

*@return

*/

public staticDategetEndOfThisWeek() {

GregorianCalendar currentDate=newGregorianCalendar();

currentDate.setFirstDayOfWeek(2);

currentDate.set(11,23);

currentDate.set(12,59);

currentDate.set(13,59);

currentDate.set(7,1);

returncurrentDate.getTime();

}

/**

* 获得这个月开始的时间

*@return

*/

public staticDategetTimesMonthmorning() {

Calendar cal=Calendar.getInstance();

cal.set(cal.get(1),cal.get(2),cal.get(5),,,);

cal.set(5,cal.getActualMinimum(5));

returncal.getTime();

}

/**

* 获得这个月结束的时间

*@return

*/

public staticDategetTimesMonthnight() {

Calendar cal=Calendar.getInstance();

cal.set(cal.get(1),cal.get(2),cal.get(5),,,);

cal.set(5,cal.getActualMaximum(5));

cal.set(11,24);

returncal.getTime();

}

/**

* 获得下一个月开始的时间

*@return

*/

public staticDategetLastMonthStartMorning() {

Calendar cal=Calendar.getInstance();

cal.setTime(getTimesMonthmorning());

cal.add(2,-1);

returncal.getTime();

}

/**

* 获得上一个月开始的时间

*@return

*/

public staticDategetCurrentQuarterStartTime() {

Calendar c=Calendar.getInstance();

intcurrentMonth=c.get(2) +1;

SimpleDateFormat longSdf=newSimpleDateFormat("yyyy-MM-dd HH:mm:ss");

SimpleDateFormat shortSdf=newSimpleDateFormat("yyyy-MM-dd");

Date now=null;

try{

if(currentMonth>=1&¤tMonth

c.set(2,);

}else if(currentMonth>=4&¤tMonth

c.set(2,3);

}else if(currentMonth>=7&¤tMonth

c.set(2,4);

}else if(currentMonth>=10&¤tMonth

c.set(2,9);

}

c.set(5,1);

now=longSdf.parse(shortSdf.format(c.getTime()) +" 00:00:00");

}catch(Exceptionvar6) {

var6.printStackTrace();

}

returnnow;

}

/**

* 获得上一个月结束的时间

*@return

*/

public staticDategetCurrentQuarterEndTime() {

Calendar cal=Calendar.getInstance();

cal.setTime(getCurrentQuarterStartTime());

cal.add(2,3);

returncal.getTime();

}

/**

* 获得当前年开始的时间

*@return

*/

public staticDategetCurrentYearStartTime() {

Calendar cal=Calendar.getInstance();

cal.set(cal.get(1),cal.get(2),cal.get(5),,,);

cal.set(5,cal.getActualMinimum(1));

returncal.getTime();

}

/**

* 获得当前年结束的时间

*@return

*/

public staticDategetCurrentYearEndTime() {

Calendar cal=Calendar.getInstance();

cal.setTime(getCurrentYearStartTime());

cal.add(1,1);

returncal.getTime();

}

/**

* 获得下一年开始的时间

*@return

*/

public staticDategetLastYearStartTime() {

Calendar cal=Calendar.getInstance();

cal.setTime(getCurrentYearStartTime());

cal.add(1,-1);

returncal.getTime();

}

/**

* 给出分钟数 计算小时 比如:DateUtil.secToTime(61) 结果:01:01

*@paramtime

*@return

*/

public staticStringsecToTime(inttime) {

String timeStr=null;

booleanhour=false;

booleanminute=false;

booleansecond=false;

if(time

return"00:00";

}else{

intminute1= time /60;

intsecond1;

if(minute1

second1= time %60;

timeStr=unitFormat(minute1) +":"+unitFormat(second1);

}else{

inthour1=minute1/60;

if(hour1>99) {

return"99:59:59";

}

minute1%=60;

second1= time -hour1*3600-minute1*60;

timeStr=unitFormat(hour1) +":"+unitFormat(minute1) +":"+unitFormat(second1);

}

returntimeStr;

}

}

/**

* 给1-9的数字前面加上0 比如:DateUtil.unitFormat(1) 结果:01

*@parami

*@return

*/

public staticStringunitFormat(inti) {

String retStr=null;

if(i >=&& i

retStr="0"+Integer.toString(i);

}else{

retStr=""+ i;

}

returnretStr;

}

public static voidmain(String[] args) {

Date date=getEndOfThisWeek();

System.out.println(date);

}

}

  • 发表于:
  • 原文链接http://kuaibao.qq.com/s/20180504G1JX3900?refer=cp_1026
  • 腾讯「腾讯云开发者社区」是腾讯内容开放平台帐号(企鹅号)传播渠道之一,根据《腾讯内容开放平台服务协议》转载发布内容。
  • 如有侵权,请联系 cloudcommunity@tencent.com 删除。

扫码

添加站长 进交流群

领取专属 10元无门槛券

私享最新 技术干货

扫码加入开发者社群
领券