• 数据格式化帮助类


    package hk.buttonwood.ops.common;

    import java.math.BigDecimal;
    import java.text.DecimalFormat;
    import java.text.ParseException;
    import java.text.SimpleDateFormat;
    import java.util.Date;

    import org.apache.commons.lang.StringUtils;

    public class FmtUtils {
    private static final String pattern = "yyyy-MM-dd HH:mm:ss";

    private static final String pattern1 = "yyyyww";

    private static final String pattern2 = "yyyy-MM-dd";

    private static final String pattern3 = "MM/dd";

    private static final String pattern4 = "MM/dd HH:mm";

    private static final String pattern5 = "yyyyMMdd";

    private static final String pattern6 = "HHmmssSS";

    private static final String pattern7 = "HHmm";

    private static final String pattern8 = "yyMMdd";

    static final String M = "M";

    static final String K = "K";

    public static String formatDate(Date d) {
    if (d == null) {
    return "";
    }
    SimpleDateFormat sdf = new SimpleDateFormat(pattern);
    return sdf.format(d);
    }

    public static String formatDateWeekInYear(Date d) {
    if (d == null) {
    return "";
    }
    SimpleDateFormat sdf1 = new SimpleDateFormat(pattern1);
    return sdf1.format(d);
    }

    public static String formatPercent(BigDecimal b) {
    if (b == null) {
    return "";
    } else {
    String pattern = "0.00";
    DecimalFormat df = new DecimalFormat(pattern);
    return df.format(b.doubleValue() * 100) + "%";
    }
    }

    public static String formatDateWithoutTime(Date d) {
    if (d == null) {
    return "";
    }
    SimpleDateFormat sdf2 = new SimpleDateFormat(pattern2);
    return sdf2.format(d);
    }

    public static String formatDateShort(Date d) {
    if (d == null) {
    return "";
    }
    SimpleDateFormat sdf3 = new SimpleDateFormat(pattern3);
    return sdf3.format(d);
    }

    public static String formatDateMiddle(Date d) {
    if (d == null) {
    return "";
    }
    SimpleDateFormat sdf4 = new SimpleDateFormat(pattern4);
    return sdf4.format(d);
    }

    public static String formatDateyyyyMMdd(Date d) {
    if (d == null) {
    return "";
    }
    SimpleDateFormat sdf5 = new SimpleDateFormat(pattern5);
    return sdf5.format(d);
    }

    public static String formatDateyyMMdd(Date d) {
    if (d == null) {
    return "";
    }
    SimpleDateFormat sdf8 = new SimpleDateFormat(pattern8);
    return sdf8.format(d);
    }

    public static Date int2Date(Integer i) {
    SimpleDateFormat sdf5 = new SimpleDateFormat(pattern5);
    try {
    return sdf5.parse(String.valueOf(i));
    } catch (ParseException e) {
    return null;
    }
    }

    public static Integer date2int(Date d) {
    if (d == null) {
    return null;
    }
    SimpleDateFormat sdf5 = new SimpleDateFormat(pattern5);
    String s = sdf5.format(d);
    return Integer.valueOf(s);
    }

    public static String formatDateHHmmssSS(Date d) {
    if (d == null) {
    return "";
    }
    SimpleDateFormat sdf6 = new SimpleDateFormat(pattern6);
    String s = sdf6.format(d);
    return StringUtils.left(s, 8);
    }

    public static String formatDateHHmm(Date d) {
    if (d == null) {
    return "";
    }
    SimpleDateFormat sdf7 = new SimpleDateFormat(pattern7);
    return sdf7.format(d);
    }

    public static String blank(Object o) {
    if (o == null) {
    return "*空白*";
    }
    if (StringUtils.isEmpty(String.valueOf(o))) {
    return "*空白*";
    }
    return StringUtils.isEmpty(o.toString()) ? "*空白*" : o.toString();
    }
    /*
    * 指定精度格式化BigDemcial
    * @param:对应值
    * @param:精度,#.00小数点后两位
    * @return:返回格式化后的数据
    */
    public static String formatDecimalWithScale(String value,String scale){
    String v=null;
    try{
    BigDecimal bigDecimal=new BigDecimal(value);
    v=new java.text.DecimalFormat(scale).format(bigDecimal);
    }catch(Exception e){
    v="0.0000000000";
    e.printStackTrace();
    }
    return v;
    }
    public static String empty(Object o) {
    if (o == null) {
    return "";
    }
    if (StringUtils.isEmpty(String.valueOf(o))) {
    return "";
    }
    return StringUtils.isEmpty(o.toString()) ? "" : o.toString();
    }

    public static String hold3Decimal(BigDecimal d) { // 保留3位小数位,按四舍五入
    try {
    String pattern = "0.000";
    DecimalFormat df = new DecimalFormat(pattern);
    String s = df.format(d);
    if (s.equals("-0.000")) {
    s = "0.000";
    }
    return s;
    } catch (Exception ex) {
    return "";
    }

    }

    public static String hold2Decimal(BigDecimal d) { // 保留2位小数位,按四舍五入
    try {
    String pattern = "0.00";
    DecimalFormat df = new DecimalFormat(pattern);
    String s = df.format(d);
    if (s.equals("-0.00")) {
    s = "0.00";
    }
    return s;
    } catch (Exception ex) {
    return "";
    }
    }

    public static String hold1Decimal(BigDecimal d) { // 保留1位小数位,按四舍五入
    try {
    String pattern = "0.0";
    DecimalFormat df = new DecimalFormat(pattern);
    String s = df.format(d);
    if (s.equals("-0.00")) {
    s = "0.00";
    }
    return s;
    } catch (Exception ex) {
    return "";
    }
    }

    public static String formatDecimal(BigDecimal d) { // 格式化数字显示
    // 2342565.555->2,342,565.56
    try {
    String pattern = "##,##0.00";
    DecimalFormat declimalFormat = new DecimalFormat(pattern);
    String s = declimalFormat.format(d.doubleValue());
    if (s.equals("-0.00")) {
    s = "0.00";
    }
    return s;
    } catch (Exception ex) {
    return "";
    }

    }

    public static String hold0Decimal(BigDecimal d) { // 格式化数字显示
    try {
    String pattern = "####0.##";
    DecimalFormat declimalFormat = new DecimalFormat(pattern);
    String s = declimalFormat.format(d.doubleValue());
    if (s.equals("-0.00")) {
    s = "0.00";
    }
    return s;
    } catch (Exception ex) {
    return "";
    }
    }

    public static String noDecimal(BigDecimal d) { // 只保留整数
    try {
    String pattern = "####0";
    DecimalFormat declimalFormat = new DecimalFormat(pattern);
    String s = declimalFormat.format(d.doubleValue());
    if (s.equals("-0")) {
    s = "0";
    }
    return s;
    } catch (Exception ex) {
    return "";
    }
    }

    public static String formatInt(BigDecimal d) { // 格式化数字显示
    // 2342565.555->2,342,565.56
    try {
    String pattern = "##,##0";
    DecimalFormat declimalFormat = new DecimalFormat(pattern);
    String s = declimalFormat.format(d.doubleValue());
    if (s.equals("-0")) {
    s = "0";
    }
    return s;
    } catch (Exception ex) {
    return "";
    }
    }

    public static String formatInt(Integer d) { // 格式化数字显示
    // 2342565.555->2,342,565.56
    try {
    String pattern = "##,##0";
    DecimalFormat declimalFormat = new DecimalFormat(pattern);
    String s = declimalFormat.format(d);
    if (s.equals("-0")) {
    s = "0";
    }
    return s;
    } catch (Exception ex) {
    return "";
    }
    }

    public static String formatLong(Long d) { // 格式化数字显示
    // 2342565.555->2,342,565.56
    try {
    String pattern = "##,##0";
    DecimalFormat declimalFormat = new DecimalFormat(pattern);
    String s = declimalFormat.format(d);
    if (s.equals("-0")) {
    s = "0";
    }
    return s;
    } catch (Exception ex) {
    return "";
    }
    }

    public static String shortQty(Integer qty) { // 将数量转换格式,1000->1K,1000000->1M
    try {
    String retVal = "";
    double d = 0.0;
    if (qty.doubleValue() >= 1000000) { // 大于1000000
    d = qty.doubleValue() / 1000000;
    if ((d * 10) / ((int) d * 10) != 1)
    retVal = (new Double(d).toString() + M);
    // retVal=formatDecimal(new BigDecimal(d))+M;
    else
    retVal = new Integer((int) d).toString() + M;
    return retVal;
    }
    if (qty.doubleValue() >= 1000) { // 大于1000
    d = qty.doubleValue() / 1000;
    if ((d * 10) / ((int) d * 10) != 1)
    retVal = (new Double(d).toString() + K);
    else
    retVal = new Integer((int) d).toString() + K;
    return retVal;
    }
    if (qty.doubleValue() <= -1000000) { // 小于-1000000
    d = qty.doubleValue() / 1000000;
    if ((d * 10) / ((int) d * 10) != 1)
    retVal = (new Double(d).toString() + M);
    else
    retVal = new Integer((int) d).toString() + M;
    return retVal;
    }
    if (qty.doubleValue() <= -1000) { // 小于1000
    d = qty.doubleValue() / 1000;
    if ((d * 10) / ((int) d * 10) != 1)
    retVal = (new Double(d).toString() + K);
    else
    retVal = new Integer((int) d).toString() + K;
    return retVal;
    }
    if (qty.doubleValue() - Math.floor(qty.doubleValue()) == 0) // 整数返回整数,有小数返回小数
    retVal = (new Long(qty.longValue()).toString());
    else
    retVal = (new Double(qty.doubleValue()).toString());
    return retVal;
    } catch (Exception ex) {
    return "";
    }
    }

    public static void main(String[] args) {
    Date d = new Date();
    System.out.println(d);
    System.out.println(formatDateHHmmssSS(d));
    }
    }

  • 相关阅读:
    作业17
    模块
    Find the Lost Sock (异或算法)
    CD(二分)
    数字流输入
    最大连续子序列(dp)
    STL学习----lower_bound和upper_bound算法
    输入挂(减少时间)
    暴力之全排列
    【C++】判断元素是否在vector中,对vector去重,两个vector求交集、并集
  • 原文地址:https://www.cnblogs.com/maybo/p/5182535.html
Copyright © 2020-2023  润新知