DateUtils.java
3.94 KB
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
package com.glsx.commonres.util;
import android.annotation.SuppressLint;
import android.text.TextUtils;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
/**
* Author by zh, Date on 2022/3/2.
* PS: write a bug or more,please indicate.
* 日期工具类
*/
public class DateUtils {
public static final String DATE_FORMAT_ALL = "yyyy-MM-dd HH:mm:ss";
public static final String DATE_FORMAT_DAY = "yyyy-MM-dd";
public static long getUnixStamp() {
return System.currentTimeMillis() / 1000;
}
public static long getTodayBeginTimestamp() {
Date date = new Date();
GregorianCalendar gc = new GregorianCalendar();
gc.setTime(date);
Date date2 = new Date(date.getTime() - gc.get(Calendar.HOUR_OF_DAY) * 60 * 60
* 1000 - gc.get(Calendar.MINUTE) * 60 * 1000 - gc.get(Calendar.SECOND)
* 1000);
return date2.getTime() / 1000;
}
public static long getYesterdayBeginTimestamp() {
return getTodayBeginTimestamp() - 24 * 60 * 60;
}
public static String name2DateString(String name) {
try {
Date date = name2Date(name);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
String dateStr = sdf.format(date);
return dateStr;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
public static Date name2Date(String name) {
try {
name = name.substring(0, name.indexOf('.'));
name = name.replaceAll("[^0-9]", "");
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
Date date = sdf.parse(name);
return date;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
private static String name2HourString(String name) {
try {
name = name.substring(0, name.indexOf('.'));
name = name.replaceAll("[^0-9]", "");
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
Date date = sdf.parse(name);
sdf.applyPattern("HH:mm");
String dateStr = sdf.format(date);
return dateStr;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
/**
* 获取当前时间戳格式化字符
*
* @return
*/
@SuppressLint("SimpleDateFormat")
public static String getTimestampStr() {
return new SimpleDateFormat(DATE_FORMAT_ALL).format(new Date());
}
/**
* 日期转格式化字符串
*
* @param date
* @return
*/
@SuppressLint("SimpleDateFormat")
public static String getTimestampStr(Date date) {
return new SimpleDateFormat(DATE_FORMAT_ALL).format(date);
}
/**
* 日期转格式化字符串
*
* @param timestamp
* @return
*/
@SuppressLint("SimpleDateFormat")
public static String getTimestampStr(long timestamp) {
return new SimpleDateFormat(DATE_FORMAT_ALL).format(timestamp);
}
@SuppressLint("SimpleDateFormat")
public static long getTimestamp(String time) {
if (TextUtils.isEmpty(time)) {
return 0;
}
long timestamp = 0;
try {
timestamp = new SimpleDateFormat(DATE_FORMAT_ALL).parse(time).getTime();
} catch (ParseException e) {
e.printStackTrace();
}
return timestamp;
}
/**
* 获取当前时间戳格式化字符
*
* @return
*/
@SuppressLint("SimpleDateFormat")
public static String getTimestampStrOfDay() {
return new SimpleDateFormat(DATE_FORMAT_DAY).format(new Date());
}
@SuppressLint("SimpleDateFormat")
public static String getTimestampStrOfDay(Date date) {
return new SimpleDateFormat(DATE_FORMAT_DAY).format(date);
}
}