工具类积累 发表于 2022-07-02- 更新于 2022-08-14 1.6k- 8m 后端-工具类MD5工具类12345678910111213141516171819202122232425262728293031package com.itheima.utils;import java.math.BigInteger;import java.security.MessageDigest;import java.security.NoSuchAlgorithmException;public class MD5Utils { /** * 使用md5的算法进行加密 */ public static String md5(String plainText) { byte[] secretBytes = null; try { secretBytes = MessageDigest.getInstance("md5").digest( plainText.getBytes()); } catch (NoSuchAlgorithmException e) { throw new RuntimeException("没有md5这个算法!"); } String md5code = new BigInteger(1, secretBytes).toString(16);// 16进制数字 // 如果生成数字未满32位,需要前面补0 for (int i = 0; i < 32 - md5code.length(); i++) { md5code = "0" + md5code; } return md5code; } public static void main(String[] args) { System.out.println(md5("1234")); }} 日期工具类123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296package com.itheima.utils;import java.text.SimpleDateFormat;import java.util.*;/** * 日期操作工具类 */public class DateUtils { /** * 日期转换- String -> Date * * @param dateString 字符串时间 * @return Date类型信息 * @throws Exception 抛出异常 */ public static Date parseString2Date(String dateString) throws Exception { if (dateString == null) { return null; } return parseString2Date(dateString, "yyyy-MM-dd"); } /** * 日期转换- String -> Date * * @param dateString 字符串时间 * @param pattern 格式模板 * @return Date类型信息 * @throws Exception 抛出异常 */ public static Date parseString2Date(String dateString, String pattern) throws Exception { if (dateString == null) { return null; } SimpleDateFormat sdf = new SimpleDateFormat(pattern); Date date = sdf.parse(dateString); return date; } /** * 日期转换 Date -> String * * @param date Date类型信息 * @return 字符串时间 * @throws Exception 抛出异常 */ public static String parseDate2String(Date date) throws Exception { if (date == null) { return null; } return parseDate2String(date, "yyyy-MM-dd"); } /** * 日期转换 Date -> String * * @param date Date类型信息 * @param pattern 格式模板 * @return 字符串时间 * @throws Exception 抛出异常 */ public static String parseDate2String(Date date, String pattern) throws Exception { if (date == null) { return null; } SimpleDateFormat sdf = new SimpleDateFormat(pattern); String strDate = sdf.format(date); return strDate; } /** * 获取当前日期的本周一是几号 * * @return 本周一的日期 */ public static Date getThisWeekMonday() { Calendar cal = Calendar.getInstance(); cal.setTime(new Date()); // 获得当前日期是一个星期的第几天 int dayWeek = cal.get(Calendar.DAY_OF_WEEK); if (1 == dayWeek) { cal.add(Calendar.DAY_OF_MONTH, -1); } // 设置一个星期的第一天,按中国的习惯一个星期的第一天是星期一 cal.setFirstDayOfWeek(Calendar.MONDAY); // 获得当前日期是一个星期的第几天 int day = cal.get(Calendar.DAY_OF_WEEK); // 根据日历的规则,给当前日期减去星期几与一个星期第一天的差值 cal.add(Calendar.DATE, cal.getFirstDayOfWeek() - day); return cal.getTime(); } /** * 获取当前日期周的最后一天 * * @return 当前日期周的最后一天 */ public static Date getSundayOfThisWeek() { Calendar c = Calendar.getInstance(); int dayOfWeek = c.get(Calendar.DAY_OF_WEEK) - 1; if (dayOfWeek == 0) { dayOfWeek = 7; } c.add(Calendar.DATE, -dayOfWeek + 7); return c.getTime(); } /** * 根据日期区间获取月份列表 * * @param minDate 开始时间 * @param maxDate 结束时间 * @return 月份列表 * @throws Exception */ public static List<String> getMonthBetween(String minDate, String maxDate, String format) throws Exception { ArrayList<String> result = new ArrayList<>(); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM"); Calendar min = Calendar.getInstance(); Calendar max = Calendar.getInstance(); min.setTime(sdf.parse(minDate)); min.set(min.get(Calendar.YEAR), min.get(Calendar.MONTH), 1); max.setTime(sdf.parse(maxDate)); max.set(max.get(Calendar.YEAR), max.get(Calendar.MONTH), 2); SimpleDateFormat sdf2 = new SimpleDateFormat(format); Calendar curr = min; while (curr.before(max)) { result.add(sdf2.format(curr.getTime())); curr.add(Calendar.MONTH, 1); } return result; } /** * 根据日期获取年度中的周索引 * * @param date 日期 * @return 周索引 * @throws Exception */ public static Integer getWeekOfYear(String date) throws Exception { Date useDate = parseString2Date(date); Calendar cal = Calendar.getInstance(); cal.setTime(useDate); return cal.get(Calendar.WEEK_OF_YEAR); } /** * 根据年份获取年中周列表 * * @param year 年分 * @return 周列表 * @throws Exception */ public static Map<Integer, String> getWeeksOfYear(String year) throws Exception { Date useDate = parseString2Date(year, "yyyy"); Calendar cal = Calendar.getInstance(); cal.setTime(useDate); //获取年中周数量 int weeksCount = cal.getWeeksInWeekYear(); Map<Integer, String> mapWeeks = new HashMap<>(55); for (int i = 0; i < weeksCount; i++) { cal.get(Calendar.DAY_OF_YEAR); mapWeeks.put(i + 1, parseDate2String(getFirstDayOfWeek(cal.get(Calendar.YEAR), i))); } return mapWeeks; } /** * 获取某年的第几周的开始日期 * * @param year 年分 * @param week 周索引 * @return 开始日期 * @throws Exception */ public static Date getFirstDayOfWeek(int year, int week) throws Exception { Calendar c = new GregorianCalendar(); c.set(Calendar.YEAR, year); c.set(Calendar.MONTH, Calendar.JANUARY); c.set(Calendar.DATE, 1); Calendar cal = (GregorianCalendar) c.clone(); cal.add(Calendar.DATE, week * 7); return getFirstDayOfWeek(cal.getTime()); } /** * 获取某年的第几周的结束日期 * * @param year 年份 * @param week 周索引 * @return 结束日期 * @throws Exception */ public static Date getLastDayOfWeek(int year, int week) throws Exception { Calendar c = new GregorianCalendar(); c.set(Calendar.YEAR, year); c.set(Calendar.MONTH, Calendar.JANUARY); c.set(Calendar.DATE, 1); Calendar cal = (GregorianCalendar) c.clone(); cal.add(Calendar.DATE, week * 7); return getLastDayOfWeek(cal.getTime()); } /** * 获取当前时间所在周的开始日期 * * @param date 当前时间 * @return 开始时间 */ public static Date getFirstDayOfWeek(Date date) { Calendar c = new GregorianCalendar(); c.setFirstDayOfWeek(Calendar.SUNDAY); c.setTime(date); c.set(Calendar.DAY_OF_WEEK, c.getFirstDayOfWeek()); return c.getTime(); } /** * 获取当前时间所在周的结束日期 * * @param date 当前时间 * @return 结束日期 */ public static Date getLastDayOfWeek(Date date) { Calendar c = new GregorianCalendar(); c.setFirstDayOfWeek(Calendar.SUNDAY); c.setTime(date); c.set(Calendar.DAY_OF_WEEK, c.getFirstDayOfWeek() + 6); return c.getTime(); } //获得上周一的日期 public static Date geLastWeekMonday(Date date) { Calendar cal = Calendar.getInstance(); cal.setTime(getThisWeekMonday(date)); cal.add(Calendar.DATE, -7); return cal.getTime(); } //获得本周一的日期 public static Date getThisWeekMonday(Date date) { Calendar cal = Calendar.getInstance(); cal.setTime(date); // 获得当前日期是一个星期的第几天 int dayWeek = cal.get(Calendar.DAY_OF_WEEK); if (1 == dayWeek) { cal.add(Calendar.DAY_OF_MONTH, -1); } // 设置一个星期的第一天,按中国的习惯一个星期的第一天是星期一 cal.setFirstDayOfWeek(Calendar.MONDAY); // 获得当前日期是一个星期的第几天 int day = cal.get(Calendar.DAY_OF_WEEK); // 根据日历的规则,给当前日期减去星期几与一个星期第一天的差值 cal.add(Calendar.DATE, cal.getFirstDayOfWeek() - day); return cal.getTime(); } //获得下周一的日期 public static Date getNextWeekMonday(Date date) { Calendar cal = Calendar.getInstance(); cal.setTime(getThisWeekMonday(date)); cal.add(Calendar.DATE, 7); return cal.getTime(); } //获得今天日期 public static Date getToday(){ return new Date(); } //获得本月一日的日期 public static Date getFirstDay4ThisMonth(){ Calendar calendar = Calendar.getInstance(); calendar.set(Calendar.DAY_OF_MONTH,1); return calendar.getTime(); } public static void main(String[] args) { try { System.out.println("本周一" + parseDate2String(getThisWeekMonday())); System.out.println("本月一日" + parseDate2String(getFirstDay4ThisMonth())); } catch (Exception e) { e.printStackTrace(); } }} 随机生成验证码工具类12345678910111213141516171819202122232425262728293031323334353637383940414243package com.itheima.utils;import java.util.Random;/** * 随机生成验证码工具类 */public class ValidateCodeUtils { /** * 随机生成验证码 * @param length 长度为4位或者6位 * @return */ public static Integer generateValidateCode(int length){ Integer code =null; if(length == 4){ code = new Random().nextInt(9999);//生成随机数,最大为9999 if(code < 1000){ code = code + 1000;//保证随机数为4位数字 } }else if(length == 6){ code = new Random().nextInt(999999);//生成随机数,最大为999999 if(code < 100000){ code = code + 100000;//保证随机数为6位数字 } }else{ throw new RuntimeException("只能生成4位或6位数字验证码"); } return code; } /** * 随机生成指定长度字符串验证码 * @param length 长度 * @return */ public static String generateValidateCode4String(int length){ Random rdm = new Random(); String hash1 = Integer.toHexString(rdm.nextInt()); String capstr = hash1.substring(0, length); return capstr; }} I'm so cute. Please give me money.本文作者:丸了鸭本文链接:https://www.kdy.icu/posts/3243178616.html版权声明:本博客所有文章除特别声明外,均默认采用 许可协议。