您的位置:首页 > 编程语言 > Java开发

Java基本类型转换总结

2013-06-11 18:32 309 查看
数值型转换成字符型 // 基本数据类型,
int i_a =7;
String str_a;
str_a = String.valueOf(i_a);
System.out.println(str_a);
str_a = String.format("%06d", i_a);
System.out.println(str_a);
//封装类型
Integer intr = new Integer("123");
str_a=intr.toString();
System.out.println(str_a);
―――――――――――――――――――――
总结
String.valueOf(),Stirng.format(),Object.toStirng();
――――――――――――――――――――――――――――――
字符型转换成数值型
//字符型转换成数值型
String str_b ="111";
int i_b =0;
i_b = Integer.parseInt(str_b);
System.out.println("i_b = " + i_b);
――――――――――――――――――――――――――――――――――――
字符型转换成日期型
Java语言的Calendar(日历),Date(日期),和DateFormat(日期格式)组成了Java标准的一个基本但是非常重要的部分. 日期是商业逻辑计算一个关键的部分. 所有的开发者都应该能够计算未来的日期, 定制日期的显示格式, 并将文本数据解析成日期对象.

1、具体类(和抽象类相对)java.util.Date
2、抽象类java.text.DateFormat 和它的一个具体子类,java.text.SimpleDateFormat
3、抽象类java.util.Calendar 和它的一个具体子类,java.util.GregorianCalendar

Date 类实际上只是一个包裹类, 它包含的是一个长整型数据,表示的是从GMT(格林尼治标准时间)1970年, 1 月 1日00:00:00这一刻之前或者是之后经历的毫秒数.
longnow = System.currentTimeMillis();
String str_c = String.format("%tR", now); // "16:04"
System.out.println("str_c1 = " + str_c);
Date d = new Date(now);
str_c = String.format("%tD", d); // "06/17/08"
System.out.println("str_c2 = " + str_c);
str_c = String.valueOf(d); // Tue Jun 17 16:13:08 CST 2008
System.out.println("str_c3 = " + str_c);
Date date = new Date();
System.out.println(date.getTime());// 1213691167226

用Calendar 类设置和获取日期数据的特定部分呢, 比如说小时, 日, 或者分钟,在日期的这些部分加上或者减去值
――――――――――――――――――――――――――――――――――――
日期型转换成字符型

DateFormatf = new SimpleDateFormat("yyyy-MM-dd");
try {
Date d_a = f.parse("2005-11-07");
System.out.println("d_a = " + d_a);// Mon Aug 07 23:00:00 CST2006
} catch (ParseException e) {
e.printStackTrace();
}
SimpleDateFormat bartDateFormat = new SimpleDateFormat(
"EEEE-MMMM-dd-yyyy");
Date d_2 = new Date();
System.out.println("d_2 = " + bartDateFormat.format(d_2));// 星期二-六月-17-2008
SimpleDateFormat bartDateFormat2 = newSimpleDateFormat("MM-dd-yyyy");
// Create a string containing a text date to be parsed.
String dateStringToParse = "9-29-2001";
try {
// Parse the text version of the date.
// We have to perform the parse method in a
// try-catch construct in case dateStringToParse
// does not contain a date in the format we are expecting.
Date date3 = bartDateFormat2.parse(dateStringToParse);
// Now send the parsed date as a long value
// to the system output.
System.out.println(date3.getTime());
} catch (Exception ex) {
System.out.println(ex.getMessage());
}
System.out.println("--------------------------------------------");
// -----------------------------
DateFormat dateFormat = DateFormat.getDateInstance(DateFormat.FULL);
// Create our Gregorian Calendar.
GregorianCalendar cal = new GregorianCalendar();
// Set the date and time of our calendar
// to the system&s date and time
cal.setTime(new Date());
System.out.println("System Date: " +dateFormat.format(cal.getTime()));
// Set the day of week to FRIDAY
cal.set(GregorianCalendar.DAY_OF_WEEK, GregorianCalendar.FRIDAY);
System.out.println("After Setting Day of Week to Friday: "
+ dateFormat.format(cal.getTime()));
int friday13Counter = 0;
while (friday13Counter <= 10) {
// Go to the next Friday by adding 7 days.
cal.add(GregorianCalendar.DAY_OF_MONTH, 7);
// If the day of month is 13 we have
// another Friday the 13th.
if (cal.get(GregorianCalendar.DAY_OF_MONTH) == 13) {
friday13Counter++;
System.out.println(dateFormat.format(cal.getTime()));
}
}
――――――――――――――――――――――――――――――――――――

本文出自 “achilles” 博客,请务必保留此出处http://orajc.blog.51cto.com/458434/94622

�{>����8~�ystem.getProperty( "file.separator " )

+ gx.netStringToGBK(file.getOriginalFilename());
try {
File file2 = new File(fullPath);
file2.delete();
result = true ;
} catch (Exception e) {
e.printStackTrace();
result = false;
}
}
return result;
}

