/*
 *  Copyright (C) 2016  Datamatica (dev@datamatica.pl)
 * 
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU Affero General Public License as published
 *  by the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 * 
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Affero General Public License for more details.
 * 
 *  You should have received a copy of the GNU Affero General Public License
 *  along with this program. If not, see <http://www.gnu.org/licenses/>.
 */
package pl.datamatica.traccar.api.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import java.util.TimeZone;

public class DateUtil {    
    private static final SimpleDateFormat rfc1123 = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss 'GMT'", Locale.US);
    private static final SimpleDateFormat rfc850 = new SimpleDateFormat("EEEE,  dd-MMM-yy HH:mm:ss 'GMT'", Locale.US);
    private static final SimpleDateFormat asctime = new SimpleDateFormat("EEE MMM  d HH:mm:ss yyyy", Locale.US);
    
    static {
        rfc1123.setTimeZone(TimeZone.getTimeZone("GMT"));
        rfc850.setTimeZone(TimeZone.getTimeZone("GMT"));
        asctime.setTimeZone(TimeZone.getTimeZone("GMT"));
    }
    
    public static synchronized Date parseDate(String in) throws ParseException {
        ParseException lastException = null;
        for(SimpleDateFormat format : getSupportedFormats())
            try{
                return format.parse(in);
            } catch(ParseException e) {
                lastException = e;
            }
        throw lastException;
    }
    
    public static synchronized String formatDate(Date in) {
        return rfc1123.format(in);
    }
    
    private static SimpleDateFormat[] getSupportedFormats() {
        return new SimpleDateFormat[]{rfc1123, rfc850, asctime};
    }
}

@@ -53,17 +53,16 @@
 a.pl)%0A *
- 
 %0A *  Thi
@@ -315,25 +315,24 @@
  version.%0A *
- 
 %0A *  This pr
@@ -579,17 +579,16 @@
 ails.%0A *
- 
 %0A *  You
@@ -943,20 +943,16 @@
 teUtil %7B
-    
 %0A    pri
@@ -986,19 +986,20 @@
 eFormat 
-rfc
+RFC_
 1123 = n
@@ -1037,37 +1037,33 @@
 M yyyy HH:mm:ss 
-'GMT'
+z
 %22, Locale.US);%0A 
@@ -1103,19 +1103,20 @@
 eFormat 
-rfc
+RFC_
 850 = ne
@@ -1165,13 +1165,9 @@
 :ss 
-'GMT'
+z
 %22, L
@@ -1219,23 +1219,23 @@
 eFormat 
-asctime
+ASCTIME
  = new S
@@ -1290,20 +1290,16 @@
 le.US);%0A
-    
 %0A    sta
@@ -1312,19 +1312,20 @@
         
-rfc
+RFC_
 1123.set
@@ -1371,19 +1371,20 @@
         
-rfc
+RFC_
 850.setT
@@ -1429,23 +1429,23 @@
         
-asctime
+ASCTIME
 .setTime
@@ -1477,36 +1477,32 @@
 (%22GMT%22));%0A    %7D%0A
-    
 %0A    public stat
@@ -1851,28 +1851,24 @@
 tion;%0A    %7D%0A
-    
 %0A    public 
@@ -1931,19 +1931,20 @@
  return 
-rfc
+RFC_
 1123.for
@@ -1958,20 +1958,16 @@
 ;%0A    %7D%0A
-    
 %0A    pri
@@ -2063,32 +2063,34 @@
 t%5B%5D%7B
-rfc
+RFC_
 1123, 
-rfc
+RFC_
 850, 
-asctime
+ASCTIME
 %7D;%0A 

Allow time zone in RFC 1123 and RFC 850 date formats.