-
admin
2024-05-17 68f0c8f92fb7c82dc447b9aaed2d23760c546f25
提交 | 用户 | 时间
d5d31b 1 package com.jcdm.main.plcserver.util;
2
3 import java.text.ParseException;
4 import java.text.SimpleDateFormat;
616f98 5 import java.time.LocalDateTime;
A 6 import java.time.format.DateTimeFormatter;
d5d31b 7 import java.util.Date;
8 import java.util.Locale;
9 import java.util.TimeZone;
10
11 public class TimeUtil {
288790 12     public static void main(String[] args) {
616f98 13         String dateString = "Tue May 14 18:25:18 CST 2024";
A 14 //        System.out.println(test(dateString));
68f0c8 15         String str = "OP330";
A 16         System.out.println(str.substring(0,5));
17 //        System.out.println(test( stringProcessing("DateTime{utcTime=133601559184960000, javaDate=Tue May 14 18:25:18 CST 2024}")));
288790 18     }
A 19
d5d31b 20     public static String stringProcessing(String dateTimeStr){
21         String result = "";
22         // 找到 "javaDate=" 的位置
23         int javaDateIndex = dateTimeStr.indexOf("javaDate=");
24         if (javaDateIndex != -1) {
25             // 从 "javaDate=" 后面开始截取
26             javaDateIndex += "javaDate=".length();
27
28             // 找到日期时间字符串结束的位置,这里假设是字符串的结尾或者空格的位置
29             int endIndex = dateTimeStr.indexOf('}', javaDateIndex);
30             if (endIndex == -1) {
31                 // 如果没有找到空格,就取到字符串的末尾
32                 endIndex = dateTimeStr.length();
33             }
34             // 使用 substring 方法截取日期时间部分
35             String dateTimePart = dateTimeStr.substring(javaDateIndex, endIndex);
36             result = dateTimePart;
37         } else {
38             System.out.println("javaDate= not found in the string.");
39         }
40         return result;
41     }
42
43     public static String getTimestamp(String param){
44         String result = "";
45         // 定义日期时间格式和时区
46         SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy", Locale.US);
47         sdf.setTimeZone(TimeZone.getTimeZone("GMT+8")); // CST通常表示中国标准时间,即东八区
48
49         try {
50             // 解析日期时间字符串
51             Date date = sdf.parse("Mon Jan 01 17:05:23 CST 2024");
52
53             // 获取时间戳(毫秒)
54             long timestamp = date.getTime();
55
56             // 输出13位时间戳
57             result = String.valueOf(timestamp);
58
59         } catch (ParseException e) {
60             e.printStackTrace();
61         }
62         return result;
63     }
288790 64
A 65     public static String getYyyyDdMm(String inputDate){
66         String outputPattern = "yyyy-MM-dd HH:mm:ss";
67
68         // 定义一个解析器,用于忽略时区(仅作为示例,可能需要调整以处理不同的输入)
69         SimpleDateFormat parser = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy", Locale.ENGLISH);
70         parser.setLenient(false); // 设置不宽松,以严格匹配格式
71
72         // 解析日期字符串
73         Date date = null;
74         try {
75             date = parser.parse(inputDate);
76         } catch (ParseException e) {
77             e.printStackTrace();
78         }
79
80         // 定义一个格式化器,用于输出所需的格式
81         SimpleDateFormat formatter = new SimpleDateFormat(outputPattern);
82
83         // 格式化日期
84         String outputDate = formatter.format(date);
85         return outputDate;
86     }
616f98 87
A 88     public static String test(String param){
89         String str = "";
90         // 创建一个SimpleDateFormat对象来解析输入的日期字符串(假设CST为中国标准时间)
91         SimpleDateFormat inputFormat = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy", Locale.US);
92         inputFormat.setTimeZone(TimeZone.getTimeZone("Asia/Shanghai")); // 设置为中国时区
93
94         Date date = null;
95         try {
96             date = inputFormat.parse(param);
97         } catch (ParseException e) {
98             e.printStackTrace();
99         }
100
101         // 创建一个SimpleDateFormat对象来格式化日期为UTC时间
102         SimpleDateFormat outputFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
103         outputFormat.setTimeZone(TimeZone.getTimeZone("UTC")); // 设置为UTC时区
104
105         // 格式化日期
106         if (date != null) {
107             str = outputFormat.format(date);
108         }
109         return str;
110     }
111
112
113
d5d31b 114 }