1 package net.sourceforge.blogentis.utils;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25 import java.text.DateFormat;
26 import java.text.SimpleDateFormat;
27 import java.util.Date;
28 import java.util.Locale;
29 import java.util.regex.Matcher;
30 import java.util.regex.Pattern;
31
32 /***
33 * Date manipulation utilities
34 *
35 * @author abas
36 */
37 public class DateUtils {
38 private static final DateFormat iso8601Format = new SimpleDateFormat(
39 "yyyy-MM-dd'T'hh:mm:ssZ", new Locale("en", "us"));
40 private static final DateFormat rfc822Format = new SimpleDateFormat(
41 "EEE, d MMM yyyy hh:mm:ss Z", new Locale("en", "us"));
42
43 private static final Pattern iso8601colonPattern = Pattern
44 .compile("^(.*)([:]?)([0-9][0-9])$");
45
46 public static String formatIso8601(Date date) {
47 String v = iso8601Format.format(date);
48
49 Matcher m = iso8601colonPattern.matcher(v);
50 if (m.find() && (m.group(2) == null || m.group(2).length() != 1)) {
51 v = m.group(1) + ":" + m.group(3);
52 }
53 return v;
54 }
55
56 public static String formatRFC822(Date date) {
57 return rfc822Format.format(date);
58 }
59 }