Skip to content

Commit 6463687

Browse files
committed
feat: Added a util that allows ease of use for generating Discord timestamps.
1 parent 7e900fe commit 6463687

File tree

1 file changed

+65
-0
lines changed

1 file changed

+65
-0
lines changed
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
package com.seailz.jdaframework.utils;
2+
3+
import java.util.Date;
4+
5+
/**
6+
* A util for creating timestamps, with ease.
7+
* @author Seailz
8+
*/
9+
public class TimestampUtil {
10+
11+
/**
12+
* Generates a Discord timestamp
13+
* @param type The type of timestamp you want to create
14+
* @param time The time it will represent
15+
* @return The generated timestamp
16+
*/
17+
public static String createTimestamp(Type type, Date time) {
18+
if (type == Type.BASE)
19+
return String.valueOf(time.getTime());
20+
String base = "<t:" + time.getTime() + ":";
21+
22+
switch (type) {
23+
case SHORT_DATE:
24+
base += "d";
25+
case LONG_DATE:
26+
base += "D";
27+
case SHORT_TIME:
28+
base += "t";
29+
case LONG_TIME:
30+
base += "T";
31+
case DATE_AND_TIME:
32+
base += "f";
33+
case LONG_DATE_AND_TIME:
34+
base += "F";
35+
case AGO:
36+
base += "R";
37+
}
38+
39+
base += ">";
40+
return base;
41+
}
42+
43+
/**
44+
* Timestamp types
45+
*/
46+
public enum Type {
47+
SHORT_DATE,
48+
// For example: 26/07/2022
49+
LONG_DATE,
50+
// For example: 26 July 2022
51+
SHORT_TIME,
52+
// For example 19:50
53+
LONG_TIME,
54+
// For example: 19:50:43
55+
DATE_AND_TIME,
56+
// For example: 26 July 2022 19:50
57+
LONG_DATE_AND_TIME,
58+
// For example: Tuesday, 26 July 2022 19:50
59+
AGO,
60+
// For example: 10 minutes ago
61+
BASE
62+
// For example: 1658861400
63+
}
64+
65+
}

0 commit comments

Comments
 (0)