工具类转换类型

InInteger的区别:

1. int为原始类型,integer为引用类型且作为in的包装类,就是把in包装成object对象,他是javaint提供的封装类

2. boolean – Boolean    char ---- Char   byte--- Byte  short---Short   int---integer  

long----Long    double -----Double(除了int类型其他均为首字母大写)

3. 例子:Integer i = new Integer(“1234”) //字符型转int

4. int compareTo to(Object o)与其他类比较,如o类也为int型则执行方法2的操作

5. parseInt(String s)s转为十进制的整数,parseInt(String sint radix)与之前的一个比多了一基数,就是转为几进制的,radix可以为210,16

6. int s =Integer.parseInt("1110",2); s为:14

7. int s =Integer.parseInt("1110",10); s为:1110


//工具类转换类型(年月日,int,Double,BigDecimal,性别)

package tast;

 

import java.math.BigDecimal;

import java.text.ParseException;

import java.text.SimpleDateFormat;

import java.util.Date;

import java.util.regex.Pattern;

 

//工具类转换类型

 

public class TypeTools {

SimpleDateFormat sdf = new SimpleDateFormat("mmHHyyyy");

SimpleDateFormat format = new SimpleDateFormat(

"yyyyMMddHHmmss");

 

//转换BigDecimal金额

public static BigDecimal getBigDecimal(String s) {

if (Pattern.matches("^\\d+\\.??\\d+$", s)) {

return new BigDecimal(s);

}

return null;

}

//转换int型

public static int getInt(String s) {

if (Pattern.matches("^\\d*$", s)) {

return Integer.parseInt(s);

}

return 0;

 

}

//转换double型

public static Double getDouble(String s) {

if (Pattern.matches("^\\d+.??+\\d*$", s)) {

return Double.parseDouble(s);

}

return null;

 

}

//转换标准日期

public static Date getDate(String s) {

SimpleDateFormat sdf = new SimpleDateFormat("mmHHyyyy");

try {

Date date = sdf.parse(s);

return date;

} catch (ParseException e) {

// TODO Auto-generated catch block

e.printStackTrace();

 

}

 

return null;

}

//转换字符串日期

public static String getStringDate(Date d) {

SimpleDateFormat sdf = new SimpleDateFormat("mmHHyyyy");

if (d != null) {

return sdf.format(d);

}

 

return "";

 

}

// parse(String source) 
          从给定字符串的开始解析文本,以生成一个日期。

public static Date parseDate(String time) {

try {

return format.parse(time);

} catch (ParseException e) {

e.printStackTrace();

}

return null;

}

//性别判断

public static String sexParse(String sex) {

return "1".equals(sex) ? "" : "";

}

}


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值