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.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                    else {
038                            _iso8601Pattern = false;
039                    }
040            }
041    
042            @Override
043            public StringBuffer format(
044                    Date date, StringBuffer toAppendToSB, FieldPosition fieldPosition) {
045    
046                    StringBuffer originalSB = super.format(
047                            date, toAppendToSB, fieldPosition);
048    
049                    if (!_iso8601Pattern) {
050                            return originalSB;
051                    }
052    
053                    StringBuffer modifiedSB = new StringBuffer();
054    
055                    modifiedSB.append(originalSB.substring(0, 22));
056                    modifiedSB.append(StringPool.COLON);
057                    modifiedSB.append(originalSB.substring(22));
058    
059                    return modifiedSB;
060            }
061    
062            private final boolean _iso8601Pattern;
063    
064    }