001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.util;
016    
017    import com.liferay.portal.kernel.util.DateUtil;
018    import com.liferay.portal.kernel.util.StringPool;
019    
020    import java.text.FieldPosition;
021    import java.text.SimpleDateFormat;
022    
023    import java.util.Date;
024    import java.util.Locale;
025    
026    /**
027     * @author Mate Thurzo
028     */
029    public class PortalSimpleDateFormat extends SimpleDateFormat {
030    
031            public PortalSimpleDateFormat(String pattern, Locale locale) {
032                    super(pattern, locale);
033    
034                    if (pattern.equals(DateUtil.ISO_8601_PATTERN)) {
035                            _iso8601Pattern = true;
036                    }
037            }
038    
039            @Override
040            public StringBuffer format(
041                    Date date, StringBuffer toAppendToSB, FieldPosition fieldPosition) {
042    
043                    StringBuffer originalSB = super.format(
044                            date, toAppendToSB, fieldPosition);
045    
046                    if (!_iso8601Pattern) {
047                            return originalSB;
048                    }
049    
050                    StringBuffer modifiedSB = new StringBuffer();
051    
052                    modifiedSB.append(originalSB.substring(0, 22));
053                    modifiedSB.append(StringPool.COLON);
054                    modifiedSB.append(originalSB.substring(22));
055    
056                    return modifiedSB;
057            }
058    
059            private boolean _iso8601Pattern;
060    
061    }