Commit a5cf1032 authored by zhouleilei's avatar zhouleilei

计划调度工具类

parent 3bf9fc65
package net.wanji.common.utils.tool;
/**
* @Author: zhouleilei
* @date: 2024/11/09 15:15
* @Version: 1.0
* @Description: Schedule Util
*/
public class ScheduleUtil {
/**
* @return java.lang.String
* @Description 根据类型和值返回对应的月、日、星期
* @Param value
* @Param type 1-月 2-日 3-星期
**/
public static String getSchedule(Integer value, Integer type) {
if (value == null || type == null){
return "";
}
//将int转为二进制
String binary = Integer.toBinaryString(value);
StringBuilder stringBuilder = new StringBuilder(binary);
String s = stringBuilder.reverse().toString();
int len = s.length();
switch (type) {
case 1:
//月
s = repeatCharacter(s, 13).substring(1, 13);
break;
case 2:
//日
s = repeatCharacter(s, 32).substring(1, 32);
break;
case 3:
//星期
s = repeatCharacter(s, 8).substring(1, 8);
break;
default:
break;
}
StringBuilder str = new StringBuilder();
char[] chars = s.toCharArray();
for (int i = 0; i < chars.length; i++) {
char aChar = chars[i];
if (aChar == 49) {
if (type == 3) {
//星期日是0
str.append(i).append(",");
} else {
str.append(i + 1).append(",");
}
}
}
int i = str.lastIndexOf(",");
if (i > 0) {
StringBuilder stringBuilder1 = str.deleteCharAt(i);
return stringBuilder1.toString();
}
return "";
}
/**
* @return java.lang.String
* @Description 字符串根据长度补0
* @Param str 原字符串
* @Param length 长度
**/
private static String repeatCharacter(String str, int length) {
int times = length - str.length();
StringBuilder sb = new StringBuilder();
for (int i = 0; i < times; i++) {
sb.append("0");
}
return str + sb.toString();
}
/**
* 将二进制整数部分转换成十进制
*
* @param inteter 二进制整数部分字符串
* @return 转换后的十进制数值
*/
public static Integer binaryIntToDecimalism(String inteter) {
int inteterSum = 0;
for (int i = inteter.length(); i > 0; i--) {
int scale = 2;
if (inteter.charAt(-(i - inteter.length())) == '1') {
if (i != 1) {
for (int j = 1; j < i - 1; j++) {
scale *= 2;
}
} else {
scale = 1;
}
} else {
scale = 0;
}
inteterSum += scale;
}
return inteterSum;
}
public static void main(String[] args) {
System.out.println(getSchedule(8190, 1));
System.out.println(getSchedule(0, 2));
System.out.println(getSchedule(4, 3));
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment