use of org.pentaho.di.core.exception.KettleValueException in project pentaho-kettle by pentaho.
the class Value method trunc.
// implement the TRUNC function, version without arguments
public Value trunc() throws KettleValueException {
if (isNull()) {
// don't do anything, leave it at NULL!
return this;
}
if (isInteger()) {
// Nothing
return this;
}
if (isBigNumber()) {
getBigNumber().setScale(0, BigDecimal.ROUND_FLOOR);
} else if (isNumber()) {
setValue(Math.floor(getNumber()));
} else if (isDate()) {
Calendar cal = Calendar.getInstance();
cal.setTime(getDate());
cal.set(Calendar.MILLISECOND, 0);
cal.set(Calendar.SECOND, 0);
cal.set(Calendar.MINUTE, 0);
cal.set(Calendar.HOUR_OF_DAY, 0);
setValue(cal.getTime());
} else {
throw new KettleValueException("Function TRUNC only works on numbers and dates");
}
return this;
}
use of org.pentaho.di.core.exception.KettleValueException in project pentaho-kettle by pentaho.
the class Value method last_day.
// implement the LAST_DAY function, arguments in args[]
public Value last_day() throws KettleValueException {
if (getType() == VALUE_TYPE_DATE) {
Calendar cal = Calendar.getInstance();
cal.setTime(getDate());
int last_day = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
cal.set(Calendar.DAY_OF_MONTH, last_day);
setValue(cal.getTime());
} else {
throw new KettleValueException("Function last_day only works on a date");
}
return this;
}
use of org.pentaho.di.core.exception.KettleValueException in project pentaho-kettle by pentaho.
the class Value method first_day.
public Value first_day() throws KettleValueException {
if (getType() == VALUE_TYPE_DATE) {
Calendar cal = Calendar.getInstance();
cal.setTime(getDate());
cal.set(Calendar.DAY_OF_MONTH, 1);
setValue(cal.getTime());
} else {
throw new KettleValueException("Function first_day only works on a date");
}
return this;
}
use of org.pentaho.di.core.exception.KettleValueException in project pentaho-kettle by pentaho.
the class Value method add_months.
// implement the ADD_MONTHS function, one argument
public Value add_months(int months) throws KettleValueException {
if (getType() == VALUE_TYPE_DATE) {
if (!isNull() && getDate() != null) {
Calendar cal = Calendar.getInstance();
cal.setTime(getDate());
int year = cal.get(Calendar.YEAR);
int month = cal.get(Calendar.MONTH);
int day = cal.get(Calendar.DAY_OF_MONTH);
month += months;
int newyear = year + (int) Math.floor(month / 12);
int newmonth = month % 12;
cal.set(newyear, newmonth, 1);
int newday = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
if (newday < day) {
cal.set(Calendar.DAY_OF_MONTH, newday);
} else {
cal.set(Calendar.DAY_OF_MONTH, day);
}
setValue(cal.getTime());
}
} else {
throw new KettleValueException("Function add_months only works on a date!");
}
return this;
}
use of org.pentaho.di.core.exception.KettleValueException in project pentaho-kettle by pentaho.
the class Value method round.
/**
* Rounds the Number value to a certain number decimal places.
*
* @param decimalPlaces
* @return The rounded Number Value
* @throws KettleValueException
* in case it's not a number (or other problem).
*/
public Value round(int decimalPlaces) throws KettleValueException {
if (isNull()) {
return this;
}
if (isNumeric()) {
if (isBigNumber()) {
// Multiply by 10^decimalPlaces
// For example 123.458343938437, Decimalplaces = 2
//
BigDecimal bigDec = getBigNumber();
// System.out.println("ROUND decimalPlaces : "+decimalPlaces+", bigNumber = "+bigDec);
bigDec = bigDec.setScale(decimalPlaces, BigDecimal.ROUND_HALF_EVEN);
// System.out.println("ROUND finished result : "+bigDec);
setValue(bigDec);
} else {
setValue(Const.round(getNumber(), decimalPlaces));
}
} else {
throw new KettleValueException("Function ROUND only works with a number");
}
return this;
}
Aggregations