aboutsummaryrefslogtreecommitdiffstats
path: root/src/java/org/apache/poi/ss/util/DateFormatConverter.java
blob: 47d1a0462313e1ecd4f3fa778f782a632d34dbc3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
/* ====================================================================
   Licensed to the Apache Software Foundation (ASF) under one or more
   contributor license agreements.  See the NOTICE file distributed with
   this work for additional information regarding copyright ownership.
   The ASF licenses this file to You under the Apache License, Version 2.0
   (the "License"); you may not use this file except in compliance with
   the License.  You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
==================================================================== */
package org.apache.poi.ss.util;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.poi.util.LocaleID;

/**
 *  Convert java DateFormat patterns into Excel custom number formats.
 *  For example, to format a date in excel using the "dd MMMM, yyyy" pattern and Japanese
 *  locale, use the following code:
 *
 *  <pre><code>
 *      // returns "[$-0411]dd MMMM, yyyy;@" where the [$-0411] prefix tells Excel to use the Japanese locale
 *      String excelFormatPattern = DateFormatConverter.convert(Locale.JAPANESE, "dd MMMM, yyyy");
 *
 *      CellStyle cellStyle = workbook.createCellStyle();
 *
 *      DataFormat poiFormat = workbook.createDataFormat();
 *      cellStyle.setDataFormat(poiFormat.getFormat(excelFormatPattern));
 *      cell.setCellValue(new Date());
 *      cell.setCellStyle(cellStyle);  // formats date as '2012\u5e743\u670817\u65e5'
 *  </code></pre>
 *
 * TODO Generalise this for all Excel format strings
 */
@SuppressWarnings("unused")
public final class DateFormatConverter  {
	private static final Logger LOG = LogManager.getLogger(DateFormatConverter.class);

	private DateFormatConverter() {
	}

	public static class DateFormatTokenizer {
		String format;
		int pos;

		public DateFormatTokenizer(String format) {
			this.format = format;
		}

		public String getNextToken() {
			if( pos >= format.length() ) {
				return null;
			}
			int subStart = pos;
			final char curChar = format.charAt(pos);
			++pos;
			if( curChar == '\'' ) {
				while( ( pos < format.length() ) && ( format.charAt(pos) != '\'' ) ) {
					++pos;
				}
				if( pos < format.length() ) {
					++pos;
				}
			} else {
				while( ( pos < format.length() ) && ( format.charAt(pos) == curChar ) ) {
					++pos;
				}
			}
			return format.substring(subStart,pos);
		}

		public static String[] tokenize( String format ) {
			List<String> result = new ArrayList<>();

			DateFormatTokenizer tokenizer = new DateFormatTokenizer(format);
			String token;
			while( ( token = tokenizer.getNextToken() ) != null ) {
				result.add(token);
			}

			return result.toArray(new String[0]);
		}

		@Override
		public String toString() {
			StringBuilder result = new StringBuilder();

			DateFormatTokenizer tokenizer = new DateFormatTokenizer(format);
			String token;
			while( ( token = tokenizer.getNextToken() ) != null ) {
				if( result.length() > 0 ) {
					result.append( ", " );
				}
				result.append("[").append(token).append("]");
			}

			return result.toString();
		}
	}

	private static Map<String,String> tokenConversions = prepareTokenConversions();

	private static Map<String,String> prepareTokenConversions() {
		Map<String,String> result = new HashMap<>();

		result.put( "EEEE", "dddd" );
		result.put( "EEE", "ddd" );
		result.put( "EE", "ddd" );
		result.put( "E", "d" );
		result.put( "Z", "" );
		result.put( "z", "" );
		result.put( "a", "am/pm" );
		result.put( "A", "AM/PM" );
		result.put( "K", "H" );
		result.put( "KK", "HH" );
		result.put( "k", "h" );
		result.put( "kk", "hh" );
		result.put( "S", "0" );
		result.put( "SS", "00" );
		result.put( "SSS", "000" );
		result.put( "y", "yyyy" );

		return result;
	}

