Java中的基本数据类型转换
Posted 荣-
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Java中的基本数据类型转换相关的知识,希望对你有一定的参考价值。
如果想要做ORM,将数据映射为对象,则基本的数据类型转换是必要的功能,而Java似乎未提供该功能,只有自己下手来完成该功能.
完成该功能的目标如下:
1: 提供基本的数据转换
2: 考虑转换的性能.
下面是我的具体实现:
1: 接口:
public static <T> T Cast(Class<T> t, Object value)
2: 依赖:需要依赖与获得数据默认值,因为在很多情况下,如果要转换的数据为null,调用者期望的是返回默认值.
系统自动提供的默认值不能满足我们的需求,例如int的默认值为0,但是在sql查询中,如果查询失败,我们期望的是小于0的值,例如Select Count(1) from table1这样的操作.
3: 实现:
/** * 类型转换 * @param t * @param value * @return */ @SuppressWarnings("unchecked") public static <T> T Cast(Class<T> t, Object value) { if (value == null) { return GetDefault(t); } if (t == Object.class) { return (T)value; } try { return Cast_i(t, value); } catch(Exception ex) { ex.printStackTrace(); return GetDefault(t); } }
private static String GetStr(Class<?> t, Object value) { if (value == null) { return ""; } if (t == String.class){ return (String)value; }else { return value.toString(); } }
@SuppressWarnings("unchecked") private static <T> T Cast_i(Class<T> t, Object value) { Class<?> objClass = value.getClass(); if (t == String.class) { return (T)GetStr(objClass, value); } if (t == int.class) { if (objClass == int.class) { return (T)value; } String text = GetStr(objClass, value); Object ret = Integer.parseInt(text); return (T)ret; } if (t == Integer.class) { if (objClass == Integer.class) { return (T)value; } if (objClass == int.class) { return (T)value; } String text = GetStr(objClass, value); Object ret = Integer.parseInt(text); return (T)ret; } if (t == short.class) { if (objClass == int.class) { return (T)value; } String text = GetStr(objClass, value); Object ret = Short.parseShort(text); return (T)ret; } if (t == boolean.class || t == Boolean.class) { if (objClass == int.class) { Object ret = (int)value >= 1? true:false; return (T)ret; } if (objClass == Integer.class) { Object ret = (Integer)value >= 1? true:false; return (T)ret; } String text = GetStr(objClass, value); Object ret = Integer.parseInt(text); ret = (int)ret >= 1? true:false; return (T)ret; } if (t == long.class) { if (objClass == long.class) { return (T)value; } String text = GetStr(objClass, value); Object ret = Long.parseLong(text); return (T)ret; } if (t == byte.class) { if (objClass == byte.class) { return (T)value; } String text = GetStr(objClass, value); Object ret = Byte.parseByte(text); return (T)ret; } if (t == double.class) { if (objClass == double.class) { return (T)value; } String text = GetStr(objClass, value); Object ret = Double.parseDouble(text); return (T)ret; } if (t == float.class) { if (objClass == float.class) { return (T)value; } String text = GetStr(objClass, value); Object ret = Float.parseFloat(text); return (T)ret; } if (t == BigDecimal.class) { if (objClass == BigDecimal.class) { return (T)value; } String text = GetStr(objClass, value); return (T)new BigDecimal(text); } if (t == java.util.Date.class){ if (objClass == java.util.Date.class) { return (T)value; } String text = GetStr(objClass, value); return (T)HiTypeHelper.Convert2Date(text); } if (t == java.sql.Date.class){ if (objClass == java.sql.Date.class) { return (T)value; } String text = GetStr(objClass, value); return (T)HiTypeHelper.Convert2SqlDate(text); } if (t == Time.class) { if (objClass == Time.class) { return (T)value; } String text = GetStr(objClass, value); Object ret = HiTypeHelper.Convert2Date(text).getTime(); return (T)ret; } if (t == Timestamp.class) { if (objClass == Timestamp.class) { return (T)value; } String text = GetStr(objClass, value); return (T)Timestamp.valueOf(text); } return (T)value; }
3: 测试代码:
@Test public void Test_HiTypeHelper_Cast() { Integer val = 1; int ret = HiTypeHelper.Cast(int.class, val); Assert.assertEquals(ret, 1); String text = "1"; ret = HiTypeHelper.Cast(int.class, text); Assert.assertEquals(ret, 1); text = "a"; ret = HiTypeHelper.Cast(int.class, text); Assert.assertEquals(ret, -1); int val2 = 1; Integer ret2 = HiTypeHelper.Cast(Integer.class, val2); Assert.assertEquals(ret2.intValue(), 1); text = "1"; ret2 = HiTypeHelper.Cast(Integer.class, text); Assert.assertEquals(ret2.intValue(), 1); boolean bl = HiTypeHelper.Cast(boolean.class, 1); Assert.assertTrue(bl); bl = HiTypeHelper.Cast(boolean.class, 3); Assert.assertTrue(bl); bl = HiTypeHelper.Cast(boolean.class, 0); Assert.assertFalse(bl); bl = HiTypeHelper.Cast(boolean.class, -1); Assert.assertFalse(bl); bl = HiTypeHelper.Cast(boolean.class, "1"); Assert.assertTrue(bl); bl = HiTypeHelper.Cast(boolean.class, "3"); Assert.assertTrue(bl); bl = HiTypeHelper.Cast(boolean.class, "0"); Assert.assertFalse(bl); bl = HiTypeHelper.Cast(boolean.class, "-1"); Assert.assertFalse(bl); }
很多时候,数据转换在与数据库交互时使用较多,所以需要提供一些数据库类型的转换,主要是指时间或日期的转换.如:java.util.Date,java.sql.date与String和long之间的转换,下面是我的实现:
/** * 字符串转换成日期 * @param text * @return */ public static java.util.Date Convert2Date(String text) { if (text == null || text.trim() == "") { return null; } String format = "yyyy-MM-dd"; String str = text.trim(); if (str.contains(":")) { format = "yyyy-MM-dd HH:mm:ss"; } SimpleDateFormat sdf = new SimpleDateFormat(format); try { return sdf.parse(str); } catch (ParseException e) { // TODO Auto-generated catch block e.printStackTrace(); return null; } } /** * 字符串转换为日期 * @param text * @return */ public static java.sql.Date Convert2SqlDate(String text) { java.util.Date dt = Convert2Date(text); return new java.sql.Date(dt.getTime()); } /** * 日期转换为字符串 * @param date * @return */ public static String ToShortString(java.util.Date date) { if (date == null) { return null; } SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); return sdf.format(date); } /** * 日期转换为字符串 * @param date * @return */ public static String ToLongString(java.util.Date date) { if (date == null) { return null; } SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); return sdf.format(date); } /** * 日期转换为字符串 * @param date * @return */ public static String ToShortString(java.sql.Date date) { if (date == null) { return null; } SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); return sdf.format(date); } /** * 日期转换为字符串 * @param date * @return */ public static String ToLongString(java.sql.Date date) { if (date == null) { return null; } SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); return sdf.format(date); }
以上是关于Java中的基本数据类型转换的主要内容,如果未能解决你的问题,请参考以下文章