小小工具类

根据hashCode生成uuid

/**
 * 根据自己需求生成相应长度的uuid字符串
 */
public static String getUUID(Integer length){
	int hashCode = UUID.randomUUID().toString().hashCode();
	return String.format("%0"+length+"d", hashCode<0?-hashCode:hashCode);
}

创建文件及文件夹

public static void createFile(String fileName){
	// 指定路径如果没有则创建并添加
	File file = new File("D:"+File.separator+"update"+File.separator+fileName);  //路径文件名
	//获得父目录
	File parentFile = file.getParentFile();
	//判断是否存在
	if(!parentFile.exists()){
	//创建父目录文件
	parentFile.mkdirs();
	}
	//创建文件
	file.createNewFile();
}

生成随机位数的验证码

public static String getRandomInteger(Integer length){
	StringBuffer sb = new StringBuffer();
	String [] code = {"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"};
	Random random = new Random();
	for(int i=0;i<length;i++){
		int index = random.nextInt(code.length);
		sb.append(code[index]);
	}
	return sb.toString();
}

获取年龄的工具类

    /**
	 * 根据时间戳计算年龄、
	 * @param birth
	 * @return
	 */
	public static int getAge(Long birth){
		Date date = new Date(birth*10001);
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		String string = sdf.format(date);
		return getAge(string);
	}
	
	/**
	 * 根据yyyy-MM-dd计算年龄
	 * @param birth
	 * @return
	 */
	public static int getAge(String birth){
		//获得出生年月日
		String [] births = birth.split("-");
		int birthYear = Integer.parseInt(births[0]);
		int birthMonth = Integer.parseInt(births[1]);
		int birthDay = Integer.parseInt(births[2]);
		//获取当前年月日
		Calendar cal = Calendar.getInstance();
		int nowYear = cal.get(Calendar.YEAR);
		int nowMonth = cal.get(Calendar.MONTH)+1;
		int nowDay = cal.get(Calendar.DATE);
		//获得年月日差
		int year = nowYear-birthYear;
		int month = nowMonth-birthMonth;
		int day = nowDay - birthDay;
		int age = year;
		if(year<0){ //选择了未来年份
			age = 0;
		}else if(year==0){  //同年可能是1岁也可能是0岁
			if(month<0){  //选择了未来月份
				age = 0;
			}else if(month==0){
				if(day<0){ //选择了未来的日子
					age = 0;
				}else{
					age = 1;
				}
			}else{    
				age = 1;
			}
		}else{ 
			if(month<0){  //未来月份
			}else if(month==0){
				if(day<0){
				}else{
					age += 1;
				}
			}else{
				age += 1;
			}
		}
		return age;
	}

时间工具类

    /**
     * 得到本月第一天
     * yyyy-MM-dd
     * @return 
     */
    public static String getMonthFirstDay() {
    	Calendar calendar = Calendar.getInstance();
    	calendar.set(Calendar.DAY_OF_MONTH,calendar.getActualMinimum(Calendar.DAY_OF_MONTH));
    	SimpleDateFormat sdf= new SimpleDateFormat("yyyy-MM-dd");
    	return  sdf.format(calendar.getTime());
    	}
    
    /**
     * 得到前一天日期
     * yyyy-MM-dd
     * @return 
     */
    public static String getYesterday(){
    	Calendar calendar = Calendar.getInstance();
    	calendar.add(Calendar.DATE, -1);
    	SimpleDateFormat sdf= new SimpleDateFormat("yyyy-MM-dd");
    	return sdf.format(calendar.getTime());
    }
    
    /**
     * 得到当前日期
     * yyyy-MM-dd
     * @return
     */
    public static String getToday(){
    	return new SimpleDateFormat("yyyy-MM-dd").format(new Date());
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值