首页

关于jasperreports的FormatUtils格式化工具类对常见基本数据类型、日期及BigDecimal、BigInteger等格式化处理

标签:jasperReports,FormatUtils,格式化工具类     发布时间:2018-04-12   

一、前言

关于JasperReports报表jasperreports-6.1.1源码包net.sf.jasperreports.engine.util.FormatUtils格式化工具类,对基本数字数据Number类型格式化处理getFormattedNumber、时间日期格式化处理getFormattedDate等。

二、源码说明

package net.sf.jasperreports.engine.util;@b@@b@import java.math.BigDecimal;@b@import java.math.BigInteger;@b@import java.sql.Time;@b@import java.sql.Timestamp;@b@import java.text.DateFormat;@b@import java.text.NumberFormat;@b@import java.text.ParseException;@b@@b@public class FormatUtils@b@{@b@  public static Number getFormattedNumber(NumberFormat numberFormat, String fieldValue, Class<?> valueClass)@b@    throws ParseException@b@  {@b@    if (valueClass.equals(Byte.class))@b@    {@b@      return new Byte(numberFormat.parse(fieldValue).byteValue());@b@    }@b@    if (valueClass.equals(Integer.class))@b@    {@b@      return Integer.valueOf(numberFormat.parse(fieldValue).intValue());@b@    }@b@    if (valueClass.equals(Long.class))@b@    {@b@      return new Long(numberFormat.parse(fieldValue).longValue());@b@    }@b@    if (valueClass.equals(Short.class))@b@    {@b@      return new Short(numberFormat.parse(fieldValue).shortValue());@b@    }@b@    if (valueClass.equals(Double.class))@b@    {@b@      return new Double(numberFormat.parse(fieldValue).doubleValue());@b@    }@b@    if (valueClass.equals(Float.class))@b@    {@b@      return new Float(numberFormat.parse(fieldValue).floatValue());@b@    }@b@    if (valueClass.equals(BigDecimal.class))@b@    {@b@      return new BigDecimal(numberFormat.parse(fieldValue).toString());@b@    }@b@    if (valueClass.equals(BigInteger.class))@b@    {@b@      return new BigInteger(String.valueOf(numberFormat.parse(fieldValue).longValue()));@b@    }@b@    if (valueClass.equals(Number.class))@b@    {@b@      return numberFormat.parse(fieldValue);@b@    }@b@    return null;@b@  }@b@@b@  public static java.util.Date getFormattedDate(DateFormat dateFormat, String fieldValue, Class<?> valueClass)@b@    throws ParseException@b@  {@b@    if (valueClass.equals(java.util.Date.class))@b@    {@b@      return dateFormat.parse(fieldValue);@b@    }@b@    if (valueClass.equals(java.sql.Date.class))@b@    {@b@      return new java.sql.Date(dateFormat.parse(fieldValue).getTime());@b@    }@b@    if (valueClass.equals(Timestamp.class))@b@    {@b@      return new Timestamp(dateFormat.parse(fieldValue).getTime());@b@    }@b@    if (valueClass.equals(Time.class))@b@    {@b@      return new Time(dateFormat.parse(fieldValue).getTime());@b@    }@b@    return null;@b@  }@b@}