java各种数据类型转换

2020-02-23  本文已影响0人  清远_03d9
package cn.com.lwkj.erts.register
import java.sql.Date
public class TypeChange {
public TypeChange() {
}//change the string type to the int typepublic static int stringToInt(String intstr)

//字符串转为Integer
{
 Integer integer;
 integer = Integer.valueOf(intstr);
 return integer.intValue();
}//change int type to the string typepublic static String intToString(int value)

//Integer转为字符串
{
 Integer integer = new Integer(value);
 return integer.toString();
}//change the string type to the float typepublic static float stringToFloat(String floatstr)

{
int i=Integer.parseInt(“123”);
}


//字符串转为float
{
 Float floatee;
 floatee = Float.valueOf(floatstr);
 return floatee.floatValue();
}//change the float type to the string typepublic static String floatToString(float value)

//float转为字符串
{
Float floatee = new Float(value);
return floatee.toString();
}//change the string type to the sqlDate typepublic static java.sql.Date stringToDate(String dateStr)

//字符串转为日期
{
return java.sql.Date.valueOf(dateStr);
}//change the sqlDate type to the string typepublic static String dateToString(java.sql.Date datee)

//日期转为字符串
{
return datee.toString();
}

//int转为double
{
 int i1=200;
double d1=i1;
}

//字符串转为double
{
Double.valueOf(“32.1”).doubleValue();
}

//字符串转为long
{
long l=Long.valueOf(“123”).longValue()
}

//字符串 → BigDecimal
{
String a = "1.2";
BigDecimal a2 = new BigDecimal(a);
}

//Big Decimal → 字符串
{
BigDecimal b = new BigDecimal("1.2");
String b2 = b.toString();
}


// BigDecimal转为double
{
BigDecimal i = new BigDecimal(1.2);//浮点型
i.doubleValue();
}
// BigDecimal转为int
{
BigDecimal i2 = new BigDecimal(1);//整型
i2.intValue();
}

public static void main(String[] args)
{
  java.sql.Date day ;
  day = TypeChange.stringToDate("2003-11-3");
  String strday = TypeChange.dateToString(day);
  System.out.println(strday);
}

} 

上一篇下一篇

猜你喜欢

热点阅读