Search in sources :

Example 71 with Format

use of java.text.Format in project kie-uberfire-extensions by kiegroup.

the class FormatTagSupport method doEndTag.

public int doEndTag() throws JspException {
    try {
        Object value = getValue();
        JspWriter out = pageContext.getOut();
        String text = null;
        if (value != null) {
            Format formatter = getFormat();
            if (formatter == null) {
                throw new JspTagException(this._tagname + " tag, could not find valid Format instance");
            }
            text = formatter.format(value);
        } else {
            text = getDefaultText();
        }
        if (text != null) {
            out.print(text);
        }
    } catch (IOException e) {
        handleIOException(e);
    }
    return EVAL_PAGE;
}
Also used : Format(java.text.Format) DateFormat(java.text.DateFormat) IOException(java.io.IOException) JspWriter(javax.servlet.jsp.JspWriter) JspTagException(javax.servlet.jsp.JspTagException)

Example 72 with Format

use of java.text.Format in project pentaho-kettle by pentaho.

the class ScriptValuesAddedFunctions method getFiscalDate.

public static Object getFiscalDate(Context actualContext, Scriptable actualObject, Object[] ArgList, Function FunctionContext) {
    if (ArgList.length == 2) {
        try {
            if (isNull(ArgList)) {
                return null;
            } else if (isUndefined(ArgList)) {
                return Context.getUndefinedValue();
            }
            java.util.Date dIn = (java.util.Date) Context.jsToJava(ArgList[0], java.util.Date.class);
            Calendar startDate = Calendar.getInstance();
            Calendar fisStartDate = Calendar.getInstance();
            Calendar fisOffsetDate = Calendar.getInstance();
            startDate.setTime(dIn);
            Format dfFormatter = new SimpleDateFormat("dd.MM.yyyy");
            String strOffsetDate = Context.toString(ArgList[1]) + String.valueOf(startDate.get(Calendar.YEAR));
            java.util.Date dOffset = (java.util.Date) dfFormatter.parseObject(strOffsetDate);
            fisOffsetDate.setTime(dOffset);
            String strFisStartDate = "01.01." + String.valueOf(startDate.get(Calendar.YEAR) + 1);
            fisStartDate.setTime((java.util.Date) dfFormatter.parseObject(strFisStartDate));
            int iDaysToAdd = (int) ((startDate.getTimeInMillis() - fisOffsetDate.getTimeInMillis()) / 86400000);
            fisStartDate.add(Calendar.DATE, iDaysToAdd);
            return fisStartDate.getTime();
        } catch (Exception e) {
            throw Context.reportRuntimeError(e.toString());
        }
    } else {
        throw Context.reportRuntimeError("The function call getFiscalDate requires 2 arguments.");
    }
}
Also used : Format(java.text.Format) DateFormat(java.text.DateFormat) SimpleDateFormat(java.text.SimpleDateFormat) DecimalFormat(java.text.DecimalFormat) Date(java.util.Date) Calendar(java.util.Calendar) SimpleDateFormat(java.text.SimpleDateFormat) Date(java.util.Date) KettleFileException(org.pentaho.di.core.exception.KettleFileException) FileNotFoundException(java.io.FileNotFoundException) WrappedException(org.mozilla.javascript.WrappedException) EvaluatorException(org.mozilla.javascript.EvaluatorException) JavaScriptException(org.mozilla.javascript.JavaScriptException) IOException(java.io.IOException)

Example 73 with Format

use of java.text.Format in project pentaho-kettle by pentaho.

the class ScriptAddedFunctions method getFiscalDate.

public static Object getFiscalDate(ScriptEngine actualContext, Bindings actualObject, Object[] ArgList, Object FunctionContext) {
    if (ArgList.length == 2) {
        try {
            if (isNull(ArgList)) {
                return null;
            } else if (isUndefined(ArgList)) {
                return undefinedValue;
            }
            java.util.Date dIn = (java.util.Date) ArgList[0];
            Calendar startDate = Calendar.getInstance();
            Calendar fisStartDate = Calendar.getInstance();
            Calendar fisOffsetDate = Calendar.getInstance();
            startDate.setTime(dIn);
            Format dfFormatter = new SimpleDateFormat("dd.MM.yyyy");
            String strOffsetDate = (String) ArgList[1] + String.valueOf(startDate.get(Calendar.YEAR));
            java.util.Date dOffset = (java.util.Date) dfFormatter.parseObject(strOffsetDate);
            fisOffsetDate.setTime(dOffset);
            String strFisStartDate = "01.01." + String.valueOf(startDate.get(Calendar.YEAR) + 1);
            fisStartDate.setTime((java.util.Date) dfFormatter.parseObject(strFisStartDate));
            int iDaysToAdd = (int) ((startDate.getTimeInMillis() - fisOffsetDate.getTimeInMillis()) / 86400000);
            fisStartDate.add(Calendar.DATE, iDaysToAdd);
            return fisStartDate.getTime();
        } catch (Exception e) {
            throw new RuntimeException(e.toString());
        }
    } else {
        throw new RuntimeException("The function call getFiscalDate requires 2 arguments.");
    }
}
Also used : Format(java.text.Format) DateFormat(java.text.DateFormat) SimpleDateFormat(java.text.SimpleDateFormat) DecimalFormat(java.text.DecimalFormat) Date(java.util.Date) Calendar(java.util.Calendar) SimpleDateFormat(java.text.SimpleDateFormat) Date(java.util.Date) KettleFileException(org.pentaho.di.core.exception.KettleFileException) ScriptException(javax.script.ScriptException) IOException(java.io.IOException)

Example 74 with Format

use of java.text.Format in project pentaho-kettle by pentaho.

the class ScriptAddedFunctions method str2date.

