001/* 002 * Licensed under the Apache License, Version 2.0 (the "License"); 003 * you may not use this file except in compliance with the License. 004 * You may obtain a copy of the License at 005 * 006 * http://www.apache.org/licenses/LICENSE-2.0 007 * 008 * Unless required by applicable law or agreed to in writing, software 009 * distributed under the License is distributed on an "AS IS" BASIS, 010 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 011 * See the License for the specific language governing permissions and 012 * limitations under the License. 013 */ 014package org.gbif.ws.client; 015 016import java.text.ParseException; 017import java.text.SimpleDateFormat; 018import java.util.Date; 019 020import org.slf4j.Logger; 021import org.slf4j.LoggerFactory; 022 023import feign.Param; 024 025/** 026 * Convert date string from the default {@link Date} format to custom 'yyyy-MM'. 027 */ 028public class PartialDateExpander implements Param.Expander { 029 030 private static final Logger LOG = LoggerFactory.getLogger(PartialDateExpander.class); 031 032 private static final String UTIL_DATE_FORMAT_PATTERN = "EEE MMM dd HH:mm:ss zzz yyyy"; 033 private static final String CUSTOM_DATE_FORMAT_PATTERN = "yyyy-MM"; 034 035 @Override 036 public String expand(Object value) { 037 return convertDate(value); 038 } 039 040 private String convertDate(Object rawDate) { 041 Date utilDate; 042 043 try { 044 utilDate = new SimpleDateFormat(UTIL_DATE_FORMAT_PATTERN).parse(rawDate.toString()); 045 } catch (ParseException e) { 046 LOG.error("Wrong date format {}. Expected format: {}", rawDate, UTIL_DATE_FORMAT_PATTERN); 047 return null; 048 } 049 050 return new SimpleDateFormat(CUSTOM_DATE_FORMAT_PATTERN).format(utilDate); 051 } 052}