Java时间

时间工具

joda-time

判断当前时间是否处在某个区间(用字符串表示的两个时间点)

1
2
3
4
5
6
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.3.1</version>
<type>jar</type>
</dependency>
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
/**
* 判断当前时间是否处在某个区间(用字符串表示的两个时间点)
* https://www.programcreek.com/java-api-examples/?class=org.apache.commons.lang.time.DateUtils&method=parseDate
* org.apache.commons.lang.time.DateUtils.parseDate
*
* @return
*/
public static boolean isNowSuitable() {
String startTime = "2019-01-12";
String endTime = "2019-08-12";
if (StringUtils.isEmpty(startTime) || StringUtils.isEmpty(endTime)) {
return false;
}

try {
String[] parsePatterns = {"yyyy-MM-dd"}; // "yyyy-MM-dd HH:mm:ss"
Date startDate = DateUtils.parseDate(startTime, parsePatterns);
Date endDate = DateUtils.parseDate(endTime, parsePatterns);
long now = Calendar.getInstance().getTimeInMillis();
if (now >= startDate.getTime() && now <= endDate.getTime()) {
return true;
}

} catch (ParseException e) {
e.printStackTrace();
}
return false;
}

iso 时间转换

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

import com.alibaba.fastjson.annotation.JSONField;
import com.alibaba.fastjson.serializer.SerializerFeature;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimeZone;

/**
* <p>类的详细说明</p>
*
* @author lyloou
* @author 其他作者姓名
* @version 1.00 2020/10/30 , 星期五 lyloou 创建
* <p>1.01 YYYY/MM/DD 修改者姓名 修改内容说明</p>
*/
public class A {
public static void main(String[] args) throws Exception {
new A().test();
}

public static String toISO8601UTC(Date date) {
TimeZone tz = TimeZone.getTimeZone("UTC");
DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
df.setTimeZone(tz);
return df.format(date);
}

public static Date fromISO8601UTC(String dateStr) {
TimeZone tz = TimeZone.getTimeZone("UTC");
DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
df.setTimeZone(tz);

try {
return df.parse(dateStr);
} catch (ParseException e) {
e.printStackTrace();
}

return null;
}

public void test() {
String input = "2020-11-05T16:00:00.000Z"; // 对应的日期时间是:Fri Nov 06 00:00:00 CST 2020
Date date = fromISO8601UTC(input);
System.out.println(date);
DateFormat df1 = new SimpleDateFormat("yyyy-MM-dd");
System.out.println(df1.format(date));

Model model = new Model();
model.startTime = date;
String data = com.alibaba.fastjson.JSON.toJSONStringWithDateFormat(model, "yyyy-MM-dd", SerializerFeature.DisableCircularReferenceDetect);
System.out.println(data);
}

public static class Model {
@JSONField(format = "yyyy-MM-dd hh:mm")
public Date startTime;

@JSONField(format = "yyyy-MM-dd")
public Date endTime;

@Override
public String toString() {
return "Model{" +
"startTime=" + startTime +
", endTime=" + endTime +
'}';
}
}
}