package com.cando.util;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.sql.Timestamp;
import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Tools {
public static Tools my = new Tools();
/**
* 判断数据是否为空
*
* @param str
* @return
*/
public static boolean isNull(Object o) {
if(o == null){
return true;
}
String str = o.toString();
if ("".equals(str.toString().trim()) || "null".equalsIgnoreCase(str.trim())
|| str.trim().length() == 0) {
return true;
} else {
return false;
}
}
/**
* 判断数据是否不为空
*
* @param str
* @return
*/
public static boolean isNotNull(Object str) {
return !isNull(str);
}
/**
* 将一个日期字符串转换成日期
* @param str
* @return
* @throws ParseException
* @throws Exception
*/
public static Date strToDate(String str, String...formatStr) throws ParseException{
String f = "yyyy-MM-dd";
if(formatStr != null && formatStr.length>0){
f = formatStr[0];
}
SimpleDateFormat format = new SimpleDateFormat(f);
// 设置lenient为false.
// 否则SimpleDateFormat会比较宽松地验证日期,比如2007/02/29会被接受,并转换成2007/03/01
format.setLenient(false);
return format.parse(str);
}
/**
* 将字符串转为驼峰命名
* @param str
* @return
*/
public static String strToHumpName(String str){
int index = str.indexOf("_");
if(isNull(str)){
return null;
}else{
str = str.trim();
if(index == -1){
return str.toLowerCase();
}
}
str = str.toLowerCase();
String []arr = str.split("_");
String one = null;
StringBuffer sb = new StringBuffer();
for(int i = 0; i<arr.length; i++){
if(i == 0){
sb.append(arr[0]);
}else{
one = arr[i];
sb.append(one.substring(0, 1).toUpperCase()).append(one.substring(1));
}
}
return sb.toString();
}
/**
* 获取当前服务器的时间字符串
*
* @param num
* @return
*/
public static String dateToStr(Date date, String... format) {
String fmt = "yyyy-MM-dd";
if(format != null && format.length != 0){
fmt = format[0];
}
SimpleDateFormat df = new SimpleDateFormat(fmt);
String dateStr = df.format(date);
return dateStr;
}
/**
* 获取某个月的天数
* @param date
* @return
*/
public static int getDaysOfMonth(Date date) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
return calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
}
/**
* 格式化月份
* @param month
* @return
* @throws Exception
*/
public String formatMonth(String month) throws Exception{
if(isNull(month)){
return "";
}
month = month.trim();
try {
int i = Integer.parseInt(month);
if(i < 10){
month = "0"+i;
}
} catch (Exception e) {
e.printStackTrace();
throw new Exception("月份非数字");
}
return month;
}
/**
* 获取当前服务器日期
*
* @param num
* @return
*/
public static String getCurrentDateStr(String... format) {
Date date = new Date();
String f = "yyyy-MM-dd";
if(format != null && format.length > 0){
f = format[0];
}
SimpleDateFormat df = new SimpleDateFormat(f);
String dateStr = df.format(date);
return dateStr;
}
/**
*日期加 num天
* @param date
* @param num
* @return
*/
public static Date dateAdd(Date date, int num){
if(date == null){
date = new Date();
}
Calendar c = Calendar.getInstance();
c.setTime(date);
c.add(Calendar.DATE, num);
return c.getTime();
}
/**
* String转int
* @param str
* @return
*/
public static Integer strToInt(String str){
if(isNull(str)){
return null;
}
int i = new BigDecimal(str).intValue();
return i;
}
/**
* 转成double
* @param v
* @param def
* @return
*/
public static Double toDouble(Object v, Double... def){
Double d = null;
if(v != null ){
d = Double.parseDouble(v.toString());
}else{
if(def != null && def.length>0){
d = def[0];
}
}
return d;
}
/**
* 计算两个日期字符串相差分钟数
* @param start
* @param end
* @return
* @throws Exception
*/
public static int minutesApart(String start,String end){
SimpleDateFormat sdf=new SimpleDateFormat("yyyymmddhhmmss");
int minutes=0;
try {
Date startDate=sdf.parse(start);
Date endDate=sdf.parse(end);
minutes=(int) ((endDate.getTime()-startDate.getTime())/(60*1000));
} catch (ParseException e) {
// TODO Auto-generated catch block
System.out.println("日期字符串格式不正确!");
e.printStackTrace();
}
return minutes;
}
/**
* 写文件
* @param filePath
* @param sf
* @throws IOException
*/
public static void writeFile(String filePath, String sf) throws Exception{
if(isNull(filePath)){
throw new Exception("文件路径不能为空");
}
FileWriter fw = null;
try {
File f = new File(filePath);
if(!f.exists()){
f.createNewFile();
}
fw = new FileWriter(f);
fw.write(sf.toString());
fw.flush();
} catch (Exception e) {
e.printStackTrace();
}finally{
if(fw != null){
fw.close();
}
}
}
/**
* 将list解析到sql中执行查询
* @param list
* @param sep
* @return
*/
public static String listToSqlStr(List<String> list, String sep){
if(list == null || list.size()==0){
return null;
}
if(isNull(sep)){
sep = ",";
}
StringBuffer conSb = new StringBuffer();
int size = list.size();
for(int i = 0; i<size; i++){
if(i == size-1){
conSb.append("'"+list.get(i)+"'");
}else{
conSb.append("'"+list.get(i)+"'" + sep);
}
}
return conSb.toString();
}
/**
* 将世界时间转为北京时间(+8小时)
* @param date
* @return
*/
public static Timestamp getBeiJingDate(Date date){
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.add(Calendar.HOUR, 8);
return new Timestamp(calendar.getTime().getTime());
}
/**
* 四舍五入并保留多少位数
* @param value
* @param len
* @return
*/
public static String digitalNumberRoundHalfUp(Object value, Integer len){
if(value == null || isNull(value.toString())){
return "0.0";
}
StringBuffer sb = new StringBuffer();
if(len == null){
len = 2;//如果没有获取到精度、默认取2位小数.
}
for(int i = 0; i<len; i++){
sb.append("0");
}
String format = "#";
if(sb.length() != 0){
format += "."+sb;
}
DecimalFormat df = new DecimalFormat(format);
df.setRoundingMode(RoundingMode.HALF_UP);
format = df.format(Double.parseDouble(String.valueOf(value)));
if(format.indexOf(".") == 0){
format = "0" + format;
}
return format;
}
/**
* 字符串是否是一个整数(正则表达式版)
*
* @param str
* @return
*/
public static boolean isDigit(String strDigit) {
Pattern pattern = Pattern.compile("[0-9]{1,}");
Matcher matcher = pattern.matcher((CharSequence) strDigit);
return matcher.matches();
}
public static boolean isNotDigit(String strDigit) {
return !isDigit(strDigit);
}
/**
* 从数组中获取最小值
* @param arr
* @return
*/
public static Double getMinFromDoubleArr(List<Double> arr){
if(arr == null || arr.size() == 0){
return null;
}
double min = arr.get(0);
int size = arr.size();
Double temp = null;
for(int i = 1; i<size; i++){
temp = arr.get(i);
if(temp < min){
min = temp;
}
}
return min;
}
}
推荐大家一个Java的学习网站:Java知识学习网,Java资料下载,Java学习路线图,网址:https://www.java1010.com