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 GMT.
039         *
040         * @param date date to format.
041         * @return RFC822 GMT for the date, <code>null</code> if the date was <code>null</null>.
042         */
043        public static String formatDateRfc822(Date date) {
044            if (date != null) {
045                SimpleDateFormat dateFormater = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss 'GMT'", Locale.US);
046                dateFormater.setTimeZone(TimeZone.getTimeZone("GMT"));
047                return dateFormater.format(date);
048            }
049            return null;
050        }
051    
052        /**
053         * Parse a string in RFC822 GMT format.
054         *
055         * @param str string to parse.
056         * @return parsed date, <code>null</code> if the string was <code>null</null> or in an invalid format.
057         */
058        static Date parseDateRfc822(String str) {
059            if (str != null) {
060                try {
061                    SimpleDateFormat dateFormater = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss 'GMT'", Locale.US);
062                    dateFormater.setTimeZone(TimeZone.getTimeZone("GMT"));
063                    return dateFormater.parse(str);
064                }
065                catch (ParseException ex) {
066                    return null;
067                }
068            }
069            return null;
070        }
071    
072    }