-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DTM.java
169 lines (143 loc) · 4.35 KB
/
DTM.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
// ADJUST THE PACKAGE PATH
package kang.cahya.com.utils;
// import dependencies
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
/*
*
* Date Time Manipulation (DTM)
*
*
* Blog : https://www.kang-cahya.com
* Github Repository : https://github.com/dyazincahya/DTM-helper-java
* 2021 © kang cahya
*
*/
public class DTM {
private Date now;
private Locale locale;
public DTM() {
this.now = new Date();
this.locale = new Locale("in", "ID");
}
public DTM(Date now) {
this.now = now;
this.locale = new Locale("in", "ID");
}
public DTM(Date now, Locale locale) {
this.now = now;
this.locale = locale;
}
// fill in positive numbers to add days (1,2,3, etc), and;
// fill in negative numbers to subtract days (-1, -2, -3, etc)
/*
Example :
DTM dtm = new DTM();
dtm.day(-1).getTime();
*/
public Calendar day(int day) {
Calendar cal = Calendar.getInstance();
cal.setTime(now);
cal.add(Calendar.DATE, day);
return cal;
}
// fill in positive numbers to add month (1,2,3, etc), and;
// fill in negative numbers to subtract month (-1, -2, -3, etc)
/*
Example :
DTM dtm = new DTM();
dtm.month(-2).getTime();
*/
public Calendar month(int month) {
Calendar cal = Calendar.getInstance();
cal.setTime(now);
cal.add(Calendar.MONTH, month);
return cal;
}
// fill in positive numbers to add year (1,2,3, etc), and;
// fill in negative numbers to subtract year (-1, -2, -3, etc)
/*
Example :
DTM dtm = new DTM();
dtm.year(-2).getTime();
*/
public Calendar year(int year) {
Calendar cal = Calendar.getInstance();
cal.setTime(now);
cal.add(Calendar.YEAR, year);
return cal;
}
// fill in positive numbers to add hour (1,2,3, etc), and;
// fill in negative numbers to subtract hour (-1, -2, -3, etc)
/*
Example :
DTM dtm = new DTM();
dtm.hour(-2).getTime();
*/
public Calendar hour(int hour) {
Calendar cal = Calendar.getInstance();
cal.setTime(now);
cal.add(Calendar.HOUR, hour);
return cal;
}
// fill in positive numbers to add minute (1,2,3, etc), and;
// fill in negative numbers to subtract minute (-1, -2, -3, etc)
/*
Example :
DTM dtm = new DTM();
dtm.minute(-2).getTime();
*/
public Calendar minute(int minute) {
Calendar cal = Calendar.getInstance();
cal.setTime(now);
cal.add(Calendar.MINUTE, minute);
return cal;
}
// fill in positive numbers to add second (1,2,3, etc), and;
// fill in negative numbers to subtract second (-1, -2, -3, etc)
/*
Example :
DTM dtm = new DTM();
dtm.second(-2).getTime();
*/
public Calendar second(int second) {
Calendar cal = Calendar.getInstance();
cal.setTime(now);
cal.add(Calendar.MINUTE, second);
return cal;
}
// fill in positive numbers to add milisecond (1,2,3, etc), and;
// fill in negative numbers to subtract milisecond (-1, -2, -3, etc)
/*
Example :
DTM dtm = new DTM();
dtm.milisecond(-2).getTime();
*/
public Calendar milisecond(int milisecond) {
Calendar cal = Calendar.getInstance();
cal.setTime(now);
cal.add(Calendar.MILLISECOND, milisecond);
return cal;
}
/*
Example :
DTM dtm = new DTM();
dtm.reFormat(dtm.day(-1).getTime(),"ddMMyyyy");
*/
public String reFormat(Date date, String pattern){
SimpleDateFormat formatter = new SimpleDateFormat(pattern, locale);
return formatter.format(date);
}
/*
Example :
DTM dtm = new DTM();
dtm.stringToDate("31/12/1998","dd/MM/yyyy");
Result : Thu Dec 31 00:00:00 IST 1998
*/
public Date stringToDate(String dateString, String pattern) throws ParseException {
return new SimpleDateFormat(pattern, locale).parse(dateString);
}
}