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;
}
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.");
}
}
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.");
}
}
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;
}
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;
}
Aggregations