package com.shlanbao.tzsc.utils.tools;
|
|
import java.text.DecimalFormat;
|
import java.util.ArrayList;
|
import java.util.List;
|
|
/**
|
* 字符串处理工具类
|
* <li>@author Leejean
|
* <li>@create 2014-6-24 下午04:13:57
|
*/
|
public class StringUtil {
|
|
public static boolean isFloat(String str){
|
try {
|
Float.parseFloat(str);
|
return true;
|
} catch (Exception e) {
|
e.printStackTrace();
|
return false;
|
}
|
}
|
|
public static Float convert2Float(Object obj){
|
float r=0.0F;
|
String str=convertObjToString(obj);
|
if(isFloat(str)){
|
r=Float.parseFloat(str);
|
}
|
return r;
|
}
|
|
public static Integer convert2Integer(Object obj){
|
int r=0;
|
String str=convertObjToString(obj);
|
if(isInteger(str)){
|
r=Integer.parseInt(str);
|
}
|
return r;
|
}
|
|
/**
|
* 去除两端空格
|
* @param str
|
* @return
|
*/
|
public static String trim(String str) {
|
if ((null!=str) && (!"".equals(str))) {
|
return str.trim();
|
}
|
return str;
|
}
|
/**
|
* 不为""," ","null","NULL",NULL 时返回true
|
* @param obj
|
* @return
|
*/
|
public static boolean notEmpty(Object obj){
|
if(obj==null){
|
return false;
|
}else if(obj.toString().trim().equals("")){
|
return false;
|
}else if(obj.toString().trim().equalsIgnoreCase("null")){
|
return false;
|
}
|
return true;
|
}
|
public static Double converObj2Double(Object o){
|
Double res=0D;
|
try {
|
res=Double.valueOf(o.toString());
|
} catch (Exception e) {
|
}
|
return res;
|
}
|
public static Long converObj2Long(Object o){
|
Long res=0L;
|
try {
|
res=Long.valueOf(o.toString());
|
} catch (Exception e) {
|
}
|
return res;
|
}
|
public static String convertObj(Object o){
|
if(o==null){
|
return null;
|
}else{
|
return o.toString().trim();
|
}
|
}
|
/**
|
* 判断字符串是否为空
|
* @param str
|
* @return true 不为空 false 空
|
*/
|
public static boolean notNull(String str) {
|
if ((null!=str) && (!"".equals(str.trim()))) {
|
return true;
|
}
|
return false;
|
}
|
/**
|
* 判断字符串是否是Integer类型
|
* @param str
|
* @return
|
*/
|
public static boolean isInteger(String str){
|
try{
|
Integer.parseInt(str.trim());
|
return true;
|
}catch(Exception ex){
|
return false;
|
}
|
}
|
/**
|
* 判断字符串是否是Double类型
|
* @param str
|
* @return
|
*/
|
public static boolean isDouble(String str){
|
try{
|
Double.parseDouble(str.trim());
|
return true;
|
}catch(Exception ex){
|
return false;
|
}
|
}
|
|
public static String convertObjToString(Object o){
|
if(null!=o){
|
return o.toString();
|
}else{
|
return "";
|
}
|
}
|
|
public static Double convertObjToDouble(Object o){
|
if(null!=o){
|
return Double.parseDouble(o.toString());
|
}else{
|
return 0.0;
|
}
|
}
|
|
public static Double convertObjTox100Double(Object o){
|
if(null!=o){
|
DecimalFormat df = new DecimalFormat("#");
|
double v = Double.parseDouble(o.toString());
|
|
String format = df.format(v*100);
|
return Double.parseDouble(format);
|
}else{
|
return 0.0;
|
}
|
}
|
/**
|
* 分割字符串
|
* @param params 被分割的字符串
|
* @param splitChar 分割字符
|
* @return List<Long>
|
*/
|
public static List<Long> splitToLongList(String params, String splitChar) {
|
List<Long> longlist = new ArrayList<Long>();
|
if(params==null||params.trim().equals("")){return longlist;}
|
String param[] = params.split(splitChar);
|
for (String string : param) {
|
if(string.equals("")){
|
longlist.add(null);
|
}else{
|
longlist.add(Long.parseLong(string));
|
}
|
}
|
return longlist;
|
}
|
/**
|
* 将字符串转换为String数组
|
* @param params
|
* @param splitChar
|
* @return
|
*/
|
public static List<String> splitToStringList(String params, String splitChar) {
|
List<String> stringlist = new ArrayList<String>();
|
if(params==null||params.trim().equals("")){return stringlist;}
|
String param[] = params.split(splitChar);
|
for (String string : param) {
|
if(string.equals("")){
|
stringlist.add(null);
|
}else{
|
stringlist.add(string);
|
}
|
}
|
return stringlist;
|
}
|
/**
|
* 将字符串转换为Integer数组
|
* @param params
|
* @param splitChar
|
* @return
|
*/
|
public static List<Integer> splitToIntegerList(String params, String splitChar) {
|
List<Integer> longlist = new ArrayList<Integer>();
|
if(params==null||params.trim().equals("")){return longlist;}
|
if(!params.trim().equals("")){
|
String param[] = params.split(splitChar);
|
for (String string : param) {
|
if(string.equals("")){
|
longlist.add(null);
|
}else{
|
longlist.add(Integer.parseInt(string));
|
}
|
}
|
}
|
return longlist;
|
}
|
/**
|
* 将字符串转换为Double数组
|
* @param params
|
* @param splitChar
|
* @return
|
*/
|
public static List<Double> splitToDouble(String params, String splitChar) {
|
List<Double> longlist = new ArrayList<Double>();
|
if(params==null||params.trim().equals("")){return longlist;}
|
if(!params.trim().equals("")){
|
String param[] = params.split(splitChar);
|
for (String string : param) {
|
if(string.equals("")){
|
longlist.add(null);
|
}else{
|
longlist.add(Double.parseDouble(string));
|
}
|
}
|
}
|
return longlist;
|
}
|
/**
|
* 格式化
|
* @author Leejean
|
* @create 2014年9月16日下午2:13:17
|
* @param from
|
* @param to
|
* @return
|
*/
|
public static String fmtDateBetweenParams(String o_date,String from,String to){
|
if(StringUtil.notNull(from)||StringUtil.notNull(to)){
|
if(!StringUtil.notNull(from)){
|
return " and ( "+o_date+" <= to_date('"+to+"','yyyy-MM-dd') ) ";
|
}
|
if(!StringUtil.notNull(to)){
|
return " and ( "+o_date+" >= to_date('"+from+"','yyyy-MM-dd') ) ";
|
}
|
return " and ("+o_date+" between to_date('"+from+"','yyyy-MM-dd') and to_date('"+to+"','yyyy_MM-dd')) ";
|
}
|
return "";
|
}
|
|
/**
|
* oracle 日期格式化
|
* @param o_date
|
* @param from
|
* @param to
|
* @return
|
*/
|
public static String OraclefmtDateBetweenParams(String o_date,String from,String to){
|
if(StringUtil.notNull(from)||StringUtil.notNull(to)){
|
if(!StringUtil.notNull(from)){
|
return " and ( "+o_date+" <= to_date('"+to+"','SYYYY-MM-DD HH24:MI:SS') ) ";
|
}
|
if(!StringUtil.notNull(to)){
|
return " and ( "+o_date+" >= to_date('"+from+"','SYYYY-MM-DD HH24:MI:SS') ) ";
|
}
|
return " and ("+o_date+" between to_date('"+from+"','SYYYY-MM-DD HH24:MI:SS') and to_date('"+to+"','SYYYY-MM-DD HH24:MI:SS')) ";
|
}
|
return "";
|
}
|
/**
|
* 格式化
|
* @author bsw
|
* @create 2018年3月5日
|
* @param from
|
* @param to
|
* @return
|
*/
|
public static String fmtDateHHmmssBetweenParams(String o_date,String from,String to){
|
if(StringUtil.notNull(from)||StringUtil.notNull(to)){
|
if(!StringUtil.notNull(from)){
|
return " and ( "+o_date+" <= to_date('"+to+"','yyyy-MM-dd HH24:mi:ss') ) ";
|
}
|
if(!StringUtil.notNull(to)){
|
return " and ( "+o_date+" >= to_date('"+from+"','yyyy-MM-dd HH24:mi:ss') ) ";
|
}
|
|
return " and ("+o_date+" between to_date('"+DateUtil.getDateStart(from)+"','yyyy-MM-dd HH24:mi:ss') and to_date('"+DateUtil.getDateEnd(to)+"','yyyy_MM-dd HH24:mi:ss')) ";
|
}
|
return "";
|
}
|
/**
|
* @Title: arrayToString
|
* @Description: String数组转 sql语句中的in '','',''
|
* @param params
|
* @return String 返回类型
|
* @throws
|
*/
|
public static String arrayToStringBySqlin(String[] params){
|
String returnString="";
|
for(int i=0;i<params.length;i++){
|
if(StringUtil.notNull(params[i])){
|
if(i==params.length-1){
|
returnString+="'"+params[i]+"' ";
|
}else{
|
returnString+="'"+params[i]+"',";
|
}
|
}
|
}
|
return returnString;
|
}
|
|
|
/**
|
* 判断字符串是否为null,如果为null返回空
|
* */
|
public static String nullToString(String str){
|
if(str == null ){
|
return "";
|
}else{
|
return str;
|
}
|
}
|
|
/**
|
* 最大值,最小值
|
* // type 1:最大值 2:最小值 args:数组
|
*
|
* */
|
public static Double getMaxAndMinVal(Double A[],int type){
|
Double min,max;
|
//Double A[]={74.5,48.9,30.2,17.1,62.0}; // 声明整数数组A,并赋初值
|
min=max=A[0];
|
for(int i=0;i<A.length;i++){
|
if(A[i]>max){ // 判断最大值
|
max=A[i];
|
}
|
if(A[i]<min){ // 判断最小值
|
min=A[i];
|
}
|
}
|
//System.out.println("\n数组的最大值是:"+max); // 输出最大值
|
//System.out.println("数组的最小值是:"+min); // 输出最小值
|
if(type==1){
|
return max;
|
}else{
|
return min;
|
}
|
}
|
}
|