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 019package org.apache.oozie.client.rest; 020 021import java.text.ParseException; 022import java.text.SimpleDateFormat; 023import java.util.ArrayList; 024import java.util.Date; 025import java.util.List; 026import java.util.Locale; 027import java.util.TimeZone; 028 029import org.json.simple.JSONArray; 030import org.json.simple.JSONObject; 031 032 033/** 034 * Json utils methods. 035 */ 036public class JsonUtils { 037 038 /** 039 * Format a Date in RFC822 with the given time zone. 040 * 041 * @param date date to format. 042 * @param timeZoneId the time zone to use 043 * @return RFC822 for the date, <code>null</code> if the date was <code>null</null>. 044 */ 045 public static String formatDateRfc822(Date date, String timeZoneId) { 046 if (date != null) { 047 TimeZone tZone = TimeZone.getTimeZone(timeZoneId); 048 SimpleDateFormat dateFormater = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss zzz", Locale.US); 049 dateFormater.setTimeZone(tZone); 050 return dateFormater.format(date); 051 } 052 return null; 053 } 054 055 /** 056 * Format a Date in RFC822 GMT. 057 * 058 * @param date date to format. 059 * @return RFC822 GMT for the date, <code>null</code> if the date was <code>null</null>. 060 */ 061 public static String formatDateRfc822(Date date) { 062 return formatDateRfc822(date, "GMT"); 063 } 064 065 /** 066 * Parse a string in RFC822 GMT format. 067 * 068 * @param str string to parse. 069 * @return parsed date, <code>null</code> if the string was <code>null</null> or in an invalid format. 070 */ 071 static Date parseDateRfc822(String str) { 072 if (str != null) { 073 try { 074 SimpleDateFormat dateFormater = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss 'GMT'", Locale.US); 075 dateFormater.setTimeZone(TimeZone.getTimeZone("GMT")); 076 return dateFormater.parse(str); 077 } 078 catch (ParseException ex) { 079 return null; 080 } 081 } 082 return null; 083 } 084 085}