/**
* 获取指定长度的随机字符串
* @param length
* @return
*/
public static String getMath(int length){
char cha[]={
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j',
'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't',
'u', 'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D',
'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N',
'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'
};;
String str="";
int i=length;
while (i>0){
int in=(int)(Math.random()*(cha.length));
str+=cha[in];
i--;
}
return str;
}
/**
* 补位0 (1补3位 输出003)
* @param num
* @param length
* @return
*/
public static String addZeroForNum(String num,int length){
while (num.length()<length){
StringBuffer stringBuffer=new StringBuffer();
stringBuffer.append("0").append(num);
num=stringBuffer.toString();
}
return num;
}
时间:
/**
* 对日期的【秒】进行加/减
*
* @param date 日期
* @param seconds 秒数,负数为减
* @return 加/减几秒后的日期
*/
public static Date addDateSeconds(Date date, int seconds) {
DateTime dateTime = new DateTime(date);
return dateTime.plusSeconds(seconds).toDate();
}
/**
* 对日期的【分钟】进行加/减
*
* @param date 日期
* @param minutes 分钟数,负数为减
* @return 加/减几分钟后的日期
*/
public static Date addDateMinutes(Date date, int minutes) {
DateTime dateTime = new DateTime(date);
return dateTime.plusMinutes(minutes).toDate();
}
/**
* 对日期的【小时】进行加/减
*
* @param date 日期
* @param hours 小时数,负数为减
* @return 加/减几小时后的日期
*/
public static Date addDateHours(Date date, int hours) {
DateTime dateTime = new DateTime(date);
return dateTime.plusHours(hours).toDate();
}
/**
* 对日期的【天】进行加/减
*
* @param date 日期
* @param days 天数,负数为减
* @return 加/减几天后的日期
*/
public static Date addDateDays(Date date, int days) {
DateTime dateTime = new DateTime(date);
return dateTime.plusDays(days).toDate();
}
/**
* 对日期的【周】进行加/减
*
* @param date 日期
* @param weeks 周数,负数为减
* @return 加/减几周后的日期
*/
public static Date addDateWeeks(Date date, int weeks) {
DateTime dateTime = new DateTime(date);
return dateTime.plusWeeks(weeks).toDate();
}
/**
* 对日期的【月】进行加/减
*
* @param date 日期
* @param months 月数,负数为减
* @return 加/减几月后的日期
*/
public static Date addDateMonths(Date date, int months) {
DateTime dateTime = new DateTime(date);
return dateTime.plusMonths(months).toDate();
}
/**
* 对日期的【年】进行加/减
*
* @param date 日期
* @param years 年数,负数为减
* @return 加/减几年后的日期
*/
public static Date addDateYears(Date date, int years) {
DateTime dateTime = new DateTime(date);
return dateTime.plusYears(years).toDate();
}
//计算两个日期相差年数
public static int yearDateDiff(Date startDate, Date endDate) {
Calendar calBegin = Calendar.getInstance(); //获取日历实例
Calendar calEnd = Calendar.getInstance();
calBegin.setTime(startDate); //字符串按照指定格式转化为日期
calEnd.setTime(endDate);
return calEnd.get(Calendar.YEAR) - calBegin.get(Calendar.YEAR);
}
/**
* 将时间格式转换为时间戳
*
* @param s 2018-12-12 | 2018-12-12 09:12:34
* @param isDateTime
* @return
*/
public static long dateToStamp(String s, boolean isDateTime) {
String pattern = isDateTime ? TimeUtil.DATE_TIME_FULL_PATTERN : TimeUtil.DATE_PATTERN;
Date date = TimeUtil.stringToDate(s, pattern);
long ts = date.getTime();
return ts / 1000;
}
/**
* 将时间格式转换为时间戳
*
* @param s 时间
* @param pattern 时间格式
* @return
*/
public static long dateToStamp(String s, String pattern) {
Date date = TimeUtil.stringToDate(s, pattern);
long ts = date.getTime();
return ts / 1000;
}
/**
* 获取当前时间戳
*
* @return
*/
public static int getCurrentUnixTimestamp() {
long millis = System.currentTimeMillis() / 1000;
return (int) millis;
}
/**
* 获得当天0点时间
*
* @return int
*/
public static int getTodayTimeStampMorning() {
Calendar cal = Calendar.getInstance();
cal.set(Calendar.HOUR_OF_DAY, 0);
cal.set(Calendar.SECOND, 0);
cal.set(Calendar.MINUTE, 0);
cal.set(Calendar.MILLISECOND, 0);
return (int) (cal.getTimeInMillis() / 1000);
}
/**
* 获得当天24点时间
*
* @return int
*/
public static int getTodayTimeStampNight() {
Calendar cal = Calendar.getInstance();
cal.set(Calendar.HOUR_OF_DAY, 24);
cal.set(Calendar.SECOND, 0);
cal.set(Calendar.MINUTE, 0);
cal.set(Calendar.MILLISECOND, 0);
return (int) (cal.getTimeInMillis() / 1000);
}