-
admin
2024-05-17 68f0c8f92fb7c82dc447b9aaed2d23760c546f25
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
package com.jcdm.main.plcserver.util;
 
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;
 
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));
//        System.out.println(test( stringProcessing("DateTime{utcTime=133601559184960000, javaDate=Tue May 14 18:25:18 CST 2024}")));
    }
 
    public static String stringProcessing(String dateTimeStr){
        String result = "";
        // 找到 "javaDate=" 的位置
        int javaDateIndex = dateTimeStr.indexOf("javaDate=");
        if (javaDateIndex != -1) {
            // 从 "javaDate=" 后面开始截取
            javaDateIndex += "javaDate=".length();
 
            // 找到日期时间字符串结束的位置,这里假设是字符串的结尾或者空格的位置
            int endIndex = dateTimeStr.indexOf('}', javaDateIndex);
            if (endIndex == -1) {
                // 如果没有找到空格,就取到字符串的末尾
                endIndex = dateTimeStr.length();
            }
            // 使用 substring 方法截取日期时间部分
            String dateTimePart = dateTimeStr.substring(javaDateIndex, endIndex);
            result = dateTimePart;
        } else {
            System.out.println("javaDate= not found in the string.");
        }
        return result;
    }
 
    public static String getTimestamp(String param){
        String result = "";
        // 定义日期时间格式和时区
        SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy", Locale.US);
        sdf.setTimeZone(TimeZone.getTimeZone("GMT+8")); // CST通常表示中国标准时间,即东八区
 
        try {
            // 解析日期时间字符串
            Date date = sdf.parse("Mon Jan 01 17:05:23 CST 2024");
 
            // 获取时间戳(毫秒)
            long timestamp = date.getTime();
 
            // 输出13位时间戳
            result = String.valueOf(timestamp);
 
        } catch (ParseException e) {
            e.printStackTrace();
        }
        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;
    }
 
 
 
}