Skip to content

Commit ad8e02e

Browse files
committed
feat: base core util
1 parent 904cde9 commit ad8e02e

File tree

5 files changed

+616
-0
lines changed

5 files changed

+616
-0
lines changed
Lines changed: 158 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,158 @@
1+
package com.starimmortal.core.util;
2+
3+
import com.starimmortal.core.constant.PatternConstant;
4+
import com.starimmortal.core.constant.StringConstant;
5+
import lombok.extern.slf4j.Slf4j;
6+
import org.springframework.stereotype.Component;
7+
import org.springframework.web.context.request.RequestContextHolder;
8+
import org.springframework.web.context.request.ServletRequestAttributes;
9+
10+
import javax.servlet.http.HttpServletRequest;
11+
import java.math.BigDecimal;
12+
import java.util.Calendar;
13+
import java.util.Random;
14+
15+
/**
16+
* 通用工具类
17+
*
18+
* @author william@StarImmortal
19+
* @date 2021/02/08
20+
*/
21+
@Component
22+
@Slf4j
23+
public class CommonUtil {
24+
/**
25+
* 请求头
26+
*/
27+
private static final String[] IP_HEADER_CANDIDATES = {
28+
"X-Forwarded-For",
29+
"Proxy-Client-IP",
30+
"WL-Proxy-Client-IP",
31+
"HTTP_X_FORWARDED_FOR",
32+
"HTTP_X_FORWARDED",
33+
"HTTP_X_CLUSTER_CLIENT_IP",
34+
"HTTP_CLIENT_IP",
35+
"HTTP_FORWARDED_FOR",
36+
"HTTP_FORWARDED",
37+
"HTTP_VIA",
38+
"REMOTE_ADDR"
39+
};
40+
41+
/**
42+
* 金额单位元转化为分
43+
* stripTrailingZeros:移除所有尾部零
44+
*
45+
* @param p 金额(单位:元)
46+
* @return 金额(单位:分)
47+
*/
48+
public static BigDecimal yuanToFenPlain(BigDecimal p) {
49+
p = p.multiply(new BigDecimal("100"));
50+
return p.stripTrailingZeros();
51+
}
52+
53+
/**
54+
* 获取本机ip地址
55+
*
56+
* @return ip地址
57+
*/
58+
public static String getRemoteRealIp(HttpServletRequest request) {
59+
if (request == null) {
60+
if (null == RequestContextHolder.getRequestAttributes()) {
61+
return null;
62+
}
63+
request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
64+
}
65+
66+
for (String header : IP_HEADER_CANDIDATES) {
67+
String ipList = request.getHeader(header);
68+
if (ipList != null && ipList.length() != 0 && !"unknown".equalsIgnoreCase(ipList)) {
69+
log.debug("ipList.split(\",\")[0]::{}", ipList.split(",")[0]);
70+
return ipList.split(",")[0];
71+
}
72+
}
73+
74+
log.debug("request.getRemoteAddress()::{}", request.getRemoteAddr());
75+
return request.getRemoteAddr();
76+
}
77+
78+
/**
79+
* 十位时间戳
80+
*
81+
* @return timestamp
82+
*/
83+
public static String tenTimestamp() {
84+
long timestamp = Calendar.getInstance().getTimeInMillis();
85+
return Long.toString(timestamp).substring(0, Long.toString(timestamp).length() - 3);
86+
}
87+
88+
/**
89+
* 常规自动日期格式识别
90+
*
91+
* @param str 时间字符串
92+
* @return Date
93+
*/
94+
public static String getDateFormat(String str) {
95+
boolean year = PatternConstant.DATE_PATTERN.matcher(str.substring(0, 4)).matches();
96+
StringBuilder sb = new StringBuilder();
97+
int index = 0;
98+
if (!year) {
99+
if (str.contains("月") || str.contains(StringConstant.LINE_SEPARATOR) || str.contains(StringConstant.SEPARATOR)) {
100+
if (Character.isDigit(str.charAt(0))) {
101+
index = 1;
102+
}
103+
} else {
104+
index = 3;
105+
}
106+
}
107+
for (int i = 0; i < str.length(); i++) {
108+
char chr = str.charAt(i);
109+
if (Character.isDigit(chr)) {
110+
if (index == 0) {
111+
sb.append("y");
112+
}
113+
if (index == 1) {
114+
sb.append("M");
115+
}
116+
if (index == 2) {
117+
sb.append("d");
118+
}
119+
if (index == 3) {
120+
sb.append("H");
121+
}
122+
if (index == 4) {
123+
sb.append("m");
124+
}
125+
if (index == 5) {
126+
sb.append("s");
127+
}
128+
if (index == 6) {
129+
sb.append("S");
130+
}
131+
} else {
132+
if (i > 0) {
133+
char lastChar = str.charAt(i - 1);
134+
if (Character.isDigit(lastChar)) {
135+
index++;
136+
}
137+
}
138+
sb.append(chr);
139+
}
140+
}
141+
return sb.toString();
142+
}
143+
144+
/**
145+
* 生成指定位数的随机数字字符串
146+
*
147+
* @param length 字符串长度
148+
* @return 随机数字字符串
149+
*/
150+
public static String randomNumbers(int length) {
151+
Random random = new Random();
152+
StringBuilder randomNumbers = new StringBuilder();
153+
for (int i = 0; i < length; i++) {
154+
randomNumbers.append(random.nextInt(10));
155+
}
156+
return randomNumbers.toString();
157+
}
158+
}

0 commit comments

Comments
 (0)