/**
* @Author: MrZcc
* @Description:
* @Modified By:
*/
public class Utils {
/**
* @Description:判断字符串是否为数字,支持整数,负数,小数
* @param: string 需要判断的字符串
* @return: boolean true---是数字,false---不是数字
*/
public static boolean isNumber(String string) {
//正则表达式判断字符串是否为数字
Boolean str = string.trim().matches("-?[0-9]+.*[0-9]*");
if (str != true) {
return false;
}
return true;
}
/**
* @Description:将字符串list转为字符串数组
* @param: list 要转换的集合
* @return: java.lang.String[]
*/
public static String[] listToArray(List<String> list) {
String[] arr = new String[list.size()];
for (int i = 0; i < list.size(); i++) {
arr[i] = list.get(i);
}
return arr;
}
/**
* @Description:交换数组中两个数据的位置
* @param: arr 传入的数组(支持各种类型数组)
* @param: i
* @param: j
* @return: void
*/
public static void swap(Object[] arr, int i, int j) {
Object t = arr[i];
arr[i] = arr[j];
arr[j] = t;
}
/**
* @Description:随机生成n个元素在[rangeL,rangeR]范围内的数组
* @param: n 数组元素个数
* @param: rangeL 数组最小值
* @param: rangeR 数组最大值
* @return: int[] 生成的数组
*/
public static int[] generateRandomArray(int n, int rangeL, int rangeR) {
int[] arr = new int[n];
for (int i = 0; i < n; i++) {
arr[i] = (int) (Math.random() * (rangeR - rangeL + 1) + rangeL);
}
return arr;
}
/**
* @Description:输出数组
* @param: arr 要进行输出的数组
* @return: void
*/
public static void printArray(Object [] arr){
for (int i = 0; i <arr.length ; i++) {
System.out.print(arr[i]);
System.out.print(" ");
}
}
/**
*
* 功能描述:
*
* @param: 获取当前系统时间 yyyy-MM-dd HH:mm:ss
* @return:
* @date: 2018/5/26 9:59
*/
public static String getCurrentDate(){
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String date = df.format(System.currentTimeMillis());
return date;
}
/**
*
* 功能描述:
*
* @param: date类 获取当前系统时间 yyyy-MM-dd HH:mm:ss
* @return:
* @date: 2018/5/26 10:39
*/
public static Date getCurrentDateToDate () {
DateFormat df = new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss" );
String date = df.format(System.currentTimeMillis());
Date d = null;
try {
d = df.parse( date.toString( ) );
} catch ( ParseException e ) {
e.printStackTrace( );
}
return d;
}
/**
* 增加时间单位:天
* @param day
* @return
*/
public static String getCurrentAddDay(int day) {
SimpleDateFormat sdf = new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss");
Calendar cal = Calendar.getInstance();
cal.setTime(new Date());
cal.add(Calendar.DATE, day);
return sdf.format(cal.getTime());
}
/**
* 增加时间单位:分钟
* @param minute
* @return
*/
public static String getCurrentAddMin(int minute) {
SimpleDateFormat sdf = new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss");
Calendar cal = Calendar.getInstance();
cal.setTime(new Date());
cal.add(Calendar.MINUTE, minute);
return sdf.format(cal.getTime());
}
/**
* 获取当前时间
* @return
*/
public static String getNowDateString ( ) {
Date d = new Date();
SimpleDateFormat sdf = new SimpleDateFormat( "yyyy-MM-dd");
return sdf.format( d );
}
/**
* 把Date转为String
* @param date
* @param format
* @return
*/
public static String getFormatTime(Date date, String format) {
SimpleDateFormat sdf = new SimpleDateFormat(format);
return sdf.format(date);
}
/**
* 增加时间单位:天
* @param day
* @return
*/
public static Date addDay(int day) {
Calendar cal = Calendar.getInstance();
cal.setTime(new Date());
cal.add(Calendar.DATE, day);
return cal.getTime();
}
/**
* 增加时间单位:天
* @param date
* @param day
* @return
*/
public static Date addDay(Date date, int day) {
Calendar cal = Calendar.getInstance();
cal.setTime(date);
cal.add(Calendar.DATE, day);
return cal.getTime();
}
/**
* 减去多少天
* @param date
* @param day
* @return
*/
public static Date minusDay(Date date, int day) {
return addDay(date, -day);
}
}