public static Object str2date(ScriptEngine actualContext, Bindings actualObject, Object[] ArgList, Object FunctionContext) {
    Object oRC = new Object();
    String sArg1 = "";
    String sArg2 = "";
    String sArg3 = "";
    String sArg4 = "";
    switch(ArgList.length) {
        case 0:
            throw new RuntimeException("Please provide a valid string to the function call str2date.");
        case 1:
            try {
                if (isNull(ArgList[0])) {
                    return null;
                } else if (isUndefined(ArgList[0])) {
                    return undefinedValue;
                }
                sArg1 = (String) ArgList[0];
                Format dfFormatter = new SimpleDateFormat();
                oRC = dfFormatter.parseObject(sArg1);
            // if(Double.isNaN(sArg1)) throw new RuntimeException("The first Argument must be a Number.");
            // DecimalFormat formatter = new DecimalFormat();
            // sRC= formatter.format(sArg1);
            } catch (Exception e) {
                throw new RuntimeException("Could not apply local format for " + sArg1 + " : " + e.getMessage());
            }
            break;
        case 2:
            try {
                if (isNull(ArgList, new int[] { 0, 1 })) {
                    return null;
                } else if (isUndefined(ArgList, new int[] { 0, 1 })) {
                    return undefinedValue;
                }
                sArg1 = (String) ArgList[0];
                sArg2 = (String) ArgList[1];
                Format dfFormatter = new SimpleDateFormat(sArg2);
                oRC = dfFormatter.parseObject(sArg1);
            } catch (Exception e) {
                throw new RuntimeException("Could not apply the given format " + sArg2 + " on the string for " + sArg1 + " : " + e.getMessage());
            }
            break;
        case 3:
            try {
                if (isNull(ArgList, new int[] { 0, 1, 2 })) {
                    return null;
                } else if (isUndefined(ArgList, new int[] { 0, 1, 2 })) {
                    return undefinedValue;
                }
                sArg1 = (String) ArgList[0];
                Format dfFormatter;
                sArg2 = (String) ArgList[1];
                sArg3 = (String) ArgList[2];
                if (sArg3.length() == 2) {
                    Locale dfLocale = EnvUtil.createLocale(sArg3);
                    dfFormatter = new SimpleDateFormat(sArg2, dfLocale);
                    oRC = dfFormatter.parseObject(sArg1);
                } else {
                    throw new RuntimeException("Locale " + sArg3 + " is not 2 characters long.");
                }
            } catch (Exception e) {
                throw new RuntimeException("Could not apply the local format for locale " + sArg3 + " with the given format " + sArg2 + " on the string for " + sArg1 + " : " + e.getMessage());
            }
            break;
        case 4:
            try {
                if (isNull(ArgList, new int[] { 0, 1, 2, 3 })) {
                    return null;
                } else if (isUndefined(ArgList, new int[] { 0, 1, 2, 3 })) {
                    return undefinedValue;
                }
                sArg1 = (String) ArgList[0];
                DateFormat dfFormatter;
                sArg2 = (String) ArgList[1];
                sArg3 = (String) ArgList[2];
                sArg4 = (String) ArgList[3];
                // If the timezone is not recognized, java will automatically
                // take GMT.
                TimeZone tz = TimeZone.getTimeZone(sArg4);
                if (sArg3.length() == 2) {
                    Locale dfLocale = EnvUtil.createLocale(sArg3);
                    dfFormatter = new SimpleDateFormat(sArg2, dfLocale);
                    dfFormatter.setTimeZone(tz);
                    oRC = dfFormatter.parseObject(sArg1);
                } else {
                    throw new RuntimeException("Locale " + sArg3 + " is not 2 characters long.");
                }
            } catch (Exception e) {
                throw new RuntimeException("Could not apply the local format for locale " + sArg3 + " with the given format " + sArg2 + " on the string for " + sArg1 + " : " + e.getMessage());
            }
            break;
        default:
            throw new RuntimeException("The function call str2date requires 1, 2, 3, or 4 arguments.");
    }
    return oRC;
}
Also used : Locale(java.util.Locale) TimeZone(java.util.TimeZone) Format(java.text.Format) DateFormat(java.text.DateFormat) SimpleDateFormat(java.text.SimpleDateFormat) DecimalFormat(java.text.DecimalFormat) DateFormat(java.text.DateFormat) SimpleDateFormat(java.text.SimpleDateFormat) FileObject(org.apache.commons.vfs2.FileObject) SimpleDateFormat(java.text.SimpleDateFormat) KettleFileException(org.pentaho.di.core.exception.KettleFileException) ScriptException(javax.script.ScriptException) IOException(java.io.IOException)

Example 75 with Format

use of java.text.Format in project pentaho-kettle by pentaho.

the class ScriptAddedFunctions method date2str.

