打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
类型转换类,将字符串转换成其他基本类型
/***************************************
 *                                     *
 *  MCC: 客服系统                        *
 *                                     *
 *  所有版权(c) 上海若雅信息系统有限公司     *
 *                                     *
 ***************************************/
package com.roya.mcc.util;
import java.util.*;
import com.roya.mcc.core.exception.TypeConvertException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
/**
 * 类型转换类,将字符串转换成其他基本类型
 *
 * @version <tt>Revision: 1.0</tt>
 * @author  <a href="mailto:hulk@royasoft.com.cn">hongking</a>
 */
public class TypeConvertor {
 public static Integer string2Integer(String str) throws TypeConvertException{
  int value = -1;
  try{
   value = Integer.parseInt(str);
  }
  catch(Exception e){
   throw new TypeConvertException(e);
  }
  return new Integer(value);
 }
 
 public static Long string2Long(String str) throws TypeConvertException{
  long value = -1;
  try{
   value = Long.parseLong(str);
  }
  catch(Exception e){
   throw new TypeConvertException(e);
  }
  return new Long(value);
 }
 
 public static Float string2Float(String str) throws TypeConvertException{
  float value = 0;
  try{
   value = Float.parseFloat(str);
  }
  catch(Exception e){
   throw new TypeConvertException(e);
  }
  return new Float(value);
 }
 
 public static Date string2Date(String str,String format)  throws TypeConvertException{
  Date dt = null;
  try{
   if(format == null || "".equals(format)){
    DateFormat Format = DateFormat.getDateInstance();
    dt = Format.parse(str);
   }
   else{
    SimpleDateFormat sdf = new SimpleDateFormat(format);
    dt = sdf.parse(str);
   }
  }
  catch(Exception e){
   throw new TypeConvertException(e);
  }
  return dt;
 }
 
 public static Date string2Time(String str,String format)  throws TypeConvertException{
  Date dt = null;
  try{
   if(format == null || "".equals(format)){
    DateFormat Format = DateFormat.getTimeInstance();
    dt = Format.parse(str);
   }
   else{
    SimpleDateFormat sdf = new SimpleDateFormat(format);
    dt = sdf.parse(str);
   }
  }
  catch(Exception e){
   throw new TypeConvertException(e);
  }
  return dt;
 }
 public static Date string2DateTime(String str,String format)  throws TypeConvertException{
  Date dt = null;
  try{
   if(format == null || "".equals(format)){
    DateFormat Format = DateFormat.getDateTimeInstance();
    dt = Format.parse(str);
   }
   else{
    SimpleDateFormat sdf = new SimpleDateFormat(format);
    dt = sdf.parse(str);
   }
  }
  catch(Exception e){
   throw new TypeConvertException(e);
  }
  return dt;
 }
 
}
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
String 转换 java.sql.Date 和java.sql.Time 格式
java.text.SimpleDateFormat.parse() 是 no thread safe
深入理解Java:SimpleDateFormat安全的时间格式化
使用SimpleDateFormat顺利解析日期
java String和Date转换
一个Java日期格式验证的历程
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服