9.日期工具
public static String getStringOfYMD ( Date date) {
SimpleDateFormat sdf = new SimpleDateFormat ( "yyyy-MM-dd HH:mm:ss" ) ;
return date == null ? "" : sdf. format ( date) ;
}
public static String getStringOfYMDEx ( Date date) {
SimpleDateFormat sdf = new SimpleDateFormat ( "yyyy年MM月dd日" ) ;
return date == null ? "" : sdf. format ( date) ;
}
public static String getDateToString ( long time) {
Date d = new Date ( time) ;
SimpleDateFormat sf = new SimpleDateFormat ( "yyyy-MM-dd HH:mm:ss" ) ;
return sf. format ( d) ;
}
public static Integer getDaysBetweenDate ( String bDate, String aDate) {
Integer ret = 0 ;
try {
SimpleDateFormat sdf = new SimpleDateFormat ( "yyyy-MM-dd" ) ;
if ( StringUtils . isNotEmpty ( bDate) && StringUtils . isNotEmpty ( aDate) ) {
Date dBDate = sdf. parse ( bDate) ;
Date dADate = sdf. parse ( aDate) ;
ret = Integer . parseInt ( String . valueOf ( ( dADate. getTime ( ) - dBDate. getTime ( ) ) / ( 24 * 60 * 60 * 1000 ) ) ) ;
}
} catch ( Exception e) {
ret = 0 ;
}
return ret;
}
public static String getMonthFirstDate ( ) {
SimpleDateFormat format = new SimpleDateFormat ( "yyyy-MM-dd 00:00:00" ) ;
Calendar cal_1= Calendar . getInstance ( ) ;
cal_1. set ( Calendar . DAY_OF_MONTH, 1 ) ;
String firstDay = format. format ( cal_1. getTime ( ) ) ;
return firstDay;
}
public static int getWeek ( ) {
int week = 0 ;
Date today = new Date ( ) ;
Calendar c = Calendar . getInstance ( ) ;
c. setTime ( today) ;
int weekday = c. get ( Calendar . DAY_OF_WEEK) ;
if ( weekday == 1 ) {
week = 7 ;
} else if ( weekday == 2 ) {
week = 1 ;
} else if ( weekday == 3 ) {
week = 2 ;
} else if ( weekday == 4 ) {
week = 3 ;
} else if ( weekday == 5 ) {
week = 4 ;
} else if ( weekday == 6 ) {
week = 5 ;
} else if ( weekday == 7 ) {
week = 6 ;
}
return week;
}
public static int dateToWeek ( Date datet) {
int [ ] weekDays = { 7 , 1 , 2 , 3 , 4 , 5 , 6 } ;
Calendar cal = Calendar . getInstance ( ) ;
cal. setTime ( datet) ;
int w = cal. get ( Calendar . DAY_OF_WEEK) - 1 ;
if ( w < 0 )
w = 0 ;
return weekDays[ w] ;
}
public static int dateToWeek ( String datetime) {
try {
SimpleDateFormat f = new SimpleDateFormat ( "yyyy-MM-dd" ) ;
return dateToWeek ( ( Date ) f. parse ( datetime) ) ;
} catch ( Exception e) {
e. printStackTrace ( ) ;
return 0 ;
}
}
public static String specifiedTime ( String time, String format, long day) {
if ( StringUtils . isEmpty ( time) ) {
return null ;
} else {
SimpleDateFormat dateFormat = new SimpleDateFormat ( format) ;
Date date = null ;
try {
date = dateFormat. parse ( time) ;
} catch ( ParseException e) {
e. printStackTrace ( ) ;
}
Date newDate = specifiedDate ( date, day) ;
return dateFormat. format ( newDate) ;
}
}
public static Date specifiedDate ( Date date, long day) {
long time = date. getTime ( ) ;
day = day * 24 * 60 * 60 * 1000 ;
time += day;
return new Date ( time) ;
}
public static Date subtractDate ( Date date, long day) {
long time = date. getTime ( ) ;
day = day * 24 * 60 * 60 * 1000 ;
time = time - day;
return new Date ( time) ;
}
public static String dateToStringType ( Date currentTime, String type) {
SimpleDateFormat formatter = new SimpleDateFormat ( type) ;
String dateString = formatter. format ( currentTime) ;
return dateString;
}
public static Integer getDaysBetweenDateByHMS ( String bDate, String aDate) {
Integer ret = 0 ;
if ( ! StringUtils . isNotEmpty ( bDate) || ! StringUtils . isNotEmpty ( aDate) )
{
return 0 ;
}
if ( bDate. length ( ) == 10 ) {
bDate += " 00:00:00" ;
}
if ( aDate. length ( ) == 10 ) {
aDate += " 00:00:00" ;
}
if ( bDate. length ( ) == 16 ) {
bDate += ":00" ;
}
if ( aDate. length ( ) == 16 ) {
aDate += ":00" ;
}
try {
SimpleDateFormat sdf = new SimpleDateFormat ( "yyyy-MM-dd HH:mm:ss" ) ;
if ( StringUtils . isNotEmpty ( bDate) && StringUtils . isNotEmpty ( aDate) ) {
Date dBDate = sdf. parse ( bDate) ;
Date dADate = sdf. parse ( aDate) ;
ret = Integer . parseInt ( String . valueOf ( ( dADate. getTime ( ) - dBDate. getTime ( ) ) / ( 24 * 60 * 60 * 1000 ) ) ) ;
}
} catch ( Exception e) {
e. printStackTrace ( ) ;
ret = 0 ;
}
return ret;
}
public static Integer compareTime ( String DATE1, String DATE2) {
if ( StringUtils . isEmpty ( DATE2) || StringUtils . isEmpty ( DATE1) ) {
return 0 ;
} else {
DateFormat df = new SimpleDateFormat ( "yyyy-MM-dd" ) ;
Date dt1 = null ;
Date dt2 = null ;
try {
dt1 = df. parse ( DATE1) ;
dt2 = df. parse ( DATE2) ;
} catch ( ParseException e) {
e. printStackTrace ( ) ;
}
if ( dt1. getTime ( ) > dt2. getTime ( ) ) {
return 1 ;
} else if ( dt1. getTime ( ) < dt2. getTime ( ) ) {
return 2 ;
} else {
return 2 ;
}
}
}
public static boolean isDate ( String theStr) {
return theStr. matches ( "\\d{4}\\-\\d{2}\\-\\d{2}" ) ;
}
public static String toChinese ( String string) {
String [ ] s1 = { "零" , "一" , "二" , "三" , "四" , "五" , "六" , "七" , "八" , "九" } ;
String [ ] s2 = { "十" , "百" , "千" , "万" , "十" , "百" , "千" , "亿" , "十" , "百" , "千" } ;
String result = "" ;
int n = string. length ( ) ;
for ( int i = 0 ; i < n; i++ ) {
int num = string. charAt ( i) - '0' ;
if ( i != n - 1 && num != 0 ) {
result += s1[ num] + s2[ n - 2 - i] ;
} else {
result += s1[ num] ;
}
}
return result;
}