You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
weaver-develop/src/com/engine/gainway/util/BasicResourceUtil.java

91 lines
2.7 KiB
Java

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

package com.engine.gainway.util;
import java.time.LocalDate;
import java.time.Period;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* @Author liang.cheng
* @Date 2024/12/12 10:19 AM
* @Description: TODO
* @Version 1.0
*/
public class BasicResourceUtil {
public static int calculateAge(String birthday) {
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
LocalDate birthDate = LocalDate.parse(birthday, formatter);
LocalDate today = LocalDate.now();
int age = Period.between(birthDate, today).getYears();
if (today.getMonthValue() < birthDate.getMonthValue() ||
(today.getMonthValue() == birthDate.getMonthValue() && today.getDayOfMonth() < birthDate.getDayOfMonth())) {
// 如果当前日期还没有到达生日日期年龄减1
age--;
}
return age;
}
public static Map<String,Integer> rangeExtractor(String name){
Pattern pattern = Pattern.compile("\\d{1,2}");
Matcher matcher = pattern.matcher(name);
int from = 0;
int to = 0;
if (matcher.find()) {
from = Integer.parseInt(matcher.group());
}
if (matcher.find()) {
to = Integer.parseInt(matcher.group());
}
Map<String,Integer> map = new HashMap<>(2);
map.put("from",from);
map.put("to",to);
return map;
}
public static String calculateDate(LocalDate today, int number) {
LocalDate birthDate = today.minusYears(number);
return birthDate.toString();
}
public static Map<String,String> selectRangeSet(String name) {
Map<String,String> map = new HashMap<>(2);
//提取范围
Map<String, Integer> extractor = rangeExtractor(name);
int from = extractor.get("from");
int to = extractor.get("to");
LocalDate today = LocalDate.now();
String fromDate = calculateDate(today, to+1);
String toDate = calculateDate(today, from);
map.put("fromDate",fromDate);
map.put("toDate",toDate);
return map;
}
public static Map<String,String> companyYearRangeSet(String name) {
Map<String,String> map = new HashMap<>(2);
//提取范围
Map<String, Integer> extractor = rangeExtractor(name);
int from = extractor.get("from");
int to = extractor.get("to");
LocalDate today = LocalDate.now();
String fromDate = calculateDate(today, to);
String toDate = calculateDate(today, from);
map.put("fromDate",fromDate);
map.put("toDate",toDate);
return map;
}
}