JAVA各种日期算法大全.docx
- 文档编号:5977579
- 上传时间:2023-05-09
- 格式:DOCX
- 页数:56
- 大小:23.21KB
JAVA各种日期算法大全.docx
《JAVA各种日期算法大全.docx》由会员分享,可在线阅读,更多相关《JAVA各种日期算法大全.docx(56页珍藏版)》请在冰点文库上搜索。
JAVA各种日期算法大全
publicclassDateTime
{
//-----------------------------------------------------------------------------------
/**
*得到系统当前年的数据字符串
*@returnstrYear返回的结果,格式yyyy,String类型
*@exception得到系统当前年的数据字符串错误
*/
publicStringgetYear()
{
StringstrYear="";
DatecurrentDate=newDate();
SimpleDateFormatformatter=newSimpleDateFormat("yyyy");
strYear=formatter.format(currentDate);
returnstrYear;
}
//-----------------------------------------------------------------------------------
/**
*得到系统当前年月数据字符串
*@returnstrYearMonth返回的结果,格式yyyy-MM,String类型
*@exception得到系统当前年月数据字符串错误
*/
publicStringgetYearMonth()
{
StringstrYearMonth="";
DatecurrentDate=newDate();
SimpleDateFormatformatter=newSimpleDateFormat("yyyy-MM");
strYearMonth=formatter.format(currentDate);
returnstrYearMonth;
}
//-----------------------------------------------------------------------------------
/**
*得到系统当前年月日数据字符串
*@returnstrCurrentDate返回的结果,格式yyyy-MM-dd,String类型
*@exception得到系统当前年月日数据字符串错误
*/
publicstaticStringgetDate()
{
StringstrCurrentDate="";
DatecurrentDate=newDate();
SimpleDateFormatformatter=newSimpleDateFormat("yyyy-MM-dd");
strCurrentDate=formatter.format(currentDate);
returnstrCurrentDate;
}
//-----------------------------------------------------------------------------------
/**
*得到系统当前小时,秒,分数据字符串
*@returnstrCurrentTime返回的结果,格式HH:
mm:
ss,String类型
*@exception得到系统当前小时,秒,分数据字符串错误
*/
publicStringgetTime()
{
StringstrCurrentTime="";
DatecurrentTime=newDate();
SimpleDateFormatformatter=newSimpleDateFormat("HH:
mm:
ss");
strCurrentTime=formatter.format(currentTime);
returnstrCurrentTime;
}
//-----------------------------------------------------------------------------------
/**
*得到系统当前年月日小时分秒数据字符串
*@returnstrCurrentDateTime返回的结果,格式yyyy-MM-ddHH:
mm:
ss,String类型
*@exception得到系统当前年月日小时分秒数据字符串错误
*/
publicStringgetDateTime()
{
StringstrCurrentDateTime="";
DatecurrentDateTime=newDate();
SimpleDateFormatformatter=newSimpleDateFormat("yyyy-MM-ddHH:
mm:
ss");
strCurrentDateTime=formatter.format(currentDateTime);
returnstrCurrentDateTime;
}
//-----------------------------------------------------------------------------------
/**
*得到系统当前年月日数据字符串
*@returnstrYMD返回的结果,格式yyyyMMdd,String类型
*@exception得到系统当前年月日数据字符串错误
*/
publicStringgetYMD()
{
StringstrYMD="";
DatecurrentDateTime=newDate();
SimpleDateFormatformatter=newSimpleDateFormat("yyyyMMdd");
strYMD=formatter.format(currentDateTime);
returnstrYMD;
}
//----------------------------------------------------------------------------------
publicstaticStringgetYYYYMMddHHmmss(Datedate){
StringstrYMD="";
SimpleDateFormatformatter=newSimpleDateFormat("yyyyMMddHHmmss");
strYMD=formatter.format(date);
returnstrYMD;
}
//-----------------------------------------------------------------------------------
/**
*得到对比日期变化的目标日期getDateChange("20031015",1)="20031016";
*@paramstrCurrentDate当前日期格式"20000101"
*@paramiQuantity变化的数量以天为单位
*@returnstrTarget返回的结果,格式"20000101",String类型
*@exception得到对比日期变化的目标日期错误
*/
publicStringgetYMDDateChange(StringstrCurrentDate,intiQuantity)
{
StringstrTarget="";
intiYear=Integer.parseInt(strCurrentDate.substring(0,4));
intiMonth=Integer.parseInt(strCurrentDate.substring(4,6));
intiDay=Integer.parseInt(strCurrentDate.substring(6,8));
Calendarcal=Calendar.getInstance();
cal.set(Calendar.YEAR,iYear);
cal.set(Calendar.MONTH,iMonth-1);
cal.set(Calendar.DAY_OF_MONTH,iDay);
cal.add(Calendar.DATE,iQuantity);
DatecurrentDate=cal.getTime();
SimpleDateFormatformatter=newSimpleDateFormat("yyyyMMdd");
strTarget=formatter.format(currentDate);
returnstrTarget;
}
//-----------------------------------------------------------------------------------
/**
*得到系统当前年月日小时分秒数据字符串
*@returnstrYMDHMS返回的结果,格式yyyyMMddHHmmss,String类型
*@exception得到系统当前年月日小时分秒数据字符串错误
*/
publicstaticStringgetYMDHMS()
{
StringstrYMDHMS="";
DatecurrentDateTime=newDate();
SimpleDateFormatformatter=newSimpleDateFormat("yyyyMMddHHmmss");
strYMDHMS=formatter.format(currentDateTime);
returnstrYMDHMS;
}
//-----------------------------------------------------------------------------------
/**
*得到系统当前年月日小时分秒毫秒数据字符串
*@returnstrYMDHMSS返回的结果,格式yyyyMMddHHmmssSSS,String类型
*@exception得到系统当前年月日小时分秒毫秒数据字符串错误
*/
publicstaticStringgetYMDHMSS()
{
StringstrYMDHMSS="";
DatecurrentDateTime=newDate();
SimpleDateFormatformatter=newSimpleDateFormat("yyyyMMddHHmmssSSS");
strYMDHMSS=formatter.format(currentDateTime);
returnstrYMDHMSS;
}
//-----------------------------------------------------------------------------------
/**
*日期格式转换从yyyy-MM-ddHH:
mm:
ss到yyyyMMddHHmmss
*@paramstrDateTime日期格式为yyyy-MM-ddHH:
mm:
ss的数据
*@returnstrYMDHMS返回的结果,日期格式为yyyyMMddHHmmss的数据,String类型
*@exception日期格式转换从yyyy-MM-ddHH:
mm:
ss到yyyyMMddHHmmss错误
*/
publicStringstringToNumber(StringstrDateTime)
{
StringstrYMDHMS="";
if(strDateTime.length()==10)
{
strYMDHMS=strDateTime.substring(0,4)+strDateTime.substring(5,7)+strDateTime.substring(8,10);
}
if(strDateTime.length()==19)
{
strYMDHMS=strDateTime.substring(0,4)+strDateTime.substring(5,7)+strDateTime.substring(8,10)
+strDateTime.substring(11,13)+strDateTime.substring(14,16)+strDateTime.substring(17,19);
}
returnstrYMDHMS;
}
//-----------------------------------------------------------------------------------
/**
*日期格式转换从yyyyMMddHHmmss到yyyy-MM-ddHH:
mm:
ss
*@paramstrYMDHMS日期格式为yyyyMMddHHmmss的数据
*@returnstrDateTime返回的结果,日期格式为yyyy-MM-ddHH:
mm:
ss的数据,String类型
*@exception日期格式转换从yyyyMMddHHmmss到yyyy-MM-ddHH:
mm:
ss错误
*/
publicStringnumberToString(StringstrYMDHMS)
{
StringstrDateTime="";
if(strYMDHMS.length()==8)
{
strDateTime=strYMDHMS.substring(0,4)+"-"+strYMDHMS.substring(4,6)+"-"+strYMDHMS.substring(6,8);
}
if(strYMDHMS.length()==14)
{
strDateTime=strYMDHMS.substring(0,4)+"-"+strYMDHMS.substring(4,6)+"-"+strYMDHMS.substring(6,8)
+""+strYMDHMS.substring(8,10)+":
"+strYMDHMS.substring(10,12)+":
"+strYMDHMS.substring(12,14);
}
returnstrDateTime;
}
//-----------------------------------------------------------------------------------
/**
*得到系统昨天年月日,格式yyyyMMdd
*@returnstrYYMD返回的结果,日期格式为yyyyMMdd的数据,String类型
*@exception得到系统昨天年月日,格式yyyyMMdd错误
*/
publicStringgetAgoYMD()
{
StringstrYYMD="";
StringstrYesterdayDateTime=getDateChange(getDate(),-1);
StringstrYear=strYesterdayDateTime.substring(0,4);
StringstrMonth=strYesterdayDateTime.substring(5,7);
StringstrDay=strYesterdayDateTime.substring(8,10);
strYYMD=strYear+strMonth+strDay;
returnstrYYMD;
}
//-----------------------------------------------------------------------------------
/**
*得到系统昨天年月日,格式yyyy-MM-dd
*@returnstrYDate返回的结果,日期格式为yyyy-MM-dd的数据,String类型
*@exception得到系统昨天年月日,格式yyyy-MM-dd错误
*/
publicStringgetAgoDate()
{
StringstrYDate="";
strYDate=getDateChange(getDate(),-1);
returnstrYDate;
}
//-----------------------------------------------------------------------------------
/**
*得到系统上个月的年月数据,格式yyyyMM
*@returnstrYYM返回的结果,日期格式为yyyyMM的数据,String类型
*@exception得到系统上个月的年月数据,格式yyyyMM错误
*/
publicStringgetAgoYM()
{
StringstrYYM="";
StringstrYearMonth=getYearMonth();
strYYM=getMonthChange(strYearMonth,-1);
strYYM=strYYM.substring(0,4)+strYYM.substring(5,7);
returnstrYYM;
}
//-----------------------------------------------------------------------------------
/**
*得到系统上个月的年月数据,格式yyyy-MM
*@returnstrYYM返回的结果,日期格式为yyyy-MM的数据,String类型
*@exception得到系统上个月的年月数据错误
*/
publicStringgetAgoYearMonth()
{
StringstrYYearMonth="";
StringstrYearMonth=getYearMonth();
strYYearMonth=getMonthChange(strYearMonth,-1);
returnstrYYearMonth;
}
//-----------------------------------------------------------------------------------
/**
*得到系统当前日期是星期几,格式"星期一"
*@returnstrCurrentWeek返回的结果,日期格式为"星期一"的数据,String类型
*@exception得到系统当前日期是星期几错误
*/
publicStringgetWeek()
{
StringstrCurrentWeek="";
DatecurrentWeek=newDate();
SimpleDateFormatformatter=newSimpleDateFormat("E");
strCurrentWeek=formatter.format(currentWeek);
returnstrCurrentWeek;
}
//-----------------------------------------------------------------------------------
/**
*得到任意输入的一个日期的星期数,格式"星期一"
*@returnstrWeek返回的结果,日期格式为"星期一"的数据,String类型
*@exception得到任意输入的一个日期的星期数错误
*/
publicStringgetDateToWeek(StringstrDate)
{
StringstrWeek="";
intiYear=Integer.parseInt(strDate.substring(0,4));
intiMonth=Integer.parseInt(strDate.substring(5,7));
intiDay=Integer.parseInt(strDate.substring(8,10));
Calendarcal=Calendar.getInstance();
cal.set(Calendar.YEAR,iYear);
cal.set(Calendar.MONTH,iMonth-1);
cal.set(Calendar.DAY_OF_MONTH,iDay);
DatecurrentDate=cal.getTime();
SimpleDateFormatformatter=newSimpleDateFormat("E");
strWeek=formatter.format(currentDate);
returnstrWeek;
}
//-----------------------------------------------------------------------------------
/**
*得到系统当前日期是一年中的第几个星期
*@returnstrCurrentWeekInYear返回的结果,String类型
*@exception得到系统当前日期是一年中的第几个星期错误
*/
publicStringgetWeekInYear()
{
StringstrCurrentWeekInYear="";
DatecurrentDate=newDate();
SimpleDateFormatformatter=newSimpleDateFormat("w");
strCurrentWeekInYear=formatter.format(currentDate);
returnstrCurrentWeekInYear;
}
//------------------------------------------------------------------
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- JAVA 各种 日期 算法 大全
![提示](https://static.bingdoc.com/images/bang_tan.gif)