001    /**
002     * Licensed to the Apache Software Foundation (ASF) under one
003     * or more contributor license agreements.  See the NOTICE file
004     * distributed with this work for additional information
005     * regarding copyright ownership.  The ASF licenses this file
006     * to you under the Apache License, Version 2.0 (the
007     * "License"); you may not use this file except in compliance
008     * with the License.  You may obtain a copy of the License at
009     * 
010     *      http://www.apache.org/licenses/LICENSE-2.0
011     * 
012     * Unless required by applicable law or agreed to in writing, software
013     * distributed under the License is distributed on an "AS IS" BASIS,
014     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015     * See the License for the specific language governing permissions and
016     * limitations under the License.
017     */
018    package org.apache.oozie.client.rest;
019    
020    import java.text.ParseException;
021    import java.text.SimpleDateFormat;
022    import java.util.ArrayList;
023    import java.util.Date;
024    import java.util.List;
025    import java.util.Locale;
026    import java.util.TimeZone;
027    
028    import org.json.simple.JSONArray;
029    import org.json.simple.JSONObject;
030    
031    
032    /**
033     * Json utils methods.
034     */
035    public class JsonUtils {
036    
037        /**
038         * Format a Date in RFC822 with the given time zone.
039         *
040         * @param date date to format.
041         * @param timeZoneId the time zone to use
042         * @return RFC822 for the date, <code>null</code> if the date was <code>null</null>.
043         */
044        public static String formatDateRfc822(Date date, String timeZoneId) {
045            if (date != null) {
046                TimeZone tZone = TimeZone.getTimeZone(timeZoneId);
047                SimpleDateFormat dateFormater = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss zzz", Locale.US);
048                dateFormater.setTimeZone(tZone);
049                return dateFormater.format(date);
050            }
051            return null;
052        }
053        
054        /**
055         * Format a Date in RFC822 GMT.
056         *
057         * @param date date to format.
058         * @return RFC822 GMT for the date, <code>null</code> if the date was <code>null</null>.
059         */
060        public static String formatDateRfc822(Date date) {
061            return formatDateRfc822(date, "GMT");
062        }
063    
064        /**
065         * Parse a string in RFC822 GMT format.
066         *
067         * @param str string to parse.
068         * @return parsed date, <code>null</code> if the string was <code>null</null> or in an invalid format.
069         */
070        static Date parseDateRfc822(String str) {
071            if (str != null) {
072                try {
073                    SimpleDateFormat dateFormater = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss 'GMT'", Locale.US);
074                    dateFormater.setTimeZone(TimeZone.getTimeZone("GMT"));
075                    return dateFormater.parse(str);
076                }
077                catch (ParseException ex) {
078                    return null;
079                }
080            }
081            return null;
082        }
083    
084    }