214 lines
5.1 KiB
Plaintext
Executable File
214 lines
5.1 KiB
Plaintext
Executable File
package com.fp.report.util;
|
|
|
|
import java.io.BufferedReader;
|
|
import java.io.IOException;
|
|
import java.math.BigDecimal;
|
|
import java.sql.Clob;
|
|
import java.sql.SQLException;
|
|
import java.text.DecimalFormat;
|
|
import java.text.ParseException;
|
|
import java.text.SimpleDateFormat;
|
|
import java.util.Calendar;
|
|
import java.util.GregorianCalendar;
|
|
|
|
import org.apache.commons.lang.StringUtils;
|
|
|
|
/**
|
|
* Clase que se encarga de ofrecer utilidades para ser utilizadas por iReports
|
|
*
|
|
* @author scastillo
|
|
*/
|
|
public class Utilities {
|
|
|
|
/**
|
|
* Formato ISO
|
|
*/
|
|
private static SimpleDateFormat simpleDateFormat = new SimpleDateFormat(
|
|
"yyyy-MM-dd");
|
|
/**
|
|
*
|
|
*/
|
|
private static GregorianCalendar gregorianCalendar = new GregorianCalendar();
|
|
|
|
/**
|
|
* Constructor por defecto de la clase
|
|
*/
|
|
public Utilities() {
|
|
}
|
|
|
|
/**
|
|
* Metodo que devuelve la fecha de expiracion para manejo de reportes
|
|
*
|
|
* @return java.util.Date
|
|
*/
|
|
public static java.util.Date getDefaultExpiryDate() {
|
|
java.util.Date date = null;
|
|
try {
|
|
date = Utilities.simpleDateFormat.parse("2999-12-31");
|
|
} catch (ParseException ex) {
|
|
return null;
|
|
}
|
|
return date;
|
|
}
|
|
|
|
/**
|
|
* Método que devuelve la fecha de expiracion para manejo de reportes
|
|
*
|
|
* @return java.sql.Timestamp
|
|
*/
|
|
public static java.sql.Timestamp getDefaultExpiryTimestamp() {
|
|
return java.sql.Timestamp.valueOf("2999-12-31 00:00:00.000");
|
|
}
|
|
|
|
/**
|
|
* Metodo que se encarga de devolver
|
|
*
|
|
* @param pDate
|
|
* String que contiene la fecha en formato yyyy-MM-dd
|
|
* @return java.util.Date
|
|
*/
|
|
public static java.util.Date StringToDate(String pDate) {
|
|
java.util.Date date = null;
|
|
try {
|
|
date = Utilities.simpleDateFormat.parse(pDate);
|
|
} catch (ParseException ex) {
|
|
return null;
|
|
}
|
|
return date;
|
|
}
|
|
|
|
/**
|
|
* Metodo que agrega dias en una fecha
|
|
*
|
|
* @param pDate
|
|
* Fecha en formato yyyy-MM-dd
|
|
* @param daysnumber
|
|
* Número de dias a sumar o restar
|
|
* @return
|
|
*/
|
|
public static java.util.Date addDays(String pDate, int daysnumber) {
|
|
try {
|
|
java.util.Date date = Utilities.simpleDateFormat.parse(pDate);
|
|
Utilities.gregorianCalendar.setTime(date);
|
|
Utilities.gregorianCalendar.add(Calendar.DAY_OF_YEAR, daysnumber);
|
|
return new java.util.Date(Utilities.gregorianCalendar.getTime()
|
|
.getTime());
|
|
} catch (ParseException ex) {
|
|
return null;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Metodo que suma/resta meses a una fecha
|
|
*
|
|
* @param pDate
|
|
* Fecha en formato yyyy-MM-dd
|
|
* @param Monthsnumber
|
|
* Número de mese a sumar o restar. Si se resta enviar un número
|
|
* entero negativo
|
|
* @return
|
|
*/
|
|
public static java.util.Date addMonths(String pDate, int Monthsnumber) {
|
|
try {
|
|
java.util.Date date = Utilities.simpleDateFormat.parse(pDate);
|
|
Utilities.gregorianCalendar.setTime(date);
|
|
Utilities.gregorianCalendar.add(Calendar.MONTH, Monthsnumber);
|
|
return new java.util.Date(Utilities.gregorianCalendar.getTime()
|
|
.getTime());
|
|
} catch (ParseException ex) {
|
|
return null;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Metodo que convierte un clob a string
|
|
*
|
|
* @param clb
|
|
* Clob a convertir
|
|
* @return
|
|
*/
|
|
public static String clobToString(Clob clb) {
|
|
if (clb == null) {
|
|
return null;
|
|
}
|
|
StringBuilder str = new StringBuilder();
|
|
String strng;
|
|
BufferedReader bufferRead;
|
|
try {
|
|
bufferRead = new BufferedReader(clb.getCharacterStream());
|
|
} catch (SQLException ex) {
|
|
return null;
|
|
}
|
|
try {
|
|
while ((strng = bufferRead.readLine()) != null) {
|
|
str.append(strng);
|
|
}
|
|
} catch (IOException ex) {
|
|
return null;
|
|
}
|
|
return str.toString();
|
|
}
|
|
|
|
/**
|
|
* Metodo que devuelve la particion
|
|
*
|
|
* @param pDate
|
|
* @return
|
|
*/
|
|
public static String getPartition(String pDate) {
|
|
if ((pDate != null) && (pDate.compareTo("") != 0)) {
|
|
pDate = pDate.replaceAll("\\-", "");
|
|
pDate = pDate.substring(0, 6);
|
|
return pDate;
|
|
} else {
|
|
return null;
|
|
}
|
|
}
|
|
|
|
public static String lpad(String pData, char pFiller, int pLength) {
|
|
return StringUtils.leftPad(pData, pLength, pFiller);
|
|
}
|
|
|
|
public static String rpad(String pData, char pFiller, int pLength) {
|
|
String st = "";
|
|
if (pData.length() > pLength) {
|
|
st = pData.substring(0, pLength);
|
|
} else {
|
|
st = StringUtils.rightPad(pData, pLength, pFiller);
|
|
}
|
|
return st;
|
|
}
|
|
|
|
public static String removeCurrencySeparatorsSymbol(BigDecimal pData,
|
|
int pLengthDecimal) {
|
|
BigDecimal value = pData.setScale(pLengthDecimal,
|
|
BigDecimal.ROUND_HALF_UP);
|
|
|
|
return value.toString().replaceAll("\\.", "").replaceAll("\\,", "");
|
|
}
|
|
|
|
public static String formatCurrency(BigDecimal pData, String format) {
|
|
DecimalFormat frmt = new DecimalFormat(format);
|
|
String formatted = frmt.format(pData.doubleValue());
|
|
|
|
return formatted;
|
|
}
|
|
|
|
public static String getFormatedDate(java.util.Date pDate, String format) {
|
|
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format);
|
|
String date = null;
|
|
date = simpleDateFormat.format(pDate);
|
|
return date;
|
|
|
|
}
|
|
|
|
public static String getFormatedDate(java.sql.Date pDate, String format) {
|
|
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format);
|
|
String date = null;
|
|
date = simpleDateFormat.format(pDate);
|
|
return date;
|
|
|
|
}
|
|
|
|
}
|