Java类型转换工具类

女爷i 2022-03-11 11:16 528阅读 0赞

不多说,直接上代码,亲们可以直接复制使用,里面包括了常用的类型转换,一般而言足够使用了!

public class ObjectParser
{
public static Date toDate(Object date) throws ParseException
{
if (date == null)
{
return null;
}
Date result = null;
String str = date.toString();
String parse = str;
parse = parse.replaceFirst(“^[0-9]{4}([^0-9])”, “yyyy$1”);
parse = parse.replaceFirst(“^[0-9]{2}([^0-9])”, “yy$1”);
parse = parse.replaceFirst(“([^0-9])[0-9]{1,2}([^0-9])”, “$1MM$2”);
parse = parse.replaceFirst(“([^0-9])[0-9]{1,2}( ?)”, “$1dd$2”);
parse = parse.replaceFirst(“( )[0-9]{1,2}([^0-9])”, “$1HH$2”);
parse = parse.replaceFirst(“([^0-9])[0-9]{1,2}([^0-9])”, “$1mm$2”);
parse = parse.replaceFirst(“([^0-9])[0-9]{1,2}([^0-9]?)”, “$1ss$2”);

  1. SimpleDateFormat format = new SimpleDateFormat(parse);
  2. result = format.parse(str);
  3. return result;
  4. \}
  5. public static Integer toInteger(Object data)
  6. \{
  7. if (data == null)
  8. \{
  9. return null;
  10. \}
  11. return Integer.valueOf(data.toString());
  12. \}
  13. public static Integer doubleToInteger(Object data)
  14. \{
  15. if (data == null)
  16. \{
  17. return null;
  18. \}
  19. double d = Double.valueOf(data.toString());
  20. return (int)d;
  21. \}
  22. public static Long toLong(Object data)
  23. \{
  24. if (data == null)
  25. \{
  26. return null;
  27. \}
  28. return Long.valueOf(data.toString());
  29. \}
  30. public static Long doubleToLong(Object data)
  31. \{
  32. if (data == null)
  33. \{
  34. return null;
  35. \}
  36. double d = Double.valueOf(data.toString());
  37. return (long)d;
  38. \}
  39. public static Short toShort(Object data)
  40. \{
  41. if (data == null)
  42. \{
  43. return null;
  44. \}
  45. return Short.valueOf(data.toString());
  46. \}
  47. public static Double toDouble(Object data)
  48. \{
  49. if (data == null)
  50. \{
  51. return null;
  52. \}
  53. return Double.valueOf(data.toString());
  54. \}
  55. public static String toDoubleString(Object data,Integer scale)
  56. \{
  57. if (data == null)
  58. \{
  59. return null;
  60. \}

// DecimalFormat df = new DecimalFormat(“#.00000”);
// return df.format(data);
NumberFormat nf = NumberFormat.getInstance();
// 是否以逗号隔开, 默认true以逗号隔开,如[123,456,789.128]
nf.setGroupingUsed(false);
//小数点后保留几
nf.setMaximumFractionDigits(scale);
return nf.format(data);
}

  1. public static String toString(Object data)
  2. \{
  3. if (data == null)
  4. \{
  5. return null;
  6. \}
  7. return String.valueOf(data.toString());
  8. \}
  9. public static Boolean toBoolean(Object data)
  10. \{
  11. if (data == null)
  12. \{
  13. return null;
  14. \}
  15. return Boolean.valueOf(data.toString());
  16. \}
  17. public static void main(String\[\] args) \{
  18. System.out.println(ObjectParser.toDouble(1234567890123.2232323232));
  19. BigDecimal b1 = new BigDecimal(Double.toString(111111111.22));
  20. System.out.println(b1.add(new BigDecimal(Double.toString(111111111.22))));
  21. System.out.println(ArithUtil.add(111111111.22,111111111.22));
  22. System.out.println(ObjectParser.toDoubleString(123456789012.221245666,6));
  23. \}

}

发表评论

表情:
评论列表 (有 0 条评论,528人围观)

还没有评论,来说两句吧...

相关阅读

    相关 Java类型转换工具

    不多说,直接上代码,亲们可以直接复制使用,里面包括了常用的类型转换,一般而言足够使用了! public class ObjectParser \{     publi