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