Java生成随机数工具类,进制之间的转换工具类,获取指定时间,时间格式转换工具类

本文主要是介绍Java生成随机数工具类,进制之间的转换工具类,获取指定时间,时间格式转换工具类,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

废话不多说,贡献一下code

1.编号生成工具


import org.apache.commons.lang3.StringUtils;import java.math.BigInteger;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;/*** 编号生成工具*/
@SuppressWarnings("Duplicates")
public abstract class CodeBuilder {/*** 生成新的流水编号** @return 20位字符串*/public static final String newTxCode() {// 年月日时分秒毫秒(15位)String time = new SimpleDateFormat("yyMMddHHmmssSSS").format(new Date());StringBuilder builder = new StringBuilder(time);Random random = new Random();// 随机数(5位)for (int i = 0; i < 5; i++) {int nextInt = random.nextInt(10);builder.append(nextInt);}return builder.toString();}public static final String genNextRadixCode(String precode) throws Exception {if (StringUtils.isBlank(precode)) {return "01";}int n=Integer.parseUnsignedInt(precode, 36);n++;return ten2radix(n,36).length()==1?"0"+ten2radix(n,36):ten2radix(n,36);}public static void main(String[] args) {try {System.out.println(genNextRadixCode("ZX"));} catch (Exception e) {e.printStackTrace();}}/*** baseString 递归调用** @param num  十进制数* @param base 要转换成的进制数*/public static String ten2radix(int num, int base) {String str = "", digit = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";if (num == 0) {return "";} else {str = ten2radix(num / base, base);return str + digit.charAt(num % base);}}/*** baseString 递归调用** @param num  十进制数* @param base 要转换成的进制数*/public static String ten2radix(BigInteger num, int base) {String str = "", digit = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";if (num.shortValue() == 0) {return "";} else {BigInteger valueOf = BigInteger.valueOf(base);str = ten2radix(num.divide(valueOf), base);return str + digit.charAt(num.mod(valueOf).shortValue());}}//随机生成指定长度的代码,包含数字public static final String generateRandomNumber(int length) {String allChar = "0123456789";StringBuffer sb = new StringBuffer();Random random = new Random();for (int i = 0; i < length; i++) {sb.append(allChar.charAt(random.nextInt(allChar.length())));}return sb.toString();}}

2.日期工具


import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;
import java.util.regex.Pattern;public class DateUtils {private static Logger logger = LoggerFactory.getLogger(DateUtils.class);private static final String DATE_FORMAT = "yyyy-MM-dd";private static final String DATE_TIME_FORMAT_1 = "yyyy-MM-dd'T'HH:mm:ss";public static final String DATE_TIME_FORMAT_2 = "yyyy-MM-dd HH:mm:ss";public static final String DATE_TIME_FORMAT_3 = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'";public static final String DATE_TIME_FORMAT_4 = "MMddHHmmssSSS";private static final Pattern DATE_PATTERN = Pattern.compile("^\\d{4}-\\d{2}-\\d{2}$");private static final Pattern DATE_TIME_PATTERN_1 = Pattern.compile("^\\d{4}-\\d{2}-\\d{2}[T]\\d{2}:\\d{2}:\\d{2}$");private static final Pattern DATE_TIME_PATTERN_2 = Pattern.compile("^\\d{4}-\\d{2}-\\d{2}\\s\\d{2}:\\d{2}:\\d{2}$");public static Date addDays(Date start, int addDay) {Date date = getDateBegin(start);Calendar cs = Calendar.getInstance();cs.setTime(date);cs.set(Calendar.DAY_OF_MONTH, cs.get(Calendar.DAY_OF_MONTH) + addDay);//让日期加1return cs.getTime();}public static Date parse(String dateString) {Date date = null;if (!StringUtils.isBlank(dateString)) {try {if (DATE_PATTERN.matcher(dateString).find()) {date = new SimpleDateFormat(DATE_FORMAT).parse(dateString);} else if (DATE_TIME_PATTERN_1.matcher(dateString).find()) {date = new SimpleDateFormat(DATE_TIME_FORMAT_1).parse(dateString);} else if (DATE_TIME_PATTERN_2.matcher(dateString).find()) {date = new SimpleDateFormat(DATE_TIME_FORMAT_2).parse(dateString);}} catch (ParseException e) {logger.debug("Exception: {}", e);}}return date;}/*** 将日期字符串转换成指定格式的date** @param 日期字符串* @param 日期格式* @return*/public static Date formatStr(String str, String formatStr) {Date date = null;if (str != null) {DateFormat sdf = new SimpleDateFormat(formatStr);try {if (formatStr.equals(DATE_TIME_FORMAT_3)) {sdf.setTimeZone(TimeZone.getTimeZone("UTC"));}date = sdf.parse(str);} catch (ParseException e) {e.printStackTrace();}}return date;}/*** 将日期转换成指定格式字符串** @param date* @param str* @return*/public static String formatDate(Date date, String str) {SimpleDateFormat sdf = new SimpleDateFormat(str);return sdf.format(date);}/*** 获取月日时分秒时间戳字符串** @return*/public static String getTimeString() {Date now = new Date();DateFormat sdf = new SimpleDateFormat(DATE_TIME_FORMAT_4);return sdf.format(now);}public static Date getDateBegin(Date date) {if (date == null) {date = new Date();}Calendar calendar = Calendar.getInstance();calendar.setTime(date);calendar.set(Calendar.HOUR_OF_DAY, 0);calendar.set(Calendar.MINUTE, 0);calendar.set(Calendar.SECOND, 0);calendar.set(Calendar.MILLISECOND, 0);return calendar.getTime();}public static Date getDateEnd(Date date) {if (date == null) {date = new Date();}Calendar calendar = Calendar.getInstance();calendar.setTime(date);calendar.set(Calendar.HOUR_OF_DAY, 23);calendar.set(Calendar.MINUTE, 59);calendar.set(Calendar.SECOND, 59);calendar.set(Calendar.MILLISECOND, 999);return calendar.getTime();}}

3.时间工具


import java.util.Calendar;
import java.util.Date;public class RemindDateUtils {/** * 获得本周的第一天,周一 * * @return */ public static Date getCurrentWeekDayStartTime() { Calendar c = Calendar.getInstance(); int day_of_week = c.get(Calendar.DAY_OF_WEEK) - 1;if (day_of_week == 0) {day_of_week = 7;}c.add(Calendar.DATE, -day_of_week + 1);c.set(Calendar.HOUR_OF_DAY, 0);c.set(Calendar.MINUTE, 0);c.set(Calendar.SECOND, 0);return c.getTime();} /** * 获得本周的最后一天,周日 * * @return */ public static Date getCurrentWeekDayEndTime() { Calendar c = Calendar.getInstance(); int weekday = c.get(Calendar.DAY_OF_WEEK); c.add(Calendar.DATE, 8 - weekday);c.set(Calendar.HOUR_OF_DAY, 23);c.set(Calendar.MINUTE, 59);c.set(Calendar.SECOND, 59);return c.getTime();} /** * 获得本天的开始时间,即2012-01-01 00:00:00 * * @return */ public static Date getCurrentDayStartTime() { Calendar c = Calendar.getInstance(); c.set(Calendar.HOUR_OF_DAY, 0);c.set(Calendar.MINUTE, 0);c.set(Calendar.SECOND, 0);return c.getTime();} /** * 获得本天的结束时间,即2012-01-01 23:59:59 * * @return */ public static Date getCurrentDayEndTime() { Calendar c = Calendar.getInstance(); c.set(Calendar.HOUR_OF_DAY, 23);c.set(Calendar.MINUTE, 59);c.set(Calendar.SECOND, 59);return c.getTime();} /** * 获得本小时的开始时间,即2012-01-01 00:00:00 * * @return */ public static Date getCurrentHourStartTime() { Calendar c = Calendar.getInstance(); c.set(Calendar.MINUTE, 0);c.set(Calendar.SECOND, 0);return c.getTime();} /** * 获得本小时的结束时间,即2012-01-01 23:59:59 * * @return */ public static Date getCurrentHourEndTime() { Calendar c = Calendar.getInstance(); c.set(Calendar.MINUTE, 59);c.set(Calendar.SECOND, 59);return c.getTime();} /** * 获得本月的开始时间,即2012-01-01 00:00:00 * * @return */ public static Date getCurrentMonthStartTime() { Calendar c = Calendar.getInstance(); c.set(Calendar.DATE, 1); c.set(Calendar.HOUR_OF_DAY, 0);c.set(Calendar.MINUTE, 0);c.set(Calendar.SECOND, 0);return c.getTime();} /** * 当前月的结束时间,即2012-01-31 23:59:59 * * @return */ public static Date getCurrentMonthEndTime() { Calendar c = Calendar.getInstance(); c.set(Calendar.DATE, 1); c.add(Calendar.MONTH, 1); c.add(Calendar.DATE, -1); c.set(Calendar.HOUR_OF_DAY, 23);c.set(Calendar.MINUTE, 59);c.set(Calendar.SECOND, 59);return c.getTime();} /** * 当前年的开始时间,即2012-01-01 00:00:00 * * @return */ public static Date getCurrentYearStartTime() { Calendar c = Calendar.getInstance(); c.set(Calendar.MONTH, 0); c.set(Calendar.DATE, 1); c.set(Calendar.HOUR_OF_DAY, 0);c.set(Calendar.MINUTE, 0);c.set(Calendar.SECOND, 0);return c.getTime(); } /** * 当前年的结束时间,即2012-12-31 23:59:59 * * @return */ public static Date getCurrentYearEndTime() { Calendar c = Calendar.getInstance(); c.set(Calendar.MONTH, 11); c.set(Calendar.DATE, 31); c.set(Calendar.HOUR_OF_DAY, 23);c.set(Calendar.MINUTE, 59);c.set(Calendar.SECOND, 59);return c.getTime();} /** * 当前季度的开始时间,即2012-01-1 00:00:00 * * @return */ public static Date getCurrentQuarterStartTime() { Calendar c = Calendar.getInstance();int currentMonth = c.get(Calendar.MONTH) + 1; if (currentMonth >= 1 && currentMonth <= 3) c.set(Calendar.MONTH, 0); else if (currentMonth >= 4 && currentMonth <= 6) c.set(Calendar.MONTH, 3); else if (currentMonth >= 7 && currentMonth <= 9) c.set(Calendar.MONTH, 4); else if (currentMonth >= 10 && currentMonth <= 12) c.set(Calendar.MONTH, 9); c.set(Calendar.DATE, 1); c.set(Calendar.HOUR_OF_DAY, 0);c.set(Calendar.MINUTE, 0);c.set(Calendar.SECOND, 0);return c.getTime(); } /** * 当前季度的结束时间,即2012-03-31 23:59:59 * * @return */ public static Date getCurrentQuarterEndTime() { Calendar c = Calendar.getInstance(); int currentMonth = c.get(Calendar.MONTH) + 1; if (currentMonth >= 1 && currentMonth <= 3) { c.set(Calendar.MONTH, 2); c.set(Calendar.DATE, 31); } else if (currentMonth >= 4 && currentMonth <= 6) { c.set(Calendar.MONTH, 5); c.set(Calendar.DATE, 30); } else if (currentMonth >= 7 && currentMonth <= 9) { c.set(Calendar.MONTH,8); c.set(Calendar.DATE, 30); } else if (currentMonth >= 10 && currentMonth <= 12) { c.set(Calendar.MONTH, 11); c.set(Calendar.DATE, 31); } c.set(Calendar.HOUR_OF_DAY, 23);c.set(Calendar.MINUTE, 59);c.set(Calendar.SECOND, 59);return c.getTime();} /** * 获取前/后半年的开始时间 * @return */ public static Date getHalfYearStartTime(){ Calendar c = Calendar.getInstance(); int currentMonth = c.get(Calendar.MONTH) + 1; if (currentMonth >= 1 && currentMonth <= 6){ c.set(Calendar.MONTH, 0); }else if (currentMonth >= 7 && currentMonth <= 12){ c.set(Calendar.MONTH, 6); } c.set(Calendar.DATE, 1); c.set(Calendar.HOUR_OF_DAY, 0);c.set(Calendar.MINUTE, 0);c.set(Calendar.SECOND, 0);return c.getTime();} /** * 获取前/后半年的结束时间 * @return */ public static Date getHalfYearEndTime(){ Calendar c = Calendar.getInstance(); int currentMonth = c.get(Calendar.MONTH) + 1; if (currentMonth >= 1 && currentMonth <= 6){ c.set(Calendar.MONTH, 5); c.set(Calendar.DATE, 30); }else if (currentMonth >= 7 && currentMonth <= 12){ c.set(Calendar.MONTH, 11); c.set(Calendar.DATE, 31); } c.set(Calendar.HOUR_OF_DAY, 23);c.set(Calendar.MINUTE, 59);c.set(Calendar.SECOND, 59);return c.getTime();} 
} 

这篇关于Java生成随机数工具类,进制之间的转换工具类,获取指定时间,时间格式转换工具类的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1142525

相关文章

SpringBoot 获取请求参数的常用注解及用法

《SpringBoot获取请求参数的常用注解及用法》SpringBoot通过@RequestParam、@PathVariable等注解支持从HTTP请求中获取参数,涵盖查询、路径、请求体、头、C... 目录SpringBoot 提供了多种注解来方便地从 HTTP 请求中获取参数以下是主要的注解及其用法:1

HTTP 与 SpringBoot 参数提交与接收协议方式

《HTTP与SpringBoot参数提交与接收协议方式》HTTP参数提交方式包括URL查询、表单、JSON/XML、路径变量、头部、Cookie、GraphQL、WebSocket和SSE,依据... 目录HTTP 协议支持多种参数提交方式,主要取决于请求方法(Method)和内容类型(Content-Ty

深度解析Java @Serial 注解及常见错误案例

《深度解析Java@Serial注解及常见错误案例》Java14引入@Serial注解,用于编译时校验序列化成员,替代传统方式解决运行时错误,适用于Serializable类的方法/字段,需注意签... 目录Java @Serial 注解深度解析1. 注解本质2. 核心作用(1) 主要用途(2) 适用位置3

深入浅出Spring中的@Autowired自动注入的工作原理及实践应用

《深入浅出Spring中的@Autowired自动注入的工作原理及实践应用》在Spring框架的学习旅程中,@Autowired无疑是一个高频出现却又让初学者头疼的注解,它看似简单,却蕴含着Sprin... 目录深入浅出Spring中的@Autowired:自动注入的奥秘什么是依赖注入?@Autowired

Spring 依赖注入与循环依赖总结

《Spring依赖注入与循环依赖总结》这篇文章给大家介绍Spring依赖注入与循环依赖总结篇,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧... 目录1. Spring 三级缓存解决循环依赖1. 创建UserService原始对象2. 将原始对象包装成工

Java中如何正确的停掉线程

《Java中如何正确的停掉线程》Java通过interrupt()通知线程停止而非强制,确保线程自主处理中断,避免数据损坏,线程池的shutdown()等待任务完成,shutdownNow()强制中断... 目录为什么不强制停止为什么 Java 不提供强制停止线程的能力呢?如何用interrupt停止线程s

Python中Json和其他类型相互转换的实现示例

《Python中Json和其他类型相互转换的实现示例》本文介绍了在Python中使用json模块实现json数据与dict、object之间的高效转换,包括loads(),load(),dumps()... 项目中经常会用到json格式转为object对象、dict字典格式等。在此做个记录,方便后续用到该方

SpringBoot请求参数传递与接收示例详解

《SpringBoot请求参数传递与接收示例详解》本文给大家介绍SpringBoot请求参数传递与接收示例详解,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋... 目录I. 基础参数传递i.查询参数(Query Parameters)ii.路径参数(Path Va

SpringBoot路径映射配置的实现步骤

《SpringBoot路径映射配置的实现步骤》本文介绍了如何在SpringBoot项目中配置路径映射,使得除static目录外的资源可被访问,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一... 目录SpringBoot路径映射补:springboot 配置虚拟路径映射 @RequestMapp

Java MCP 的鉴权深度解析

《JavaMCP的鉴权深度解析》文章介绍JavaMCP鉴权的实现方式,指出客户端可通过queryString、header或env传递鉴权信息,服务器端支持工具单独鉴权、过滤器集中鉴权及启动时鉴权... 目录一、MCP Client 侧(负责传递,比较简单)(1)常见的 mcpServers json 配置