DateUtils
public class DateUtil {
private final static SimpleDateFormat sdfYear = new SimpleDateFormat ( "yyyy" ) ;
private final static SimpleDateFormat sdfDay = new SimpleDateFormat (
"yyyy-MM-dd" ) ;
private final static SimpleDateFormat sdfDays = new SimpleDateFormat (
"yyyyMMdd" ) ;
private final static SimpleDateFormat sdfTime = new SimpleDateFormat (
"yyyy-MM-dd HH:mm:ss" ) ;
private final static SimpleDateFormat sdfTimes = new SimpleDateFormat (
"HH:mm:ss" ) ;
public static String getYear ( ) {
return sdfYear. format ( new Date ( ) ) ;
}
public static String sdfTimes ( ) {
return sdfTimes. format ( new Date ( ) ) ;
}
public static String getDay ( ) {
return sdfDay. format ( new Date ( ) ) ;
}
public static String getDays ( ) {
return sdfDays. format ( new Date ( ) ) ;
}
public static String getTime ( ) {
return sdfTime. format ( new Date ( ) ) ;
}
public static boolean compareDate ( String s, String e) {
if ( fomatDate ( s) == null|| fomatDate ( e) == null) {
return false ;
}
return fomatDate ( s) . getTime ( ) >= fomatDate ( e) . getTime ( ) ;
}
public static Date fomatDate ( String date) {
DateFormat fmt = new SimpleDateFormat ( "yyyy-MM-dd" ) ;
try {
return fmt. parse ( date) ;
} catch ( ParseException e) {
e. printStackTrace ( ) ;
return null;
}
}
public static boolean isValidDate ( String s) {
DateFormat fmt = new SimpleDateFormat ( "yyyy-MM-dd" ) ;
try {
fmt. parse ( s) ;
return true ;
} catch ( Exception e) {
return false ;
}
}
public static int getDiffYear ( String startTime, String endTime) {
DateFormat fmt = new SimpleDateFormat ( "yyyy-MM-dd" ) ;
try {
long aa= 0 ;
int years= ( int ) ( ( ( fmt. parse ( endTime) . getTime ( ) - fmt. parse ( startTime) . getTime ( ) ) / ( 1000 * 60 * 60 * 24 ) ) / 365 ) ;
return years;
} catch ( Exception e) {
return 0 ;
}
}
public static long getDaySub ( String beginDateStr, String endDateStr) {
long day= 0 ;
java. text. SimpleDateFormat format = new java. text. SimpleDateFormat ( "yyyy-MM-dd" ) ;
java. util. Date beginDate = null;
java. util. Date endDate = null;
try {
beginDate = format. parse ( beginDateStr) ;
endDate= format. parse ( endDateStr) ;
} catch ( ParseException e) {
e. printStackTrace ( ) ;
}
day= ( endDate. getTime ( ) - beginDate. getTime ( ) ) / ( 24 * 60 * 60 * 1000 ) ;
return day;
}
public static String getAfterDayDate ( String days) {
int daysInt = Integer. parseInt ( days) ;
Calendar canlendar = Calendar. getInstance ( ) ;
canlendar. add ( Calendar. DATE, daysInt) ;
Date date = canlendar. getTime ( ) ;
SimpleDateFormat sdfd = new SimpleDateFormat ( "yyyy-MM-dd HH:mm:ss" ) ;
String dateStr = sdfd. format ( date) ;
return dateStr;
}
public static String getAfterDayWeek ( String days) {
int daysInt = Integer. parseInt ( days) ;
Calendar canlendar = Calendar. getInstance ( ) ;
canlendar. add ( Calendar. DATE, daysInt) ;
Date date = canlendar. getTime ( ) ;
SimpleDateFormat sdf = new SimpleDateFormat ( "E" ) ;
String dateStr = sdf. format ( date) ;
return dateStr;
}
public static String getData ( ) {
Calendar cal = Calendar. getInstance ( ) ;
int year = cal. get ( Calendar. YEAR) ;
int month = cal. get ( Calendar. MONTH) + 1 ;
int day = cal. get ( Calendar. DAY_OF_MONTH) ;
String data = year+ "-" + month+ "-" + day;
return data;
}
public static String getTimes ( ) {
Calendar cal = Calendar. getInstance ( ) ;
int hour = cal. get ( Calendar. HOUR_OF_DAY) ;
int minute = cal. get ( Calendar. MINUTE) ;
int second = cal. get ( Calendar. SECOND) ;
String time = hour+ ":" + minute+ ":" + second;
return time;
}
}
FileUpload
public class FileUpload {
public static String fileUp ( MultipartFile file, String filePath, String fileName) {
String extName = "" ;
try {
if ( file. getOriginalFilename ( ) . lastIndexOf ( "." ) >= 0 ) {
extName = file. getOriginalFilename ( ) . substring ( file. getOriginalFilename ( ) . lastIndexOf ( "." ) ) ;
}
copyFile ( file. getInputStream ( ) , filePath, fileName+ extName) . replaceAll ( "-" , "" ) ;
} catch ( IOException e) {
System. out. println ( e) ;
}
return fileName+ extName;
}
private static String copyFile ( InputStream in, String dir, String realName)
throws IOException {
File file = new File ( dir, realName) ;
if ( ! file. exists ( ) ) {
if ( ! file. getParentFile ( ) . exists ( ) ) {
file. getParentFile ( ) . mkdirs ( ) ;
}
file. createNewFile ( ) ;
}
FileUtils. copyInputStreamToFile ( in, file) ;
return realName;
}
}
FileDownload
public class FileDownload {
public static void fileDownload ( final HttpServletResponse response, String filePath, String fileName) throws Exception{
byte [ ] data = FileUtil. toByteArray2 ( filePath) ;
fileName = URLEncoder. encode ( fileName, "UTF-8" ) ;
response. reset ( ) ;
response. setHeader ( "Content-Disposition" , "attachment; filename=\"" + fileName + "\"" ) ;
response. addHeader ( "Content-Length" , "" + data. length) ;
response. setContentType ( "application/octet-stream;charset=UTF-8" ) ;
OutputStream outputStream = new BufferedOutputStream ( response. getOutputStream ( ) ) ;
outputStream. write ( data) ;
outputStream. flush ( ) ;
outputStream. close ( ) ;
response. flushBuffer ( ) ;
}
}
Word4PDF
public class Word2PdfUtil {
static final int wdFormatPDF = 17 ;
public static boolean word2pdf ( String source, String target) {
ComThread. InitSTA ( ) ;
ActiveXComponent app = null;
try {
app = new ActiveXComponent ( "Word.Application" ) ;
app. setProperty ( "Visible" , false ) ;
Dispatch docs = app. getProperty ( "Documents" ) . toDispatch ( ) ;
Dispatch doc = Dispatch. call ( docs, "Open" , source, false , true ) . toDispatch ( ) ;
File tofile = new File ( target) ;
if ( tofile. exists ( ) ) {
tofile. delete ( ) ;
}
Dispatch. call ( doc, "SaveAs" , target, wdFormatPDF) ;
Dispatch. call ( doc, "Close" , false ) ;
return true ;
} catch ( Exception e) {
System. out. println ( e) ;
return false ;
} finally {
if ( app != null) {
app. invoke ( "Quit" , 0 ) ;
}
ComThread. Release ( ) ;
}
}
}
PasswordUtil
public class PasswordUtil {
public static String generate ( String password) {
Random r = new Random ( ) ;
StringBuilder sb = new StringBuilder ( 16 ) ;
sb. append ( r. nextInt ( 99999999 ) ) . append ( r. nextInt ( 99999999 ) ) ;
int len = sb. length ( ) ;
if ( len < 16 ) {
for ( int i = 0 ; i < 16 - len; i++ ) {
sb. append ( "0" ) ;
}
}
String salt = sb. toString ( ) ;
password = md5Hex ( password + salt) ;
char [ ] cs = new char [ 48 ] ;
for ( int i = 0 ; i < 48 ; i += 3 ) {
cs[ i] = password. charAt ( i / 3 * 2 ) ;
char c = salt. charAt ( i / 3 ) ;
cs[ i + 1 ] = c;
cs[ i + 2 ] = password. charAt ( i / 3 * 2 + 1 ) ;
}
return new String ( cs) ;
}
public static boolean verify ( String password, String md5) {
char [ ] cs1 = new char [ 32 ] ;
char [ ] cs2 = new char [ 16 ] ;
for ( int i = 0 ; i < 48 ; i += 3 ) {
cs1[ i / 3 * 2 ] = md5. charAt ( i) ;
cs1[ i / 3 * 2 + 1 ] = md5. charAt ( i + 2 ) ;
cs2[ i / 3 ] = md5. charAt ( i + 1 ) ;
}
String salt = new String ( cs2) ;
return md5Hex ( password + salt) . equals ( new String ( cs1) ) ;
}
public static String md5Hex ( String src) {
try {
MessageDigest md5 = MessageDigest. getInstance ( "MD5" ) ;
byte [ ] bs = md5. digest ( src. getBytes ( ) ) ;
return new String ( new Hex ( ) . encode ( bs) ) ;
} catch ( Exception e) {
return null;
}
}
public static String getStringRandom ( int length) {
String val = "" ;
Random random = new Random ( ) ;
for ( int i = 0 ; i < length; i++ ) {
String charOrNum = random. nextInt ( 2 ) % 2 == 0 ? "char" : "num" ;
if ( "char" . equalsIgnoreCase ( charOrNum) ) {
int temp = random. nextInt ( 2 ) % 2 == 0 ? 65 : 97 ;
val += ( char ) ( random. nextInt ( 26 ) + temp) ;
} else if ( "num" . equalsIgnoreCase ( charOrNum) ) {
val += String. valueOf ( random. nextInt ( 10 ) ) ;
}
}
return val;
}
}