📄 objecttype.java
字号:
if (obj == null)
return null;
if ("PlainString".equals(type)) {
return obj.toString();
}
if ("Object".equals(type)) {
return obj;
}
String fromType = null;
if (obj instanceof java.lang.String) {
fromType = "String";
String str = (String) obj;
if ("String".equals(type) || "java.lang.String".equals(type)) {
return obj;
}
if (str.length() == 0) {
return null;
}
if ("Boolean".equals(type) || "java.lang.Boolean".equals(type)) {
Boolean value = null;
if (str.equalsIgnoreCase("TRUE"))
value = new Boolean(true);
else
value = new Boolean(false);
return value;
} else if ("Locale".equals(type) || "java.util.Locale".equals(type)) {
Locale loc = UtilMisc.parseLocale(str);
if (loc != null) {
return loc;
} else {
throw new GeneralException("Could not convert " + str + " to " + type + ": ");
}
} else if ("Double".equals(type) || "java.lang.Double".equals(type)) {
try {
NumberFormat nf = null;
if (locale == null)
nf = NumberFormat.getNumberInstance();
else
nf = NumberFormat.getNumberInstance(locale);
Number tempNum = nf.parse(str);
return new Double(tempNum.doubleValue());
} catch (ParseException e) {
throw new GeneralException("Could not convert " + str + " to " + type + ": ", e);
}
} else if ("Float".equals(type) || "java.lang.Float".equals(type)) {
try {
NumberFormat nf = null;
if (locale == null)
nf = NumberFormat.getNumberInstance();
else
nf = NumberFormat.getNumberInstance(locale);
Number tempNum = nf.parse(str);
return new Float(tempNum.floatValue());
} catch (ParseException e) {
throw new GeneralException("Could not convert " + str + " to " + type + ": ", e);
}
} else if ("Long".equals(type) || "java.lang.Long".equals(type)) {
try {
NumberFormat nf = null;
if (locale == null)
nf = NumberFormat.getNumberInstance();
else
nf = NumberFormat.getNumberInstance(locale);
nf.setMaximumFractionDigits(0);
Number tempNum = nf.parse(str);
return new Long(tempNum.longValue());
} catch (ParseException e) {
throw new GeneralException("Could not convert " + str + " to " + type + ": ", e);
}
} else if ("Integer".equals(type) || "java.lang.Integer".equals(type)) {
try {
NumberFormat nf = null;
if (locale == null)
nf = NumberFormat.getNumberInstance();
else
nf = NumberFormat.getNumberInstance(locale);
nf.setMaximumFractionDigits(0);
Number tempNum = nf.parse(str);
return new Integer(tempNum.intValue());
} catch (ParseException e) {
throw new GeneralException("Could not convert " + str + " to " + type + ": ", e);
}
} else if ("Date".equals(type) || "java.sql.Date".equals(type)) {
if (format == null || format.length() == 0) {
try {
return java.sql.Date.valueOf(str);
} catch (Exception e) {
try {
DateFormat df = null;
if (locale != null) {
df = DateFormat.getDateInstance(DateFormat.SHORT, locale);
} else {
df = DateFormat.getDateInstance(DateFormat.SHORT);
}
Date fieldDate = df.parse(str);
return new java.sql.Date(fieldDate.getTime());
} catch (ParseException e1) {
throw new GeneralException("Could not convert " + str + " to " + type + ": ", e);
}
}
} else {
try {
SimpleDateFormat sdf = new SimpleDateFormat(format);
java.util.Date fieldDate = sdf.parse(str);
return new java.sql.Date(fieldDate.getTime());
} catch (ParseException e) {
throw new GeneralException("Could not convert " + str + " to " + type + ": ", e);
}
}
} else if ("Time".equals(type) || "java.sql.Time".equals(type)) {
if (format == null || format.length() == 0) {
try {
return java.sql.Time.valueOf(str);
} catch (Exception e) {
try {
DateFormat df = null;
if (locale != null) {
df = DateFormat.getTimeInstance(DateFormat.SHORT, locale);
} else {
df = DateFormat.getTimeInstance(DateFormat.SHORT);
}
Date fieldDate = df.parse(str);
return new java.sql.Time(fieldDate.getTime());
} catch (ParseException e1) {
throw new GeneralException("Could not convert " + str + " to " + type + ": ", e);
}
}
} else {
try {
SimpleDateFormat sdf = new SimpleDateFormat(format);
java.util.Date fieldDate = sdf.parse(str);
return new java.sql.Time(fieldDate.getTime());
} catch (ParseException e) {
throw new GeneralException("Could not convert " + str + " to " + type + ": ", e);
}
}
} else if ("Timestamp".equals(type) || "java.sql.Timestamp".equals(type)) {
if (format == null || format.length() == 0) {
try {
return java.sql.Timestamp.valueOf(str);
} catch (Exception e) {
try {
DateFormat df = null;
if (locale != null) {
df = DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT, locale);
} else {
df = DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT);
}
Date fieldDate = df.parse(str);
return new java.sql.Timestamp(fieldDate.getTime());
} catch (ParseException e1) {
throw new GeneralException("Could not convert " + str + " to " + type + ": ", e);
}
}
} else {
try {
SimpleDateFormat sdf = new SimpleDateFormat(format);
java.util.Date fieldDate = sdf.parse(str);
return new java.sql.Timestamp(fieldDate.getTime());
} catch (ParseException e) {
throw new GeneralException("Could not convert " + str + " to " + type + ": ", e);
}
}
} else {
throw new GeneralException("Conversion from " + fromType + " to " + type + " not currently supported");
}
} else if (obj instanceof java.lang.Double) {
fromType = "Double";
Double dbl = (Double) obj;
if ("String".equals(type) || "java.lang.String".equals(type)) {
NumberFormat nf = null;
if (locale == null) {
nf = NumberFormat.getNumberInstance();
} else {
nf = NumberFormat.getNumberInstance(locale);
}
return nf.format(dbl.doubleValue());
} else if ("Double".equals(type) || "java.lang.Double".equals(type)) {
return obj;
} else if ("Float".equals(type) || "java.lang.Float".equals(type)) {
return new Float(dbl.floatValue());
} else if ("Long".equals(type) || "java.lang.Long".equals(type)) {
return new Long(Math.round(dbl.doubleValue()));
} else if ("Integer".equals(type) || "java.lang.Integer".equals(type)) {
return new Integer((int) Math.round(dbl.doubleValue()));
} else {
throw new GeneralException("Conversion from " + fromType + " to " + type + " not currently supported");
}
} else if (obj instanceof java.lang.Float) {
fromType = "Float";
Float flt = (Float) obj;
if ("String".equals(type)) {
NumberFormat nf = null;
if (locale == null)
nf = NumberFormat.getNumberInstance();
else
nf = NumberFormat.getNumberInstance(locale);
return nf.format(flt.doubleValue());
} else if ("Double".equals(type)) {
return new Double(flt.doubleValue());
} else if ("Float".equals(type)) {
return obj;
} else if ("Long".equals(type)) {
return new Long(Math.round(flt.doubleValue()));
} else if ("Integer".equals(type)) {
return new Integer((int) Math.round(flt.doubleValue()));
} else {
throw new GeneralException("Conversion from " + fromType + " to " + type + " not currently supported");
}
} else if (obj instanceof java.lang.Long) {
fromType = "Long";
Long lng = (Long) obj;
if ("String".equals(type) || "java.lang.String".equals(type)) {
NumberFormat nf = null;
if (locale == null)
nf = NumberFormat.getNumberInstance();
else
nf = NumberFormat.getNumberInstance(locale);
return nf.format(lng.longValue());
} else if ("Double".equals(type) || "java.lang.Double".equals(type)) {
return new Double(lng.doubleValue());
} else if ("Float".equals(type) || "java.lang.Float".equals(type)) {
return new Float(lng.floatValue());
} else if ("Long".equals(type) || "java.lang.Long".equals(type)) {
return obj;
} else if ("Integer".equals(type) || "java.lang.Integer".equals(type)) {
return new Integer(lng.intValue());
} else {
throw new GeneralException("Conversion from " + fromType + " to " + type + " not currently supported");
}
} else if (obj instanceof java.lang.Integer) {
fromType = "Integer";
Integer intgr = (Integer) obj;
if ("String".equals(type) || "java.lang.String".equals(type)) {
NumberFormat nf = null;
if (locale == null)
nf = NumberFormat.getNumberInstance();
else
nf = NumberFormat.getNumberInstance(locale);
return nf.format(intgr.longValue());
} else if ("Double".equals(type) || "java.lang.Double".equals(type)) {
return new Double(intgr.doubleValue());
} else if ("Float".equals(type) || "java.lang.Float".equals(type)) {
return new Float(intgr.floatValue());
} else if ("Long".equals(type) || "java.lang.Long".equals(type)) {
return new Long(intgr.longValue());
} else if ("Integer".equals(type) || "java.lang.Integer".equals(type)) {
return obj;
} else {
throw new GeneralException("Conversion from " + fromType + " to " + type + " not currently supported");
}
} else if (obj instanceof java.sql.Date) {
fromType = "Date";
java.sql.Date dte = (java.sql.Date) obj;
if ("String".equals(type) || "java.lang.String".equals(type)) {
if (format == null || format.length() == 0) {
return dte.toString();
} else {
SimpleDateFormat sdf = new SimpleDateFormat(format);
return sdf.format(new java.util.Date(dte.getTime()));
}
} else if ("Date".equals(type) || "java.sql.Date".equals(type)) {
return obj;
} else if ("Time".equals(type) || "java.sql.Time".equals(type)) {
throw new GeneralException("Conversion from " + fromType + " to " + type + " not currently supported");
} else if ("Timestamp".equals(type) || "java.sql.Timestamp".equals(type)) {
return new java.sql.Timestamp(dte.getTime());
} else {
throw new GeneralException("Conversion from " + fromType + " to " + type + " not currently supported");
}
} else if (obj instanceof java.sql.Time) {
fromType = "Time";
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -