-
懒羊羊
2024-04-12 d5d31b9096d6e50cdac2069426b19b4507a91237
提交 | 用户 | 时间
d5d31b 1 package com.jcdm.main.plcserver.util;
2
3 import java.text.ParseException;
4 import java.text.SimpleDateFormat;
5 import java.util.Date;
6 import java.util.Locale;
7 import java.util.TimeZone;
8
9 public class TimeUtil {
10     public static String stringProcessing(String dateTimeStr){
11         String result = "";
12         // 找到 "javaDate=" 的位置
13         int javaDateIndex = dateTimeStr.indexOf("javaDate=");
14         if (javaDateIndex != -1) {
15             // 从 "javaDate=" 后面开始截取
16             javaDateIndex += "javaDate=".length();
17
18             // 找到日期时间字符串结束的位置,这里假设是字符串的结尾或者空格的位置
19             int endIndex = dateTimeStr.indexOf('}', javaDateIndex);
20             if (endIndex == -1) {
21                 // 如果没有找到空格,就取到字符串的末尾
22                 endIndex = dateTimeStr.length();
23             }
24             // 使用 substring 方法截取日期时间部分
25             String dateTimePart = dateTimeStr.substring(javaDateIndex, endIndex);
26             result = dateTimePart;
27         } else {
28             System.out.println("javaDate= not found in the string.");
29         }
30         return result;
31     }
32
33     public static String getTimestamp(String param){
34         String result = "";
35         // 定义日期时间格式和时区
36         SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy", Locale.US);
37         sdf.setTimeZone(TimeZone.getTimeZone("GMT+8")); // CST通常表示中国标准时间,即东八区
38
39         try {
40             // 解析日期时间字符串
41             Date date = sdf.parse("Mon Jan 01 17:05:23 CST 2024");
42
43             // 获取时间戳(毫秒)
44             long timestamp = date.getTime();
45
46             // 输出13位时间戳
47             result = String.valueOf(timestamp);
48
49         } catch (ParseException e) {
50             e.printStackTrace();
51         }
52         return result;
53     }
54 }