RelativeDateFormat.java
1.77 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
/*
* Decompiled with CFR 0_118.
*/
package com.day.cq.analytics.sitecatalyst.util;
import java.text.DateFormatSymbols;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Locale;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class RelativeDateFormat
extends SimpleDateFormat {
protected Date base = new Date();
protected static final String RELATIVE_DATE_REGEX = "([+-]\\d+)([dDmMyY])";
protected Pattern pattern = Pattern.compile("([+-]\\d+)([dDmMyY])");
public RelativeDateFormat() {
}
public RelativeDateFormat(String pattern) {
super(pattern);
}
public RelativeDateFormat(String pattern, DateFormatSymbols formatSymbols) {
super(pattern, formatSymbols);
}
public RelativeDateFormat(String pattern, Locale locale) {
super(pattern, locale);
}
public Date parseRelative(String text) throws ParseException {
Matcher matcher = this.pattern.matcher(text);
if (matcher.matches()) {
int amount = Integer.parseInt(matcher.group(1).replace("+", ""));
String unit = matcher.group(2).toLowerCase();
GregorianCalendar cal = new GregorianCalendar();
cal.setTime(this.base);
int field = 0;
if (unit.equals("d")) {
field = 5;
} else if (unit.equals("m")) {
field = 2;
} else if (unit.equals("y")) {
field = 1;
}
cal.add(field, amount);
return cal.getTime();
}
return this.parse(text);
}
public void setBaseDate(Date base) {
if (base != null) {
this.base = base;
}
}
}