2.JAVA中常用的工具方法

2.JAVA中常用的工具方法,第1张

2.JAVA中常用的工具方法 1.日期常用
1.1 获取当前日期
 		 //获取当前时间 2021-11-26 09:23:21
        System.out.println(LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
        //获取当前年YYYY-MM-DD
        System.out.println(LocalDate.now());
        //获取当前时间HH:mm:ss
        System.out.println(LocalTime.now().format(DateTimeFormatter.ofPattern("HH:mm:ss")));
        //获取年月日   时分秒  当前时间星期几
        System.out.println("年:" + LocalDate.now().getYear());
        System.out.println("月:" + LocalDate.now().getMonthValue());
        System.out.println("日:" + LocalDate.now().getDayOfMonth());
        System.out.println("小时:" + LocalDateTime.now().get(ChronoField.HOUR_OF_DAY));
        System.out.println("分:" + LocalDateTime.now().get(ChronoField.MINUTE_OF_HOUR));
        System.out.println("秒:" + LocalDateTime.now().get(ChronoField.SECOND_OF_MINUTE));
        System.out.println("星期几:" + LocalDateTime.now().get(ChronoField.DAY_OF_WEEK));
        System.out.println("判断是否是润年(返回Boolean类型):" + LocalDate.now().isLeapYear());
        
        //得到一个localData格式时间日期
        LocalDate date4 = LocalDate.of(2021, 11, 26);
        LocalTime date2 = LocalTime.of(12, 30, 00);
        //比较两个日期
        System.out.println(date4.equals(date2));
1.2对日期进行 *** 作
        //加天  + 加日  +加年
        System.out.println(LocalDateTime.now().plusHours(2).format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
        System.out.println(LocalDateTime.now().plusDays(2).format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
        System.out.println(LocalDateTime.now().plusYears(2).format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
        //加1星期也就是加7天
        System.out.println(LocalDateTime.now().plusWeeks(1).format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));

        LocalDateTime date1 = LocalDateTime.of(2021, 11, 26, 12, 30, 00);
        LocalDateTime date3 = LocalDateTime.of(2021, 9, 26, 12, 30, 00);
        //date1 是在date3之后 返回true
        System.out.println(date1.isAfter(date3));
        //date1 不在date3之前 返回false
        System.out.println(date1.isBefore(date3));

        //相差 年  月  日  小时 分钟  秒
        System.out.println("ChronoUnit.DAYS.between计算出的天数时间差:" + ChronoUnit.YEARS.between(date3, date1));
        System.out.println("ChronoUnit.DAYS.between计算出的天数时间差:" + ChronoUnit.MONTHS.between(date3, date1));
        System.out.println("ChronoUnit.DAYS.between计算出的天数时间差:" + ChronoUnit.DAYS.between(date3, date1));
        System.out.println("ChronoUnit.DAYS.between计算出的天数时间差:" + ChronoUnit.HOURS.between(date3, date1));
        System.out.println("ChronoUnit.DAYS.between计算出的天数时间差:" + ChronoUnit.MINUTES.between(date3, date1));
        System.out.println("ChronoUnit.DAYS.between计算出的天数时间差:" + ChronoUnit.SECONDS.between(date3, date1));
1.3日期的一些常用转换
  		//String-->LocalDateTime
        LocalDateTime parse = LocalDateTime.parse("2021-11-12 12:30:10", DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        //long--->LocalDateTime
        System.out.println(LocalDateTime.ofInstant(Instant.ofEpochMilli(1637896436195L), ZoneId.systemDefault()));
        //LocalDateTime-->long
        System.out.println(Timestamp.valueOf(LocalDateTime.now()).getTime());
        //LocalDateTime-->Date
        System.out.println(Date.from(LocalDateTime.now().atZone(ZoneId.systemDefault()).toInstant()));
2.密码加密 *** 作
2.1MD5+随机盐密码加密

用的依赖

		
			cn.hutool
			hutool-all
			5.6.5
		
public class PasswordUtil {

    
    private static final int PASSWORD_AND_SALT_LENGTH = 32;

    
    public static String createRandomKet() {
        return IdUtil.simpleUUID();
    }

    
    public static String generate(String password, String salt) {
        password = DigestUtil.md5Hex(password + salt);
        char[] md5 = new char[PASSWORD_AND_SALT_LENGTH];
        for (int i = 0; i < PASSWORD_AND_SALT_LENGTH; i++) {
            md5[i] = password.charAt(i / 2);
            md5[++i] = salt.charAt(i / 2);
        }
        return new String(md5);
    }


    
    public static boolean checkPassword(String md5Password, String password, String salt) {
        if (md5Password.equals(generate(password, salt))) {
            return true;
        }
        return false;
    }
    
}

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5606256.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-15
下一篇 2022-12-15

发表评论

登录后才能评论

评论列表(0条)

保存