package com.sise.common;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Field;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Collection;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
public class Converts {
public static final String EMPTY = "";
public static final SimpleDateFormat DATE_FORMAT;
public static final SimpleDateFormat DATETIME_FORMAT;
public static final String DATE_PATTERN = "^\\d{4}-\\d{2}-\\d{2}$";
public static final String DATETIME_PATTERN = "^\\d{4}-\\d{2}-\\d{2} \\d{2}:\\d{2}:\\{2}$";
static{
DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd");
DATETIME_FORMAT = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
}
/**
* 除去字符串最后一个 ,
*/
public static String sub(String s){
return s.substring(0,s.length()-1);
}
public static String sub(String s,int beginIndex){
return s.substring(beginIndex);
}
public static String sub(String s,int beginIndex,int endIndex){
return s.substring(beginIndex, endIndex);
}
/**
* 集合除首尾 '[' ']'
*/
public static String sub(Collection<?> col){
return join(col,",");
}
// Integer 转换
//------------------------------------------------------
public static Integer I(Object o){
return I(o, null);
}
public static Integer I(Object o,Integer defaultVal){
if(o == null){
return defaultVal;
}
if(o instanceof Number){
return ((Number)o).intValue();
}
return Integer.valueOf((String)o);
}
// Long 转换
//------------------------------------------------------
public static Long L(Object o){
return L(o, null);
}
public static Long L(Object o,Long defaultVal){
if(o == null){
return defaultVal;
}
if(o instanceof Number){
return ((Number)o).longValue();
}
return Long.valueOf((String)o);
}
// Float 转换
//------------------------------------------------------
public static Float F(Object o){
return F(o, null);
}
public static Float F(Object o,Float defaultVal){
if(o == null){
return defaultVal;
}
if(o instanceof Number){
return ((Number)o).floatValue();
}
return Float.valueOf((String)o);
}
// Double 转换
//------------------------------------------------------
public static Double D(Object o){
return D(o, null);
}
public static Double D(Object o,Double defaultVal){
if(o == null){
return defaultVal;
}
if(o instanceof Number){
return ((Number)o).doubleValue();
}
return Double.valueOf((String)o);
}
// Character 转换 String
//------------------------------------------------------
public static String C(Object o){
return o!=null ? o.toString(): null ;
}
public static String C(Object o,String defaultVal){
return o!=null ? o.toString(): defaultVal ;
}
public static String S(Object o){
return o!=null ? o.toString(): null ;
}
public static String S(Object o,String defaultVal){
return o!=null ? o.toString(): defaultVal ;
}
public static int parseInt(String s){
return Integer.parseInt(s);
}
public static long parseLong(String s){
return Long.parseLong(s);
}
public static float parseFloat(String s){
return Float.parseFloat(s);
}
public static double parseDouble(String s){
return Double.parseDouble(s);
}
// String 转 Date
//------------------------------------------------------
public static synchronized Date parsetDate(String s) throws ParseException{
try {
return DATE_FORMAT.parse(s);
} catch (ParseException e) {
throw e;
}
}
public static synchronized Date parsetDateTime(String s) throws ParseException{
try {
return DATETIME_FORMAT.parse(s);
} catch (ParseException e) {
throw e;
}
}
// Date 转 String
//------------------------------------------------------
public static String format(Date date,String pattern){
return new SimpleDateFormat(pattern).format(date);
}
public static synchronized String dateFormat(Date date){
return DATE_FORMAT.format(date);
}
public static synchronized String dateTimeFormat(Date date){
return DATETIME_FORMAT.format(date);
}
// 集合,数组转换为字符串,自定设置分隔符
//------------------------------------------------------
/**
* <p>Joins the elements of the provided array into a single String
* containing the provided list of elements.</p>
*
* <p>No separator is added to the joined String.
* Null objects or empty strings within the array are represented by
* empty strings.</p>
*
* <pre>
* StringUtils.join(null) = null
* StringUtils.join([]) = ""
* StringUtils.join([null]) = ""
* StringUtils.join(["a", "b", "c"]) = "abc"
* StringUtils.join([null, "", "a"]) = "a"
* </pre>
*
* @param array the array of values to join together, may be null
* @return the joined String, <code>null</code> if null array input
* @since 2.0
*/
public static String join(Object[] array) {
return join(array, null);
}
/**
* <p>Joins the elements of the provided array into a single String
* containing the provided list of elements.</p>
*
* <p>No delimiter is added before or after the list.
* Null objects or empty strings within the array are represented by
* empty strings.</p>
*
* <pre>
* StringUtils.join(null, *) = null
* StringUtils.join([], *) = ""
* StringUtils.join([null], *) = ""
* StringUtils.join(["a", "b", "c"], ';') = "a;b;c"
* StringUtils.join(["a", "b", "c"], null) = "abc"
* StringUtils.join([null, "", "a"], ';') = ";;a"
* </pre>
*
* @param array the array of values to join together, may be null
* @param separator the separator character to use
* @return the joined String, <code>null</code> if null array input
* @since 2.0
*/
public static String join(Object[] array, char separator) {
if (array == null) {
return null;
}
return join(array, separator, 0, array.length);
}
/**
* <p>Joins the elements of the provided array into a single String
* containing the provided list of elements.</p>
*
* <p>No delimiter is added before or after the list.
* Null objects or empty strings within the array are represented by
* empty strings.</p>
*
* <pre>
* StringUtils.join(null, *) = null
* StringUtils.join([], *) = ""
* StringUtils.join([null], *) = ""
* StringUtils.join(["a", "b", "c"], ';') = "a;b;c"
* StringUtils.join(["a", "b", "c"], null) = "abc"
* StringUtils.join([null, "", "a"], ';') = ";;a"
* </pre>
*
* @param array the array of values to join together, may be null
* @param separator the separator character to use
* @param startIndex the first index to start joining from. It is
* an error to pass in an end index past the end of the array
* @param endIndex the index to stop joining from (exclusive). It is
* an error to pass in an end index past the end of the array
* @return the joined String, <code>null</code> if null array input
* @since 2.0
*/
public static String join(Object[] array, char separator, int startIndex, int endIndex) {
if (array == null) {
return null;
}
int bufSize = (endIndex - startIndex);
if (bufSize <= 0) {
return EMPTY;
}
bufSize *= ((array[startIndex] == null ? 16 : array[startIndex].toString().length()) + 1);
StringBuffer