1 /* 2 * Copyright 2007 Kasper B. Graversen 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 package org.supercsv.cellprocessor; 17 18 import java.text.SimpleDateFormat; 19 import java.util.Date; 20 21 import org.supercsv.cellprocessor.ift.DateCellProcessor; 22 import org.supercsv.cellprocessor.ift.StringCellProcessor; 23 import org.supercsv.exception.SuperCsvCellProcessorException; 24 import org.supercsv.util.CsvContext; 25 26 /** 27 * Converts a date into a formatted string using the {@link SimpleDateFormat} class. If you want to convert from a 28 * String to a Date, use the {@link ParseDate} processor. 29 * <p> 30 * Some example date formats you can use are:<br> 31 * <code>"dd/MM/yyyy"</code> (formats a date as "25/12/2011")<br> 32 * <code>"dd-MMM-yy"</code> (formats a date as "25-Dec-11")<br> 33 * <code>"yyyy.MM.dd.HH.mm.ss"</code> (formats a date as "2011.12.25.08.36.33"<br> 34 * <code>"E, dd MMM yyyy HH:mm:ss Z"</code> (formats a date as "Tue, 25 Dec 2011 08:36:33 -0500")<br> 35 * 36 * @since 1.50 37 * @author Dominique De Vito 38 * @author James Bassett 39 */ 40 public class FmtDate extends CellProcessorAdaptor implements DateCellProcessor { 41 42 private final String dateFormat; 43 44 /** 45 * Constructs a new <tt>FmtDate</tt> processor, which converts a date into a formatted string using 46 * SimpleDateFormat. 47 * 48 * @param dateFormat 49 * the date format String (see {@link SimpleDateFormat}) 50 * @throws NullPointerException 51 * if dateFormat is null 52 */ 53 public FmtDate(final String dateFormat) { 54 super(); 55 checkPreconditions(dateFormat); 56 this.dateFormat = dateFormat; 57 } 58 59 /** 60 * Constructs a new <tt>FmtDate</tt> processor, which converts a date into a formatted string using 61 * SimpleDateFormat, then calls the next processor in the chain. 62 * 63 * @param dateFormat 64 * the date format String (see {@link SimpleDateFormat}) 65 * @param next 66 * the next processor in the chain 67 * @throws NullPointerException 68 * if dateFormat or next is null 69 */ 70 public FmtDate(final String dateFormat, final StringCellProcessor next) { 71 super(next); 72 checkPreconditions(dateFormat); 73 this.dateFormat = dateFormat; 74 } 75 76 /** 77 * Checks the preconditions for creating a new FmtDate processor. 78 * 79 * @param dateFormat 80 * the date format String 81 * @throws NullPointerException 82 * if dateFormat is null 83 */ 84 private static void checkPreconditions(final String dateFormat) { 85 if( dateFormat == null ) { 86 throw new NullPointerException("dateFormat should not be null"); 87 } 88 } 89 90 /** 91 * {@inheritDoc} 92 * 93 * @throws SuperCsvCellProcessorException 94 * if value is null or is not a Date, or if dateFormat is not a valid date format 95 */ 96 public Object execute(final Object value, final CsvContext context) { 97 validateInputNotNull(value, context); 98 99 if( !(value instanceof Date) ) { 100 throw new SuperCsvCellProcessorException(Date.class, value, context, this); 101 } 102 103 final SimpleDateFormat formatter; 104 try { 105 formatter = new SimpleDateFormat(dateFormat); 106 } 107 catch(IllegalArgumentException e) { 108 throw new SuperCsvCellProcessorException(String.format("'%s' is not a valid date format", dateFormat), 109 context, this, e); 110 } 111 112 String result = formatter.format((Date) value); 113 return next.execute(result, context); 114 } 115 }