-
Notifications
You must be signed in to change notification settings - Fork 108
/
Formats.java
137 lines (127 loc) · 3.47 KB
/
Formats.java
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
/*
* Copyright (c) 2018.
*
* This file is part of AvaIre.
*
* AvaIre is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* AvaIre is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with AvaIre. If not, see <https://www.gnu.org/licenses/>.
*
*
*/
package com.avairebot.time;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
public enum Formats {
/**
* Generates a date time string, example:
* <p>
* 1975-12-25 14:15:16
*/
DATE_TIME("yyyy-MM-dd HH:mm:ss"),
/**
* Generates a date string, example:
* <p>
* 1975-12-25
*/
DATE("yyyy-MM-dd"),
/**
* Generates a formatted date string, example:
* <p>
* Dec 25, 1975
*/
FORMATTED_DATE("MMM dd, yyyy"),
/**
* Generates a time string, example:
* <p>
* 14:15:16
*/
TIME("HH:mm:ss"),
/**
* Generates a time offset time string, example:
* <p>
* 14:15:16-05:00
*/
TIME_OFFSET("HH:mm:ssXXX"),
/**
* Generates a day date time string, example:
* <p>
* Thu, Dec 25, 1975 2:15 PM
*/
DAY_DATE_TIME("EEE, MMM dd, yyyy h:mm aaa"),
/**
* Generates a cookie time string, example:
* <p>
* Thursday, 25-Dec-1975 14:15:16 EST
*/
COOKIE("EEEEEEEE, dd-MMM-yyyy HH:mm:ss z"),
/**
* Generates a RFC 822 time string, example:
* <p>
* Thu, 25 Dec 1975 14:15:16 -0500
*/
RFC_822("EEE, dd MMM yyyy HH:mm:ss Z"),
/**
* Generates a RFC 850 time string, example:
* <p>
* Thursday, 25-Dec-1975 14:15:16 EST
*/
RFC_850("EEEEEEEE, dd-MMM-yyyy HH:mm:ss z"),
/**
* Generates a RFC 1036 time string, example:
* <p>
* 1975-12-25T14:15:16-05:00
*/
RFC_1036("EEE, dd MMM yyyy HH:mm:ssXXX"),
/**
* Generates a RSS time string, example:
* <p>
* Thu, 25 Dec 1975 14:15:16 -0500
*/
RSS("EEE, dd MMM yyyy HH:mm:ss Z");
private final String string;
Formats(String string) {
this.string = string;
}
/**
* Gets the {@link java.text.SimpleDateFormat} format as a string.
*
* @return the {@link java.text.SimpleDateFormat} format as a string.
*/
public String getFormat() {
return string;
}
/**
* Creates a {@link java.text.SimpleDateFormat} object from the format string.
*
* @return the {@link java.text.SimpleDateFormat} format what was created.
*/
public SimpleDateFormat make() {
return new SimpleDateFormat(string, Locale.ENGLISH);
}
/**
* Attempts to parse the provided time string with the format.
*
* @param time The time string to try and parse
* @return the created {@link java.util.Date} object from the time string.
* @throws ParseException if the provided time doesn't match the provided format.
*/
public Date parse(String time) throws ParseException {
return make().parse(time);
}
@Override
public String toString() {
return string;
}
}