public boolean deleteFolder(File folder) {
boolean result = false ;
try {
String childs[] = folder.list();
if (childs == null || childs.length <= 0 ) {
if(folder.delete()) {
result = true ;
}
} else {
for ( int i = 0 ; i < childs.length; i ++ ) {
String childName = childs[i];
String childPath =
folder.getPath() + File.separator + childName;
File filePath = new File(childPath);
if (filePath.exists() && filePath.isFile()) {
if(filePath.delete()) {
result = true ;
} else {
result = false ;
break ;
}
}
else if (filePath.exists() && filePath.isDirectory()) {
if(deleteFolder(filePath)) {
result = true ;
} else {
result = false ;
break ;
}
}
}
}

folder.delete();
} catch (Exception e) {
e.printStackTrace();
result = false ;
}
return result;
}
}
150%;�yac���8~�amily:simsun;mso-hansi-font-family:simsun;color:#323E32'>十进制整数转换成二进制整数,返回结果是一个字符串:
Integer.toBinaryString(int i);
Integer和Long提供了toBinaryString,toHexString和toOctalString方法,可以方便的将数据转换成二进制、十六进制和八进制字符串。功能更加强大的是其toString(int/long i, int radix)方法,可以将一个十进制数转换成任意进制的字符串形式。
byte, short, float和double等数据类型,可以利用Integer或者是Long的toBinaryString, toHexString, to OctalString和toString方法转换成其他进制的字符串形式。
4 其它进制到十进制的转换
五进制字符串14414转换成十进制整数,结果是1234:
System.out.println(Integer.valueOf("14414", 5);
Integer和Long提供的valueOf(Stringsource, int radix)方法,可以将任意进制的字符串转换成十进制数据。
5 整数到字节数组的转换
public static byte[] toByteArray(int number)
{
int temp = number;
byte[] b=new byte[4];
for (int i = b.length - 1; i > -1; i--)
{
b[i] = new Integer(temp & 0xff).byteValue();
temp = temp >> 8;
}
return b;
}
6 字节数组到整数的转换
public static int toInteger(byte[] b)
{
int s = 0;
for (int i = 0; i < 3; i++)
{
if (b[i] > 0)
s = s + b[i];
else
s = s + 256 + b[i];
s = s * 256;
}
if (b[3] > 0)
s = s + b[3];
else
s = s + 256 + b[3];
return s;
}
7 短整数与字节数组之间的相互转换
short与int之间的区别在于short是两个字节的,而int是四个字节的。因此,只需要将5 与6 中的范例程序小做改动,即可实现短整数与字节数组之间的相互转换。
8 字节数组转换成双精度浮点数
public double toDouble(byte[] b)
{
long l = 0;
Double D = new Double(0.0);
l = b[0];
l |= ((long)b[1]<<8);
l |= ((long)b[2]<<16);
l |= ((long)b[3]<<24);
l |= ((long)b[4]<<32);
l |= ((long)b[5]<<40);
l |= ((long)b[6]<<48);
l |= ((long)b[7]<<56);
return D.longBitsToDouble(l);
}
9 布尔类型转换成字符串
第一种方法是:
boolean bool = true;
String s = new Boolean(bool).toString();//将bool利用对象封装器转化为对象
s.equals("true");

第二种方法是:
boolean bool = true;
String s = String.valueOf( bool );
首先,从代码长度上讲第二种方法明显要比第一种方法简洁;其次,第一种方法在转化过程中多引入了一个完全没有必要的对象,因此,相对第二种方法来说这就造成了内存空间的浪费,大大减慢了运行速度。所以,推荐使用第二种方法。
10 数字类型与数字类对象之间的转换
byte b = 169;
Byte bo = new Byte( b );
b = bo.byteValue();
short t = 169;
Short to = new Short( t );
t = to.shortValue();
int i = 169;
Integer io = new Integer( i );
i = io.intValue();
long l = 169;
Long lo = new Long( l );
l = lo.longValue();
float f = 169f;
Float fo = new Float( f );
f = fo.floatValue();
double d = 169f;
Double dObj = new Double( d );
d = dObj.doubleValue();
5.0 String 转Integer
Integer in=new Integer(String s);
Integer in=new Integer(Integer.parseInt(String s));

5.1 String 转 int
1). int i = Integer.parseInt([String]); 或
i = Integer.parseInt([String],[int radix]);
2). int i = Integer.valueOf(my_str).intValue();
5.2 如何将整数 int 转换成字串 String ?A. 有叁种方法:
1.) String s = String.valueOf(i);
2.) String s = Integer.toString(i);
3.) String s = "" + i;
注:Double, Float, Long 转成字串的方法大同小异.
5.3 String 转Date
导入 java.util.Date date=null;
date=java.sql.Date.valueOf(String s);
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: