Java常用工具类之DateUtils
需要引入的pom坐标
< ! -- GENERAL UTILS begin -- >
< dependency>
< groupId> org. apache. commons< / groupId>
< artifactId> commons- lang3< / artifactId>
< version> 3.3 .2 < / version>
< / dependency>
DateUtils的编写
package com. xinchacha. security. utils;
import org. apache. commons. lang3. StringUtils;
import org. apache. commons. lang3. time. DateFormatUtils;
import java. text. ParseException;
import java. text. SimpleDateFormat;
import java. util. *;
public class DateUtils {
public final static int FORMAT_DEFAULT = 0 ;
public final static int FORMAT_LONG = 1 ;
public final static int FORMAT_SHORT = 2 ;
public final static String DATE_DEFAULT = "yyyy-MM-dd" ;
private final static String DATE_YYYY= "yyyy" ;
private final static String DATE_MM= "mm" ;
private final static String DATE_DD= "dd" ;
public final static String DATE_YYYYMM = "yyyyMM" ;
public final static String DATE_YYYYMMDD = "yyyyMMdd" ;
public final static String DATE_YYYY_MM = "yyyy-MM" ;
public final static String DATE_YYYY_MM_DD = "yyyy-MM-dd" ;
public final static String DATETIME_DEFAULT = "yyyy-MM-dd HH:mm:ss" ;
public final static String DATETIME_YYYY_MM_DD_HH_MM = "yyyy-MM-dd HH:mm" ;
public final static String DATETIME_YYYY_MM_DD_HH_MM_SS = "yyyy-MM-dd HH:mm:ss" ;
public final static String DATETIME_YYYY_MM_DD_HH_MM_SS_SSS = "yyyy-MM-dd HH:mm:ss.SSS" ;
public final static String TIME_DEFAULT = "HH:mm:ss" ;
public final static String TIME_HH_MM = "HH:mm" ;
public final static String TIME_HH_MM_SS = "HH:mm:ss" ;
public static final long YEAR_NUMBER= 365 ;
public static final long MINUTE_TTL = 60 * 1000 l;
public static final long HOURS_TTL = 60 * 60 * 1000 l;
public static final long HALF_DAY_TTL = 12 * 60 * 60 * 1000 l;
public static final long DAY_TTL = 24 * 60 * 60 * 1000 l;
public static final long MONTH_TTL = 30 * 24 * 60 * 60 * 1000 l;
public static int daysBetween ( Date startDate, Date endDate) throws ParseException {
Calendar calendar = Calendar. getInstance ( ) ;
calendar. setTime ( startDate) ;
long sTime = calendar. getTimeInMillis ( ) ;
calendar. setTime ( endDate) ;
long endTime = calendar. getTimeInMillis ( ) ;
long between_days= ( endTime- sTime) / DAY_TTL;
return Integer. parseInt ( String. valueOf ( between_days) ) ;
}
public static Date getNewDate ( ) {
SimpleDateFormat df = new SimpleDateFormat ( DATETIME_YYYY_MM_DD_HH_MM_SS) ;
Date newDate= null;
try {
newDate= df. parse ( df. format ( new Date ( ) ) ) ;
} catch ( ParseException e) {
e. printStackTrace ( ) ;
}
return newDate;
}
public static String formatDate ( Date date, Object. . . pattern) {
String formatDate = null;
if ( pattern != null && pattern. length > 0 ) {
formatDate = DateFormatUtils. format ( date, pattern[ 0 ] . toString ( ) ) ;
} else {
formatDate = DateFormatUtils. format ( date, DATE_YYYY_MM_DD) ;
}
return formatDate;
}
public static String getYear ( ) {
return formatDate ( new Date ( ) , DATE_YYYY) ;
}
public static String getMonth ( ) {
return formatDate ( new Date ( ) , DATE_MM) ;
}
public static String getDay ( ) {
return formatDate ( new Date ( ) , DATE_DD) ;
}
public static String getDateToString ( ) {
Calendar c = Calendar. getInstance ( ) ;
return getYear ( ) + getMonth ( ) + getDay ( ) + c. get ( Calendar. HOUR_OF_DAY) + c. get ( Calendar. MINUTE) ;
}
public static Long pastYear ( Date date) {
return date== null? null: pastDays ( date) / YEAR_NUMBER;
}
public static long pastDays ( Date date) {
long t = new Date ( ) . getTime ( ) - date. getTime ( ) ;
return t/ DAY_TTL;
}
public static long pastHour ( Date date) {
long t = new Date ( ) . getTime ( ) - date. getTime ( ) ;
return t/ HOURS_TTL;
}
public static long pastMinutes ( Date date) {
long t = new Date ( ) . getTime ( ) - date. getTime ( ) ;
return t/ MINUTE_TTL;
}
public static double getDistanceOfTwoDate ( Date before, Date after) {
long beforeTime = before. getTime ( ) ;
long afterTime = after. getTime ( ) ;
return ( afterTime - beforeTime) / DAY_TTL;
}
public static Map< String, String> getBirAgeSex ( String certificateNo) {
System. out. println ( certificateNo) ;
String birthday = "" ;
String age = "" ;
String sexCode = "" ;
int year = Calendar. getInstance ( ) . get ( Calendar. YEAR) ;
char [ ] number = certificateNo. toCharArray ( ) ;
boolean flag = true ;
if ( number. length == 15 ) {
for ( int x = 0 ; x < number. length; x++ ) {
if ( ! flag) return new HashMap < String, String> ( ) ;
flag = Character. isDigit ( number[ x] ) ;
}
} else if ( number. length == 18 ) {
for ( int x = 0 ; x < number. length - 1 ; x++ ) {
if ( ! flag) return new HashMap < String, String> ( ) ;
flag = Character. isDigit ( number[ x] ) ;
}
}
if ( flag && certificateNo. length ( ) == 15 ) {
birthday = "19" + certificateNo. substring ( 6 , 8 ) + "-"
+ certificateNo. substring ( 8 , 10 ) + "-"
+ certificateNo. substring ( 10 , 12 ) ;
sexCode = Integer. parseInt ( certificateNo. substring ( certificateNo. length ( ) - 3 , certificateNo. length ( ) ) ) % 2 == 0 ? "2" : "1" ;
age = ( year - Integer. parseInt ( "19" + certificateNo. substring ( 6 , 8 ) ) ) + "" ;
} else {
birthday = certificateNo. substring ( 6 , 10 ) + "-"
+ certificateNo. substring ( 10 , 12 ) + "-"
+ certificateNo. substring ( 12 , 14 ) ;
sexCode = Integer. parseInt ( certificateNo. substring ( certificateNo. length ( ) - 4 , certificateNo. length ( ) - 1 ) ) % 2 == 0 ? "2" : "1" ;
age = ( year - Integer. parseInt ( certificateNo. substring ( 6 , 10 ) ) ) + "" ;
}
Map< String, String> map = new HashMap < String, String> ( ) ;
map. put ( "birthday" , birthday) ;
map. put ( "age" , age) ;
map. put ( "sexCode" , sexCode) ;
return map;
}
public static int getCarAge ( String birthday) {
String year = birthday. substring ( 0 , 4 ) ;
String yue = birthday. substring ( 4 , 6 ) ;
Date date = new Date ( ) ;
SimpleDateFormat format = new SimpleDateFormat ( DATE_YYYY_MM_DD) ;
String fYear = format. format ( date) . substring ( 0 , 4 ) ;
String fYue = format. format ( date) . substring ( 5 , 7 ) ;
int age = 0 ;
if ( Integer. parseInt ( yue) <= Integer. parseInt ( fYue) ) {
age = Integer. parseInt ( fYear) - Integer. parseInt ( year) + 1 ;
} else {
age = Integer. parseInt ( fYear) - Integer. parseInt ( year) ;
}
return age;
}
public static String getCarSex ( String CardCode) {
String sex;
if ( Integer. parseInt ( CardCode. substring ( 16 ) . substring ( 0 , 1 ) ) % 2 == 0 ) {
sex = "2" ;
} else {
sex = "1" ;
}
return sex;
}
public static String countExecTimeToString_exact ( Calendar beginTime, Calendar endTime)
{
Long timeDispersion = endTime. getTimeInMillis ( ) - beginTime. getTimeInMillis ( ) ;
String tmpMsg = "耗时: " ;
int timeNum = 0 ;
if ( timeDispersion >= ( HOURS_TTL) )
{
timeNum = ( int ) ( timeDispersion/ HOURS_TTL) ;
tmpMsg += timeNum + "时" ;
timeDispersion = timeDispersion - ( timeNum* HOURS_TTL) ;
}
if ( timeDispersion >= ( MINUTE_TTL) )
{
timeNum = ( int ) ( timeDispersion/ MINUTE_TTL) ;
tmpMsg += timeNum + "分" ;
timeDispersion = timeDispersion - ( timeNum* MINUTE_TTL) ;
}
if ( timeDispersion >= 1000 )
{
timeNum = ( int ) ( timeDispersion/ 1000 ) ;
tmpMsg += timeNum + "秒" ;
timeDispersion = timeDispersion - timeNum* 1000 ;
}
tmpMsg += timeDispersion + "毫秒" ;
return tmpMsg;
}
public static Long countExecTimeToLong_exact ( Calendar beginTime, Calendar endTime)
{
return ( endTime. getTimeInMillis ( ) - beginTime. getTimeInMillis ( ) ) ;
}
public static String countExecTimeToString_exact ( Long beginTime, Long endTime)
{
Long timeDispersion = endTime - beginTime;
String tmpMsg = "耗时: " ;
int timeNum = 0 ;
if ( timeDispersion >= ( HOURS_TTL) )
{
timeNum = ( int ) ( timeDispersion/ ( HOURS_TTL) ) ;
tmpMsg += timeNum + "时" ;
timeDispersion = timeDispersion - ( timeNum* HOURS_TTL) ;
}
if ( timeDispersion >= ( MINUTE_TTL) )
{
timeNum = ( int ) ( timeDispersion/ ( MINUTE_TTL) ) ;
tmpMsg += timeNum + "分" ;
timeDispersion = timeDispersion - ( timeNum* MINUTE_TTL) ;
}
if ( timeDispersion >= 1000 )
{
timeNum = ( int ) ( timeDispersion/ 1000 ) ;
tmpMsg += timeNum + "秒" ;
timeDispersion = timeDispersion - timeNum* 1000 ;
}
tmpMsg += timeDispersion + "毫秒" ;
return tmpMsg;
}
public static List< String> getYearMonthByYear ( String year) {
List< String> monthList = new ArrayList < > ( ) ;
for ( int i = 1 ; i <= 12 ; i++ ) {
monthList. add ( year+ "-" + ( i< 10 ? "0" : "" ) + i) ;
}
return monthList;
}
public static String getPreMonth ( String dateStr, String format) {
String preMonth = "" ;
SimpleDateFormat sdf = new SimpleDateFormat ( format) ;
Date date;
try {
date = sdf. parse ( dateStr) ;
Calendar calendar = Calendar. getInstance ( ) ;
calendar. setTime ( date) ;
calendar. add ( calendar. MONTH, 1 ) ;
sdf. format ( calendar. getTime ( ) ) ;
} catch ( ParseException e) {
e. printStackTrace ( ) ;
}
return preMonth;
}
public static String getPreMonthFormat ( String dateStr, String format) {
String preMonth = "" ;
SimpleDateFormat sdf = new SimpleDateFormat ( format) ;
Date date;
try {
date = sdf. parse ( dateStr) ;
Calendar calendar = Calendar. getInstance ( ) ;
calendar. setTime ( date) ;
calendar. add ( calendar. MONTH, 1 ) ;
preMonth= sdf. format ( calendar. getTime ( ) ) ;
} catch ( ParseException e) {
e. printStackTrace ( ) ;
}
return preMonth;
}
public static boolean dateSize ( String aDate, String bDate, SimpleDateFormat format) {
boolean str = false ;
try {
if ( StringUtils. isNotBlank ( aDate) && StringUtils. isNotBlank ( bDate) ) {
Date date1 = format. parse ( aDate) ;
Date date2 = format. parse ( bDate) ;
str = date1. after ( date2) ;
}
} catch ( ParseException e) {
e. printStackTrace ( ) ;
}
return str;
}
}