package com.ezio.utils;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.math.BigDecimal;
import java.nio.channels.FileChannel;
import java.text.DateFormat;
import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collection;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import org.apache.commons.io.FileUtils;
import org.springframework.web.multipart.MultipartFile;
public class CommonUtil {
/**
* 判断对象是否Empty(null或元素为0)<br>
* 实用于对如下对象做判断:String Collection及其子类 Map及其子类
*
* @param pObj
* 待检查对象
* @return boolean 返回的布尔值
*/
@SuppressWarnings("rawtypes")
public static boolean isEmpty(Object pObj) {
if (pObj == null)
return true;
if (pObj.equals(""))
return true;
if (pObj instanceof String) {
if (((String) pObj).length() == 0) {
return true;
}
} else if (pObj instanceof Collection) {
if (((Collection) pObj).size() == 0) {
return true;
}
} else if (pObj instanceof Map) {
if (((Map) pObj).size() == 0) {
return true;
}
}
return false;
}
/**
* 判断对象是否为NotEmpty(!null或元素>0)<br>
* 实用于对如下对象做判断:String Collection及其子类 Map及其子类
*
* @param pObj
* 待检查对象
* @return boolean 返回的布尔值
*/
@SuppressWarnings("rawtypes")
public static boolean isNotEmpty(Object pObj) {
if (pObj == null)
return false;
if (pObj.equals(""))
return false;
if (pObj instanceof String) {
if (((String) pObj).length() == 0) {
return false;
}
} else if (pObj instanceof Collection) {
if (((Collection) pObj).size() == 0) {
return false;
}
} else if (pObj instanceof Map) {
if (((Map) pObj).size() == 0) {
return false;
}
}
return true;
}
/**
* 数据格式化.
*
* @param pattern
* the pattern
* @param i
* the i
* @return the string
*/
public static String codeFormat(String pattern, Object value) {
DecimalFormat df = new DecimalFormat(pattern);
return df.format(value);
}
/**
* 格式化时间.
*
* @param date
* the date
* @return the string
*/
public static String fomatDate(String date) {
if (isNotEmpty(date)) {
return date.substring(0, 4) + "-" + date.substring(4, 6) + "-"
+ date.substring(6, 8);
}
return null;
}
/**
* 格式化时间.
*
* @param date
* the date
* @return the string
*/
public static String fomatLongDate(String date) {
if (isNotEmpty(date)) {
return date.substring(0, 4) + "-" + date.substring(4, 6) + "-"
+ date.substring(6, 8) + " " + date.substring(8, 10) + ":"
+ date.substring(10, 12) + ":" + date.substring(12, 14);
}
return null;
}
/**
* 格式化时间.
*
* @param date
* the date
* @return the string
*/
public static String fomatDateTime2String(String date) {
if (isNotEmpty(date)) {
return date.replace("-", "").replace("T", "").replace(":", "")
.replace(" ", "");
}
return null;
}
/**
* 将时间字符串格式化成一个日期(java.util.Date)
*
* @param dateStr
* 要格式化的日期字符串,如"2014-06-15 12:30:12"
* @param formatStr
* 格式化模板,如"yyyy-MM-dd HH:mm:ss"
* @return the string
*/
public static Date formatDateString2Date(String dateStr, String formatStr) {
DateFormat dateFormat = new SimpleDateFormat(formatStr);
Date date = null;
try {
date = dateFormat.parse(dateStr);
} catch (Exception e) {
e.printStackTrace();
}
return date;
}
/**
* 将时间字符串格式化成一个日期(java.util.Date)
*
* @param dateStr
* 要格式化的日期字符串,如"2014-06-15 12:30:12"
* @param formatStr
* 格式化模板,如"yyyy-MM-dd HH:mm:ss"
* @return the string
*/
public static String formatDate2String(Date date, String formatStr) {
DateFormat dateFormat = new SimpleDateFormat(formatStr);
String result = null;
try {
result = dateFormat.format(date);
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
/**
* 将一个毫秒数时间转换为相应的时间格式
*
* @param longSecond
* @return
*/
public static String formateDateLongToString(long longSecond) {
GregorianCalendar gc = new GregorianCalendar();
gc.setTimeInMillis(longSecond);
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
return format.format(gc.getTime());
}
/**
* 格式化金额.
*
* @param value
* the value
* @return the string
*/
public static String formatCurrency2String(Long value) {
if (value == null || "0".equals(String.valueOf(value))) {
return "0.00";
}
DecimalFormat df = new DecimalFormat("0.00");
return df.format(value / 100.00);
}
/**
* 格式化金额.
*
* @param priceFormat
* the price format
* @return the long
*/
public static Long formatCurrency2Long(String priceFormat) {
BigDecimal bg = new BigDecimal(priceFormat);
Long price = bg.multiply(new BigDecimal(100)).longValue();
return price;
}
/**
* 获取当前时间.
*
* @param currentDate
* the current date
* @return
* @throws
*/
public static String getToDayStr() {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
return sdf.format(new Date());
}
/**
* 获取当前时间当作文件名称
*
* @return
*/
public static String getToDayStrAsFileName() {
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmssSSS");
return sdf.format(new Date());
}
public static Date getToDay() throws ParseException {
// SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
// Date date = sdf.parse(String.valueOf(System.currentTimeMillis()));
return new Date();
}
/**
* 获取下一天.
*
* @param currentDate
* the current date
* @return the next date str
* @throws ParseException
* the parse exception
*/
public static String getNextDateStr(String currentDate)
throws ParseException {
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
Date date = sdf.parse(currentDate);
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.add(Calendar.DATE, 1);
String nextDate = sdf.format(calendar.getTime());
return nextDate;
}
/**
* 获取上一天.
*
* @param currentDate
* the current date
* @return the next date str
* @throws ParseException
* the parse exception
*/
public static String getYesterdayStr(String currentDate)
throws ParseException {
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
Date date = sdf.parse(currentDate);
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.add(Calendar.DATE, -1);
String nextDate = sdf.format(calendar.getTime());
return nextDate;
}
/**
* 根据日期获取星期
*
* @param strdate
* @return
*/
public static String getWeekDayByDate(String strdate) {
final String dayNames[] = { "星期日", "星期一", "星期二", "星期三", "星期四", "星期五",
"星期六" };
SimpleDateFormat sdfInput = new SimpleDateFormat("yyyy-MM-dd");
Calendar calendar = Calendar.getInstance();
Date date = new Date();
tr