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

相关文章

JVM 的类初始化机制

前言 当你在 Java 程序中new对象时,有没有考虑过 JVM 是如何把静态的字节码(byte code)转化为运行时对象的呢,这个问题看似简单,但清楚的同学相信也不会太多,这篇文章首先介绍 JVM 类初始化的机制,然后给出几个易出错的实例来分析,帮助大家更好理解这个知识点。 JVM 将字节码转化为运行时对象分为三个阶段,分别是:loading 、Linking、initialization

Spring Security 基于表达式的权限控制

前言 spring security 3.0已经可以使用spring el表达式来控制授权,允许在表达式中使用复杂的布尔逻辑来控制访问的权限。 常见的表达式 Spring Security可用表达式对象的基类是SecurityExpressionRoot。 表达式描述hasRole([role])用户拥有制定的角色时返回true (Spring security默认会带有ROLE_前缀),去

浅析Spring Security认证过程

类图 为了方便理解Spring Security认证流程,特意画了如下的类图,包含相关的核心认证类 概述 核心验证器 AuthenticationManager 该对象提供了认证方法的入口,接收一个Authentiaton对象作为参数; public interface AuthenticationManager {Authentication authenticate(Authenti

Spring Security--Architecture Overview

1 核心组件 这一节主要介绍一些在Spring Security中常见且核心的Java类,它们之间的依赖,构建起了整个框架。想要理解整个架构,最起码得对这些类眼熟。 1.1 SecurityContextHolder SecurityContextHolder用于存储安全上下文(security context)的信息。当前操作的用户是谁,该用户是否已经被认证,他拥有哪些角色权限…这些都被保

Spring Security基于数据库验证流程详解

Spring Security 校验流程图 相关解释说明(认真看哦) AbstractAuthenticationProcessingFilter 抽象类 /*** 调用 #requiresAuthentication(HttpServletRequest, HttpServletResponse) 决定是否需要进行验证操作。* 如果需要验证,则会调用 #attemptAuthentica

Spring Security 从入门到进阶系列教程

Spring Security 入门系列 《保护 Web 应用的安全》 《Spring-Security-入门(一):登录与退出》 《Spring-Security-入门(二):基于数据库验证》 《Spring-Security-入门(三):密码加密》 《Spring-Security-入门(四):自定义-Filter》 《Spring-Security-入门(五):在 Sprin

Java架构师知识体认识

源码分析 常用设计模式 Proxy代理模式Factory工厂模式Singleton单例模式Delegate委派模式Strategy策略模式Prototype原型模式Template模板模式 Spring5 beans 接口实例化代理Bean操作 Context Ioc容器设计原理及高级特性Aop设计原理Factorybean与Beanfactory Transaction 声明式事物

服务器集群同步时间手记

1.时间服务器配置(必须root用户) (1)检查ntp是否安装 [root@node1 桌面]# rpm -qa|grep ntpntp-4.2.6p5-10.el6.centos.x86_64fontpackages-filesystem-1.41-1.1.el6.noarchntpdate-4.2.6p5-10.el6.centos.x86_64 (2)修改ntp配置文件 [r

Java进阶13讲__第12讲_1/2

多线程、线程池 1.  线程概念 1.1  什么是线程 1.2  线程的好处 2.   创建线程的三种方式 注意事项 2.1  继承Thread类 2.1.1 认识  2.1.2  编码实现  package cn.hdc.oop10.Thread;import org.slf4j.Logger;import org.slf4j.LoggerFactory

AI一键生成 PPT

AI一键生成 PPT 操作步骤 作为一名打工人,是不是经常需要制作各种PPT来分享我的生活和想法。但是,你们知道,有时候灵感来了,时间却不够用了!😩直到我发现了Kimi AI——一个能够自动生成PPT的神奇助手!🌟 什么是Kimi? 一款月之暗面科技有限公司开发的AI办公工具,帮助用户快速生成高质量的演示文稿。 无论你是职场人士、学生还是教师,Kimi都能够为你的办公文