001    /**
002     * Copyright 2007 Mike Kroutikov.
003     *
004     * This program is free software; you can redistribute it and/or modify
005     *   it under the terms of the Lesser GNU General Public License as 
006     *   published by the Free Software Foundation; either version 3 of
007     *   the License, or (at your option) any later version.
008     *
009     *   This program is distributed in the hope that it will be useful,
010     *   but WITHOUT ANY WARRANTY; without even the implied warranty of
011     *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
012     *   Lesser GNU General Public License for more details.
013     *
014     *   You should have received a copy of the Lesser GNU General Public License
015     *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
016     */
017    
018    package org.otfeed.samples;
019    
020    import java.text.DateFormat;
021    import java.text.SimpleDateFormat;
022    import java.util.Date;
023    
024    /**
025     * Utility for property value formatting.
026     * 
027     * Most importantly, knows how to format date as Unix timestamp, 
028     * or as a simple human-readable string.
029     */
030    public final class FormatUtil {
031            
032            private FormatUtil() { }
033    
034            public static final IPropertyFormatter DATE_FORMAT_AS_UNIX_TIMESTAMP =
035                    new IPropertyFormatter() {
036                    public String format(Object obj) {
037                            Date date = (Date) obj;
038    
039                            return "" + (int) ((date.getTime() / 1000L));
040                    }
041            };
042    
043            private static final DateFormat dateFormat 
044            = new SimpleDateFormat("MM/dd/yyyy HH:mm.ss");
045    
046            public static final IPropertyFormatter DATE_FORMAT_AS_STRING = 
047                    new IPropertyFormatter() {
048                    public String format(Object obj) {
049                            return dateFormat.format((Date) obj);
050                    }
051            };
052    }