public static Object date2str(ScriptEngine actualContext, Bindings actualObject, Object[] ArgList, Object FunctionContext) {
    Object oRC = new Object();
    switch(ArgList.length) {
        case 0:
            throw new RuntimeException("Please provide a valid date to the function call date2str.");
        case 1:
            try {
                if (isNull(ArgList)) {
                    return null;
                } else if (isUndefined(ArgList)) {
                    return undefinedValue;
                }
                java.util.Date dArg1 = (java.util.Date) ArgList[0];
                if (dArg1.equals(null)) {
                    return null;
                }
                Format dfFormatter = new SimpleDateFormat();
                oRC = dfFormatter.format(dArg1);
            } catch (Exception e) {
                throw new RuntimeException("Could not convert to local format.");
            }
            break;
        case 2:
            try {
                if (isNull(ArgList, new int[] { 0, 1 })) {
                    return null;
                } else if (isUndefined(ArgList, new int[] { 0, 1 })) {
                    return undefinedValue;
                }
                java.util.Date dArg1 = (java.util.Date) ArgList[0];
                String sArg2 = (String) ArgList[1];
                Format dfFormatter = new SimpleDateFormat(sArg2);
                oRC = dfFormatter.format(dArg1);
            } catch (Exception e) {
                throw new RuntimeException("Could not convert to the given format.");
            }
            break;
        case 3:
            try {
                if (isNull(ArgList, new int[] { 0, 1, 2 })) {
                    return null;
                } else if (isUndefined(ArgList, new int[] { 0, 1, 2 })) {
                    return undefinedValue;
                }
                java.util.Date dArg1 = (java.util.Date) ArgList[0];
                DateFormat dfFormatter;
                String sArg2 = (String) ArgList[1];
                String sArg3 = (String) ArgList[2];
                if (sArg3.length() == 2) {
                    Locale dfLocale = EnvUtil.createLocale(sArg3.toLowerCase());
                    dfFormatter = new SimpleDateFormat(sArg2, dfLocale);
                    oRC = dfFormatter.format(dArg1);
                } else {
                    throw new RuntimeException("Locale is not 2 characters long.");
                }
            } catch (Exception e) {
                throw new RuntimeException("Could not convert to the given local format.");
            }
            break;
        case 4:
            try {
                if (isNull(ArgList, new int[] { 0, 1, 2, 3 })) {
                    return null;
                } else if (isUndefined(ArgList, new int[] { 0, 1, 2, 3 })) {
                    return undefinedValue;
                }
                java.util.Date dArg1 = (java.util.Date) ArgList[0];
                DateFormat dfFormatter;
                String sArg2 = (String) ArgList[1];
                String sArg3 = (String) ArgList[2];
                String sArg4 = (String) ArgList[3];
                // If the timezone is not recognized, java will automatically
                // take GMT.
                TimeZone tz = TimeZone.getTimeZone(sArg4);
                if (sArg3.length() == 2) {
                    Locale dfLocale = EnvUtil.createLocale(sArg3.toLowerCase());
                    dfFormatter = new SimpleDateFormat(sArg2, dfLocale);
                    dfFormatter.setTimeZone(tz);
                    oRC = dfFormatter.format(dArg1);
                } else {
                    throw new RuntimeException("Locale is not 2 characters long.");
                }
            } catch (Exception e) {
                throw new RuntimeException("Could not convert to the given local format.");
            }
            break;
        default:
            throw new RuntimeException("The function call date2str requires 1, 2, 3, or 4 arguments.");
    }
    return oRC;
}
Also used : Locale(java.util.Locale) Date(java.util.Date) Date(java.util.Date) KettleFileException(org.pentaho.di.core.exception.KettleFileException) ScriptException(javax.script.ScriptException) IOException(java.io.IOException) TimeZone(java.util.TimeZone) Format(java.text.Format) DateFormat(java.text.DateFormat) SimpleDateFormat(java.text.SimpleDateFormat) DecimalFormat(java.text.DecimalFormat) DateFormat(java.text.DateFormat) SimpleDateFormat(java.text.SimpleDateFormat) FileObject(org.apache.commons.vfs2.FileObject) SimpleDateFormat(java.text.SimpleDateFormat)

Aggregations

Format (java.text.Format)160 SimpleDateFormat (java.text.SimpleDateFormat)63 DecimalFormat (java.text.DecimalFormat)39 Test (org.junit.Test)38 DateFormat (java.text.DateFormat)34 Date (java.util.Date)32 NumberFormat (java.text.NumberFormat)29 DateTimeFormatter (java.time.format.DateTimeFormatter)27 ChoiceFormat (java.text.ChoiceFormat)24 ParsePosition (java.text.ParsePosition)22 MessageFormat (java.text.MessageFormat)20 FieldPosition (java.text.FieldPosition)16 Test (org.testng.annotations.Test)16 IOException (java.io.IOException)15 ParseException (java.text.ParseException)11 ArrayList (java.util.ArrayList)11 Locale (java.util.Locale)10 AttributedString (java.text.AttributedString)9 TemporalAccessor (java.time.temporal.TemporalAccessor)7 Calendar (java.util.Calendar)7