001    /**
002     * Copyright (c) 2000-present 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.kernel.util;
016    
017    import java.text.FieldPosition;
018    import java.text.SimpleDateFormat;
019    
020    import java.util.Date;
021    import java.util.Locale;
022    
023    /**
024     * @author Mate Thurzo
025     */
026    public class PortalSimpleDateFormat extends SimpleDateFormat {
027    
028            public PortalSimpleDateFormat(String pattern, Locale locale) {
029                    super(pattern, locale);
030    
031                    if (pattern.equals(DateUtil.ISO_8601_PATTERN)) {
032                            _iso8601Pattern = true;
033                    }
034                    else {
035                            _iso8601Pattern = false;
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 final boolean _iso8601Pattern;
060    
061    }