	public static String getPrefixForLocale( Locale locale ) {
		final String languageTag = locale.toLanguageTag();
		if (Locale.ROOT.equals(locale) || "".equals(languageTag)) {
			// JDK 8 adds an empty locale-string, see also https://issues.apache.org/jira/browse/LANG-941
			return "";
		}

		LocaleID loc = LocaleID.lookupByLanguageTag(languageTag);
		if (loc == null) {
			String cmpTag = (languageTag.indexOf('_') > -1) ? languageTag.replace('_', '-') : languageTag;
			int idx = languageTag.length();
			while (loc == null && (idx = cmpTag.lastIndexOf('-', idx - 1)) > 0) {
				loc = LocaleID.lookupByLanguageTag(languageTag.substring(0, idx));
			}
		}

		if (loc == null) {
			LOG.atError().log("Unable to find prefix for Locale '{}' or its parent locales.", languageTag);
			return "";
		}

		return String.format(Locale.ROOT, "[$-%04X]", loc.getLcid());
	}

    public static String convert( Locale locale, DateFormat df ) {
        String ptrn = ((SimpleDateFormat)df).toPattern();
        return convert(locale, ptrn);
    }

    public static String convert( Locale locale, String format ) {
		StringBuilder result = new StringBuilder();

		result.append(getPrefixForLocale(locale));
		DateFormatTokenizer tokenizer = new DateFormatTokenizer(format);
		String token;
		while( ( token = tokenizer.getNextToken() ) != null ) {
			if( token.startsWith("'") ) {
				result.append( token.replace('\'', '"') );
			} else if( ! Character.isLetter( token.charAt( 0 ) ) ) {
				result.append( token );
			} else {
				// It's a code, translate it if necessary
				String mappedToken = tokenConversions.get(token);
				result.append( mappedToken == null ? token : mappedToken );
			}
		}
        result.append(";@");
		return result.toString().trim();
	}

	public static String getJavaDatePattern(int style, Locale locale) {
    	DateFormat df = DateFormat.getDateInstance(style, locale);
    	if( df instanceof SimpleDateFormat ) {
    		return ((SimpleDateFormat)df).toPattern();
    	} else {
    		switch( style ) {
    		case DateFormat.SHORT:
    			return "d/MM/yy";
    		case DateFormat.LONG:
    			return "MMMM d, yyyy";
    		case DateFormat.FULL:
    			return "dddd, MMMM d, yyyy";
    		default:
			case DateFormat.MEDIUM:
    			return "MMM d, yyyy";
    		}
    	}
	}

	public static String getJavaTimePattern(int style, Locale locale) {
    	DateFormat df = DateFormat.getTimeInstance(style, locale);
    	if( df instanceof SimpleDateFormat ) {
    		return ((SimpleDateFormat)df).toPattern();
    	} else {
    		switch( style ) {
    		case DateFormat.SHORT:
    			return "h:mm a";
			default:
    		case DateFormat.MEDIUM:
    		case DateFormat.LONG:
    		case DateFormat.FULL:
    			return "h:mm:ss a";
    		}
    	}
	}

	public static String getJavaDateTimePattern(int style, Locale locale) {
    	DateFormat df = DateFormat.getDateTimeInstance(style, style, locale);
    	if( df instanceof SimpleDateFormat ) {
    		return ((SimpleDateFormat)df).toPattern();
    	} else {
    		switch( style ) {
    		case DateFormat.SHORT:
    			return "M/d/yy h:mm a";
    		case DateFormat.LONG:
    			return "MMMM d, yyyy h:mm:ss a";
    		case DateFormat.FULL:
    			return "dddd, MMMM d, yyyy h:mm:ss a";
    		default:
			case DateFormat.MEDIUM:
    			return "MMM d, yyyy h:mm:ss a";
    		}
    	}
	}

}