| | |
| | | |
| | | import java.text.ParseException; |
| | | import java.text.SimpleDateFormat; |
| | | import java.time.LocalDateTime; |
| | | import java.time.format.DateTimeFormatter; |
| | | import java.util.Date; |
| | | import java.util.Locale; |
| | | import java.util.TimeZone; |
| | | import java.util.regex.Matcher; |
| | | import java.util.regex.Pattern; |
| | | |
| | | public class TimeUtil { |
| | | public static void main(String[] args) { |
| | | String dateString = "Tue May 14 18:25:18 CST 2024"; |
| | | // System.out.println(test(dateString)); |
| | | // String str = "OP330"; |
| | | // System.out.println(str.substring(0,5)); |
| | | // String tightenData = "[9.077463,12.0,OK,OK]"; |
| | | // tightenData = tightenData.replace("[", "").replace("]", "").replace(" ", ""); |
| | | // String[] tightenDataParts = tightenData.split(","); |
| | | // for (int i = 0; i < tightenDataParts.length; i++) { |
| | | // System.out.println(tightenDataParts[i]); |
| | | // } |
| | | String originalString = "[3.6,7.8,OK,OK]"; |
| | | String modifiedString = originalString.replaceFirst("OK", "NG"); |
| | | System.out.println(modifiedString); // 输出: [3.6,7.8,NG,OK] |
| | | // System.out.println(isNumberInRange(7.7, 7.5, 8.5)); |
| | | // System.out.println(test( stringProcessing("DateTime{utcTime=133601559184960000, javaDate=Tue May 14 18:25:18 CST 2024}"))); |
| | | } |
| | | |
| | | public static String replaceNumber(String input) { |
| | | String regex = ",(\\d+),"; |
| | | String replacement = ",NG,"; |
| | | |
| | | Pattern pattern = Pattern.compile(regex); |
| | | Matcher matcher = pattern.matcher(input); |
| | | |
| | | return matcher.replaceAll(replacement); |
| | | } |
| | | |
| | | // 检查一个数是否在两个数定义的区间内(不包含边界) |
| | | public static boolean isNumberInRange(Double number, Double start, Double end) { |
| | | return number >= start && number <= end; |
| | | } |
| | | |
| | | public static String stringProcessing(String dateTimeStr){ |
| | | String result = ""; |
| | | // 找到 "javaDate=" 的位置 |
| | |
| | | } |
| | | return result; |
| | | } |
| | | |
| | | public static String getYyyyDdMm(String inputDate){ |
| | | String outputPattern = "yyyy-MM-dd HH:mm:ss"; |
| | | |
| | | // 定义一个解析器,用于忽略时区(仅作为示例,可能需要调整以处理不同的输入) |
| | | SimpleDateFormat parser = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy", Locale.ENGLISH); |
| | | parser.setLenient(false); // 设置不宽松,以严格匹配格式 |
| | | |
| | | // 解析日期字符串 |
| | | Date date = null; |
| | | try { |
| | | date = parser.parse(inputDate); |
| | | } catch (ParseException e) { |
| | | e.printStackTrace(); |
| | | } |
| | | |
| | | // 定义一个格式化器,用于输出所需的格式 |
| | | SimpleDateFormat formatter = new SimpleDateFormat(outputPattern); |
| | | |
| | | // 格式化日期 |
| | | String outputDate = formatter.format(date); |
| | | return outputDate; |
| | | } |
| | | |
| | | public static String test(String param){ |
| | | String str = ""; |
| | | // 创建一个SimpleDateFormat对象来解析输入的日期字符串(假设CST为中国标准时间) |
| | | SimpleDateFormat inputFormat = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy", Locale.US); |
| | | inputFormat.setTimeZone(TimeZone.getTimeZone("Asia/Shanghai")); // 设置为中国时区 |
| | | |
| | | Date date = null; |
| | | try { |
| | | date = inputFormat.parse(param); |
| | | } catch (ParseException e) { |
| | | e.printStackTrace(); |
| | | } |
| | | |
| | | // 创建一个SimpleDateFormat对象来格式化日期为UTC时间 |
| | | SimpleDateFormat outputFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); |
| | | outputFormat.setTimeZone(TimeZone.getTimeZone("UTC")); // 设置为UTC时区 |
| | | |
| | | // 格式化日期 |
| | | if (date != null) { |
| | | str = outputFormat.format(date); |
| | | } |
| | | return str; |
| | | } |
| | | |
| | | |
| | | |
| | | } |