Ecology-Dev-chaofa/FiveHzCenter/src/com/fage/DemoTest.java

449 lines
17 KiB
Java
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package com.fage;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.wbi.util.Util;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import weaver.general.TimeUtil;
import java.io.*;
import java.math.BigDecimal;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.Period;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeParseException;
import java.time.temporal.ChronoUnit;
import java.text.DecimalFormat;
import java.util.*;
import org.apache.commons.codec.binary.Base64;
import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.nio.charset.StandardCharsets;
public class DemoTest {
public static void main(String[] args) {
// 示例日期字符串
String startDateStr = "2025-05-21";
// // 解析日期
// LocalDate startDate = LocalDate.parse(startDateStr);
// LocalDate endDate = LocalDate.now();
//
// // 计算工龄(月份)
// long monthsBetween = ChronoUnit.MONTHS.between(startDate, endDate);
//
// // 将月份转换为年并格式化为2位小数
// double years = (double) monthsBetween / 12;
// DecimalFormat df = new DecimalFormat("0.00"); // 设置格式为2位小数
String formattedYears = calculateAge(startDateStr);
// String kjshgl = "2.00";
// String endAge = String.valueOf(new BigDecimal(formattedYears).subtract(new BigDecimal(kjshgl)));
// 输出结果
// System.out.println("工龄(年): " + formattedYears);
//
// System.out.println(getMonthStartStr(startDateStr,0));
//
// System.out.println(getMonthByDateStr(startDateStr));
// System.out.println(getMonthEndByDateStr(startDateStr,0));
//
// String currenttime = TimeUtil.getOnlyCurrentTimeString();
//
// String ywrq = "2025-05-26";
// ywrq += "T"+currenttime;
//
// System.out.println(ywrq);
//
// System.out.println(ywrq.substring(0,7).replaceAll("-",""));
//
// String APP_SECRET = "YVBKbcEQSXlwcpOxjOGRhODWASSkzg";
// String data = System.currentTimeMillis() + "||" + APP_SECRET;
// String IV = APP_SECRET.substring(APP_SECRET.length() - 16);
// String key = APP_SECRET.substring(0, 16);
// System.out.println(encryptData(data, key, IV));
// double ycqzss=149.08;
// double yycqss=152.0;
// double fdxjsc = 0.0;
// double jrjbs=0.0;
// double prjbss=0.0;
//
// double xxrjbs = ycqzss - fdxjsc - yycqss - jrjbs - prjbss;
// System.out.println(xxrjbs);
//
// String dateStr1 = "2023-03-01";
// String dateStr2 = "2025-07-24";
//
// DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
// LocalDate date1 = LocalDate.parse(dateStr1, formatter);
// LocalDate date2 = LocalDate.parse(dateStr2, formatter);
//
// long daysBetween = Math.abs(ChronoUnit.DAYS.between(date1, date2));
// System.out.println("相差天数: " + daysBetween);
// String aaa = "2025-08-07";
// System.out.println("相差天数: " + addDateDay(aaa,-24));
//
// String token = "X4eTEiMr-1pdUwi1c9vxTidpgSg8rhOGbl7GFPvG8465Kadur7e5KBjFl3FS5amfsls0nPpzA";
//
// String applyId = "5dd8c3ca-b986-441f-ba66-12e63c841152";
//
// String url = "http://openapi.italent.cn/RecruitV6/api/v1/Applicant/GetResumeByApplyId";
//
// String result = httpGetRequestInfo(url,applyId,token);
//
// System.out.println("北森,简历接口返回 : " + result);
//
// JSONObject maindata_jsonObject = JSONObject.parseObject(result);
//
// String data = maindata_jsonObject.get("data").toString();
// JSONObject data_jsonObject = JSONObject.parseObject(data);
//
// JSONArray itemArray = data_jsonObject.getJSONArray("personProfile");
// for (int i = 0; i < itemArray.size(); i++) {
// Map<String, Object> newmap = new HashMap<>();
//
// JSONObject dataObject = itemArray.getJSONObject(i);
//
// String name = dataObject.get("name").toString();
// System.out.println("北森name : " + name);
// }
// String a = "1999/02/12";
// a = a.replaceAll("/","-");
// System.out.println(a);
//
//
//
// String data = "{\n" +
// "\"education\": [\n" +
// "\t\t\t[\n" +
// "\t\t\t\t{\n" +
// "\t\t\t\t\t\"name\": \"_id\",\n" +
// "\t\t\t\t\t\"value\": \"b1c310ec-e661-48a6-891c-5838c5e92231\",\n" +
// "\t\t\t\t\t\"text\": \"b1c310ec-e661-48a6-891c-5838c5e92231\",\n" +
// "\t\t\t\t\t\"downloadUrl\": null\n" +
// "\t\t\t\t},\n" +
// "\t\t\t\t{\n" +
// "\t\t\t\t\t\"name\": \"EndDate\",\n" +
// "\t\t\t\t\t\"value\": \"2025/07\",\n" +
// "\t\t\t\t\t\"text\": \"2025/07\",\n" +
// "\t\t\t\t\t\"downloadUrl\": null\n" +
// "\t\t\t\t},\n" +
// "\t\t\t\t{\n" +
// "\t\t\t\t\t\"name\": \"EducationLevel\",\n" +
// "\t\t\t\t\t\"value\": \"1\",\n" +
// "\t\t\t\t\t\"text\": \"本科\",\n" +
// "\t\t\t\t\t\"downloadUrl\": null\n" +
// "\t\t\t\t},\n" +
// "\t\t\t\t{\n" +
// "\t\t\t\t\t\"name\": \"StartDate\",\n" +
// "\t\t\t\t\t\"value\": \"2021/09\",\n" +
// "\t\t\t\t\t\"text\": \"2021/09\",\n" +
// "\t\t\t\t\t\"downloadUrl\": null\n" +
// "\t\t\t\t},\n" +
// "\t\t\t\t{\n" +
// "\t\t\t\t\t\"name\": \"SchoolName\",\n" +
// "\t\t\t\t\t\"value\": \"10235\",\n" +
// "\t\t\t\t\t\"text\": \"大庆师范学院\",\n" +
// "\t\t\t\t\t\"downloadUrl\": null\n" +
// "\t\t\t\t},\n" +
// "\t\t\t\t{\n" +
// "\t\t\t\t\t\"name\": \"MajorName\",\n" +
// "\t\t\t\t\t\"value\": \"钢琴表演\",\n" +
// "\t\t\t\t\t\"text\": \"钢琴表演\",\n" +
// "\t\t\t\t\t\"downloadUrl\": null\n" +
// "\t\t\t\t}\n" +
// "\t\t\t]\n" +
// "\t\t]" +
// "}";
// JSONObject data_jsonObject = JSONObject.parseObject(data);
// JSONArray jyjlArray = data_jsonObject.getJSONArray("education");
// for (int i = 0; i < jyjlArray.size(); i++) {
// JSONArray dataArray = jyjlArray.getJSONArray(i);
// for (int j = 0; j < dataArray.size(); j++) {
// JSONObject dataObject = dataArray.getJSONObject(j);
// System.out.println(dataObject);
// String name = dataObject.get("name").toString();
// String value = dataObject.get("value").toString();
// String text = dataObject.get("text").toString();
// }
// }
String a = "1991-10-07";
System.out.println(a.substring(0,4));
System.out.println(a.substring(5,7));
System.out.println(a.substring(8,10));
System.out.println(a.substring(5,10));
// 假设入职日期是2020年1月1日
LocalDate hireDate = LocalDate.of(2023, 8, 30);
// 获取当前日期
LocalDate currentDate = LocalDate.now();
// 计算司龄总月份数除以12
double monthsBetween = Math.abs(Period.between(hireDate, currentDate).getMonths()) + 12 * Math.abs(Period.between(hireDate, currentDate).getYears());
double serviceYears = monthsBetween / 12.0; // 注意这里已经是总月份数除以12的结果可以直接格式化输出
// 使用DecimalFormat格式化输出保留两位小数
DecimalFormat df = new DecimalFormat("#.##"); // 注意这里的格式化符号使用的是英文的点号和井号,而不是中文的井号和井号。
String formattedServiceYears = df.format(serviceYears);
System.out.println("司龄(保留两位小数): " + formattedServiceYears + "");
String date1 = "2023-08-30";
String date2 = "2025-08-29";
int days = getDays(date1,date2);
System.out.println("相差天数:"+ days);
}
/**
* 发送 http get 请求,参数以原生字符串进行提交
* @param urll
* @param applyId
* @param token
* @return
*/
public static String httpGetRequestInfo(String urll, String applyId, String token){
String result = "";
HttpURLConnection conn = null;
InputStream in=null;
BufferedReader reader=null;
int statusCode = 0;
try {
StringBuilder urlBuilder = new StringBuilder(urll);
urlBuilder.append("?");
urlBuilder.append("applyId").append("=").append(URLEncoder.encode(applyId, "UTF-8"));
System.out.println(" urlParameters : " + urlBuilder);
URL url = new URL(urlBuilder.toString());
conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
conn.setRequestProperty("Authorization", "Bearer "+ token);
conn.setDoOutput(true);
conn.setDoInput(true);
conn.setUseCaches(false);
statusCode = conn.getResponseCode();
System.out.println(statusCode);
if (statusCode == 200) {
in = conn.getInputStream();
//通过InputStreamReader将字节流转换成字符串在通过BufferedReader将字符流转换成自带缓冲流
reader = new BufferedReader(new InputStreamReader(in, StandardCharsets.UTF_8));
StringBuilder sb = new StringBuilder();
String line = null;
//按行读取
while ((line = reader.readLine()) != null) {
sb.append(line);
}
result = sb.toString();
System.out.println(result);
}
} catch (Exception e) {
e.printStackTrace();
}finally{
if (conn != null) {
conn.disconnect();
}
if (in != null) {
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (reader != null) {
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return result;
}
public static String addDateDay(String daytime, int day){
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
Date date = null;
try {
date = format.parse(daytime);
if (date == null){
return "";
}
Calendar cal = Calendar.getInstance();
cal.setTime(date);
cal.add(Calendar.DAY_OF_MONTH, day);// 加一天
date = cal.getTime();
cal = null;
return format.format(date);
} catch (Exception ex) {
ex.printStackTrace();
}
return "";
}
/**
* 计算两个时间字符串日期的相差天数
*
* @param date1 字符串时间1
* @param date2 字符串时间2
* @return 相差时间
*/
public static int getDays(String date1, String date2) {
if (!"".equals(date1) && !"".equals(date2)) {
String date1Arr[] = date1.split("-");
String date2Arr[] = date2.split("-");
Calendar cal1 = Calendar.getInstance();
cal1.set(Integer.parseInt(date1Arr[0]), Integer.parseInt(date1Arr[1]) - 1, Integer.parseInt(date1Arr[2]));
Date date1Obj = cal1.getTime();
Calendar cal2 = Calendar.getInstance();
cal2.set(Integer.parseInt(date2Arr[0]), Integer.parseInt(date2Arr[1]) - 1, Integer.parseInt(date2Arr[2]));
Date date2Obj = cal2.getTime();
return calculateDays(date1Obj, date2Obj);
}
return 0;
}
/**
* 计算天数
* @param date1 日期
* @param date2 日期
* @return 天数
*/
public static int calculateDays(Date date1, Date date2) {
Calendar cal1 = Calendar.getInstance();
cal1.setTime(date1);
Calendar cal2 = Calendar.getInstance();
cal2.setTime(date2);
int day1 = cal1.get(Calendar.DAY_OF_YEAR);
int day2 = cal2.get(Calendar.DAY_OF_YEAR);
int year1 = cal1.get(Calendar.YEAR);
int year2 = cal2.get(Calendar.YEAR);
if (year1 != year2) {//不同 年
int timeDistance = 0;
if (year1 < year2) {//后面日期年大于前面日期
for (int i = year1; i < year2; i++) {
if (i % 4 == 0 && i % 100 != 0 || i % 400 == 0) {//闰年
timeDistance += 366;
} else {//不是闰年
timeDistance += 365;
}
}
} else {
for (int i = year2; i < year1; i++) {
if (i % 4 == 0 && i % 100 != 0 || i % 400 == 0) {//闰年
timeDistance += 366;
} else {//不是闰年
timeDistance += 365;
timeDistance = 0 - timeDistance;
}
}
}
return timeDistance + (day2 - day1);
} else {//同一年
return day2 - day1;
}
}
/**
*
* @param data 需要加密的数据
* @param key 秘钥
* @param IV 偏移量
* @return
*/
private static String encryptData(String data, String key, String IV) {
try {
//算法/模式/填充模式 AES/CBC/PKCS5Padding
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
byte[] dataBytes = data.getBytes(StandardCharsets.UTF_8);
int plaintextLength = dataBytes.length;
byte[] plaintext = new byte[plaintextLength];
System.arraycopy(dataBytes, 0, plaintext, 0, dataBytes.length);
SecretKeySpec keyspec = new SecretKeySpec(key.getBytes(), "AES");
IvParameterSpec ivspec = new IvParameterSpec(IV.getBytes());
cipher.init(Cipher.ENCRYPT_MODE, keyspec, ivspec);
byte[] encrypted = cipher.doFinal(plaintext);
//输出内容Base64处理
return new String(Base64.encodeBase64(encrypted));
} catch (Exception e) {
return null;
}
}
/**
* 根据日期字符串的月末
*
* @param dateStr 日期字符串
* @param num 上下某月
* @return 月初月末集合
*/
public static String getMonthEndByDateStr(String dateStr, int num) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
String res = "";
try {
Date date = sdf.parse(dateStr);
Calendar c1 = Calendar.getInstance();
c1.setTime(date);
c1.add(Calendar.MONTH, num);
c1.set(Calendar.DAY_OF_MONTH, 1);
String start = sdf.format(c1.getTime());
Calendar c2 = Calendar.getInstance();
Date dateTemp = sdf.parse(start);
c2.setTime(dateTemp);
c2.add(Calendar.MONTH, 1);
c2.set(Calendar.DAY_OF_MONTH, 0);
res = sdf.format(c2.getTime());
} catch (Exception e) {
e.printStackTrace();
}
return res;
}
public static String calculateAge(String birthdate) {
String result = "0";
try {
// 解析日期
LocalDate startDate = LocalDate.parse(birthdate);
LocalDate endDate = LocalDate.now();
// 计算工龄(月份)
long monthsBetween = ChronoUnit.MONTHS.between(startDate, endDate);
// 将月份转换为年并格式化为2位小数
double years = (double) monthsBetween / 12;
DecimalFormat df = new DecimalFormat("0.00"); // 设置格式为2位小数
result = df.format(years);
}catch (Exception e){
e.printStackTrace();
}
return result;
}
}