本文主要是介绍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生成随机数工具类,进制之间的转换工具类,获取指定时间,时间格式转换工具类的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!