DateUtil日期处理工具类
程序员文章站
2022-04-04 21:18:19
...
public class DateUtil
{
// 按照给定的格式化字符串格式化日期
public static String formatDate( Date date, String formatStr )
{
SimpleDateFormat sdf = new SimpleDateFormat( formatStr );
return sdf.format( date );
}
// 按照给定的格式化字符串解析日期
public static Date parseDate( String dateStr, String formatStr )
throws ParseException
{
Date date = null;
SimpleDateFormat sdf = new SimpleDateFormat( formatStr );
date = sdf.parse( dateStr );
return date;
}
// 从字符串中分析日期
public static Date parseDate( String dateStr ) throws ParseException
{
Date date = null;
String[] dateArray = dateStr.split( "\\D+" ); // +防止多个非数字字符在一起时导致解析错误
int dateLen = dateArray.length;
int dateStrLen = dateStr.length();
if ( dateLen > 0 )
{
if ( dateLen == 1 && dateStrLen > 4 )
{
if ( dateStrLen == "yyyyMMddHHmmss".length() )
{
// 如果字符串长度为14位并且不包含其他非数字字符,则按照(yyyyMMddHHmmss)格式解析
date = parseDate( dateStr, "yyyyMMddHHmmss" );
} else if ( dateStrLen == "yyyyMMddHHmm".length() )
{
date = parseDate( dateStr, "yyyyMMddHHmm" );
} else if ( dateStrLen == "yyyyMMddHH".length() )
{
date = parseDate( dateStr, "yyyyMMddHH" );
} else if ( dateStrLen == "yyyyMMdd".length() )
{
date = parseDate( dateStr, "yyyyMMdd" );
} else if ( dateStrLen == "yyyyMM".length() )
{
date = parseDate( dateStr, "yyyyMM" );
}
} else
{
String fDateStr = dateArray[0];
for ( int i = 1; i < dateLen; i++ )
{
// 左补齐是防止十位数省略的情况
fDateStr += leftPad( dateArray[i], "0", 2 );
}
if ( dateStr.trim()
.matches( "^\\d{1,2}:\\d{1,2}(:\\d{1,2})?$" ) )
{
// 补充年月日3个字段
dateLen += 3;
fDateStr = formatDate( new Date(), "yyyyMMdd" ) + fDateStr;
}
date = parseDate( fDateStr, "yyyyMMddHHmmss".substring( 0,
(dateLen - 1) * 2 + 4 ) );
}
}
return date;
}
// 左补齐
public static String leftPad( String str, String pad, int len )
{
String newStr = (str == null ? "" : str);
while ( newStr.length() < len )
{
newStr = pad + newStr;
}
if ( newStr.length() > len )
{
newStr = newStr.substring( newStr.length() - len );
}
return newStr;
}
// 获取给定日期中的年月
public static String getYearMonth( java.sql.Date date )
{
Calendar mCalendar = GregorianCalendar.getInstance();
mCalendar.setTime( date );
int year = mCalendar.get( Calendar.YEAR );
int month = mCalendar.get( Calendar.MONTH ) + 1;
if ( month < 10 )
{
return String.valueOf( year ) + "0" + String.valueOf( month );
} else
{
return String.valueOf( year ) + String.valueOf( month );
}
}
// 获取当前年月字符串
public static String findYearMonth()
{
/**
* 声明一个int变量year
*/
int year;
/**
* 声明一个int变量month
*/
int month;
/**
* 声明一个字符串变量date
*/
String date;
/**
* 实例化一个对象calendar
*/
Calendar calendar = Calendar.getInstance();
/**
* 获取年份
*/
year = calendar.get( Calendar.YEAR );
/**
* 获取月份
*/
month = calendar.get( Calendar.MONTH ) + 1;
/**
* 拼接年份和月份
*/
if ( month < 10 )
{
return String.valueOf( year ) + "0" + String.valueOf( month );
} else
{
return String.valueOf( year ) + String.valueOf( month );
}
}
// 获取当前年月字符串
public static String findYearMonthWithSlash()
{
/**
* 声明一个int变量year
*/
int year;
/**
* 声明一个int变量month
*/
int month;
/**
* 声明一个字符串变量date
*/
String date;
/**
* 实例化一个对象calendar
*/
Calendar calendar = Calendar.getInstance();
/**
* 获取年份
*/
year = calendar.get( Calendar.YEAR );
/**
* 获取月份
*/
month = calendar.get( Calendar.MONTH ) + 1;
/**
* 拼接年份和月份
*/
if ( month < 10 )
{
return String.valueOf( year ) + "-0" + String.valueOf( month );
} else
{
return String.valueOf( year ) + "-" + String.valueOf( month );
}
}
public static int getDay( String strDate )
{
try
{
SimpleDateFormat format = new SimpleDateFormat( "yyyy-MM-dd" );
Date date = format.parse( strDate );
Calendar mCalendar = GregorianCalendar.getInstance();
mCalendar.setTime( date );
return mCalendar.get( Calendar.DAY_OF_MONTH );
} catch ( ParseException e )
{
e.printStackTrace();
return -1;
}
}
public static int getDateDay( Date date )
{
Calendar mCalendar = GregorianCalendar.getInstance();
mCalendar.setTime( date );
return mCalendar.get( Calendar.DAY_OF_MONTH );
}
/**
* @Title: days @Description: 获取某年某月的天数 @param @param year 年份 @param @param
* month 月份 @param @return 设定文件 @return int 返回类型 @throws
*/
public static int days( int year, int month )
{
int days = 0;
if ( month != 2 )
{
switch ( month )
{
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
days = 31;
break;
case 4:
case 6:
case 9:
case 11:
days = 30;
}
} else
{
// 闰年
if ( year % 4 == 0 && year % 100 != 0 || year % 400 == 0 )
days = 29;
else
days = 28;
}
return days;
}
/**
* @Title: getLastDate @Description: 获取前一天的日期 @param @param
* date @param @return 设定文件 @return Date 返回类型 @throws
*/
public static Date getLastDate( Date date )
{
Calendar calendar = Calendar.getInstance();
calendar.setTime( date );
calendar.add( Calendar.DAY_OF_MONTH, -1 );// -1今天的时间减一天
date = calendar.getTime();
return date;
}
/**
* 获取指定月的前一月(年)或后一月(年)
* @param dateStr 指定日期
* @param addYear 增加的年数
* @param addMonth 增加的月数
* @param addDate 增加的天数
* @return 输入的时期格式为yyyyMM,输出的日期格式为yyyyMM
* @throws Exception
*/
public static String getLastMonth( String dateStr, int addYear,
int addMonth, int addDate ) throws Exception
{
try
{
SimpleDateFormat sdf = new SimpleDateFormat(
"yyyyMM" );
Date sourceDate = sdf.parse( dateStr );
Calendar cal = Calendar.getInstance();
cal.setTime( sourceDate );
cal.add( Calendar.YEAR, addYear );
cal.add( Calendar.MONTH, addMonth );
cal.add( Calendar.DATE, addDate );
SimpleDateFormat returnSdf = new SimpleDateFormat(
"yyyyMM" );
String dateTmp = returnSdf.format( cal.getTime() );
Date returnDate = returnSdf.parse( dateTmp );
return dateTmp;
} catch ( Exception e )
{
e.printStackTrace();
throw new Exception( e.getMessage() );
}
}
/**
* @Title: getNextDate @Description: 获取后一天的日期 @param @param
* date @param @return 设定文件 @return Date 返回类型 @throws
*/
public static Date getNextDate( Date date )
{
Calendar calendar = Calendar.getInstance();
calendar.setTime( date );
calendar.add( Calendar.DAY_OF_MONTH, +1 );// +1今天的时间加一天
date = calendar.getTime();
return date;
}
public static int getAge( Date birthDate )
{
if ( birthDate == null )
throw new RuntimeException( "出生日期不能为null" );
int age = 0;
Date now = new Date();
SimpleDateFormat format_y = new SimpleDateFormat( "yyyy" );
SimpleDateFormat format_M = new SimpleDateFormat( "MM" );
String birth_year = format_y.format( birthDate );
String this_year = format_y.format( now );
String birth_month = format_M.format( birthDate );
String this_month = format_M.format( now );
// 初步,估算
age = Integer.parseInt( this_year ) - Integer.parseInt( birth_year );
// 如果未到出生月份,则age - 1
if ( this_month.compareTo( birth_month ) < 0 )
age -= 1;
if ( age < 0 )
age = 0;
return age;
}
}
上一篇: 效果真的很明显笑了吧