/* * RapidMiner * * Copyright (C) 2001-2011 by Rapid-I and the contributors * * Complete list of developers available at our web site: * * http://rapid-i.com * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU Affero General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Affero General Public License for more details. * * You should have received a copy of the GNU Affero General Public License * along with this program. If not, see http://www.gnu.org/licenses/. */ package com.rapidminer.tools.math.function.expressions.date; import java.text.DateFormat; import java.util.Calendar; import java.util.Locale; import java.util.Stack; import org.nfunk.jep.ParseException; import org.nfunk.jep.function.PostfixMathCommand; import com.rapidminer.tools.math.function.ExpressionParserConstants; import com.rapidminer.tools.math.function.UnknownValue; /** * Changes a Calendar object to a String using a given locale. * * @author Marco Boeck */ public class Date2StringWithLocale extends PostfixMathCommand { public Date2StringWithLocale() { numberOfParameters = 4; } /** * Creates the string result. */ @SuppressWarnings("unchecked") @Override public void run(Stack stack) throws ParseException { checkStack(stack); Locale locale = Locale.getDefault(); Object localeObject = stack.pop(); if (!(localeObject instanceof String)) { throw new ParseException("Invalid argument type for 'date_str_loc', fourth argument must be (String) for locale (e.g. \"en\")"); } locale = new Locale(String.valueOf(localeObject)); Object constantShowObject = stack.pop(); if (!(constantShowObject instanceof String)) { throw new ParseException("Invalid argument type for 'date_str_loc', third argument must be show constant (e.g. DATE_SHOW_DATE_ONLY)"); } String constantShow = (String)constantShowObject; Object constantFormatObject = stack.pop(); if (!(constantFormatObject instanceof String)) { throw new ParseException("Invalid argument type for 'date_str_loc', second argument must be formatting constant (e.g. DATE_FULL)"); } String constantFormat = (String)constantFormatObject; Object dateObject = stack.pop(); // check for unknown values if (dateObject == UnknownValue.UNKNOWN_DATE) { stack.push(UnknownValue.UNKNOWN_NOMINAL); return; } if (!(dateObject instanceof Calendar)) { throw new ParseException("Invalid argument type for 'date_str_loc', first argument must be Calendar"); } Calendar dateCal = (Calendar)dateObject; String result; DateFormat dateFormat; int formatting; if (constantFormat.equals(ExpressionParserConstants.DATE_FORMAT_FULL)) { formatting = DateFormat.FULL; } else if (constantFormat.equals(ExpressionParserConstants.DATE_FORMAT_LONG)) { formatting = DateFormat.LONG; } else if (constantFormat.equals(ExpressionParserConstants.DATE_FORMAT_MEDIUM)) { formatting = DateFormat.MEDIUM; } else if (constantFormat.equals(ExpressionParserConstants.DATE_FORMAT_SHORT)) { formatting = DateFormat.SHORT; } else { throw new ParseException("Invalid format constant for 'date_str_loc'"); } if (constantShow.equals(ExpressionParserConstants.DATE_SHOW_DATE_ONLY)) { dateFormat = DateFormat.getDateInstance(formatting, locale); } else if (constantShow.equals(ExpressionParserConstants.DATE_SHOW_TIME_ONLY)) { dateFormat = DateFormat.getTimeInstance(formatting, locale); } else if (constantShow.equals(ExpressionParserConstants.DATE_SHOW_DATE_AND_TIME)) { dateFormat = DateFormat.getDateTimeInstance(formatting, formatting, locale); } else { throw new ParseException("Invalid show constant for 'date_str_loc'"); } result = dateFormat.format(dateCal.getTime()); stack.push(result); } }