View Javadoc

1   package net.sourceforge.blogentis.utils;
2   
3   //-----------------------------------------------------------------------
4   //Blogentis - a blog publishing platform.
5   //Copyright (C) 2004 Tassos Bassoukos <abassouk@gmail.com>
6   //
7   //This library is free software; you can redistribute it and/or
8   //modify it under the terms of the GNU Lesser General Public
9   //License as published by the Free Software Foundation; either
10  //version 2.1 of the License, or (at your option) any later version.
11  //
12  //This library is distributed in the hope that it will be useful,
13  //but WITHOUT ANY WARRANTY; without even the implied warranty of
14  //MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  //Lesser General Public License for more details.
16  //
17  //You should have received a copy of the GNU Lesser General Public
18  //License along with this library; if not, write to the Free Software
19  //Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20  //-----------------------------------------------------------------------
21  //
22  //$Id: DateUtils.java,v 1.1 2004/10/22 17:34:05 tassos Exp $
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  }