字符串转换成整数:
String MyNumber ="1234";
int MyInt = Integer.parseInt(MyNumber);
字符串转换成byte, short, int, float, double, long等数据类型,可
以分别参考Byte, Short, Integer, Float, Double, Long类的parseXXX
方法。
2 数据转换成字符串
整数转换成字符串:
int MyInt = 1234;
String MyString = "" + MyInt;
其它数据类型可以利用同样的方法转换成字符串。
3 十进制到其他进制的转换
十进制整数转换成二进制整数,返回结果是一个字符串:
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(String source, 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);
}
7 个解决方案
#1
ok
#2
go
#3
能否谈谈日期类型的转换?
#4
java中各种数据类型转换已经封装好了,用起来比VC方便
#5
有什么问题?
#6
StringToDate如何做?
#7
import java.util.*;
public class Now {
public static void main(String arg[]) {
/*
** on some JDK, the default TimeZone is wrong
** we must set the TimeZone manually!!!
** Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("EST"));
*/
Calendar cal = Calendar.getInstance(TimeZone.getDefault());
String DATE_FORMAT = "yyyy-MM-dd HH:mm:ss";
java.text.SimpleDateFormat sdf =
new java.text.SimpleDateFormat(DATE_FORMAT);
/*
** on some JDK, the default TimeZone is wrong
** we must set the TimeZone manually!!!
** sdf.setTimeZone(TimeZone.getTimeZone("EST"));
*/
sdf.setTimeZone(TimeZone.getDefault());
System.out.println("Now : " + sdf.format(cal.getTime()));
}
}
Here some formatting possibilities available through the SimpleDateFormat class.
Thanks to T. Guirado for the tip. import java.util.*;
import java.text.*;
public class ShowToday {
public static void main(String args[]) {
ShowToday st = new ShowToday();
st.demo();
}
public void demo() {
System.out.println(easyDateFormat("dd MMMMM yyyy"));
System.out.println(easyDateFormat("yyyyMMdd"));
System.out.println(easyDateFormat("dd.MM.yy"));
System.out.println(easyDateFormat("MM/dd/yy"));
System.out.println(easyDateFormat("yyyy.MM.dd G 'at' hh:mm:ss z"));
System.out.println(easyDateFormat("EEE, MMM d, ''yy"));
System.out.println(easyDateFormat("h:mm a"));
System.out.println(easyDateFormat("H:mm:ss:SSS"));
System.out.println(easyDateFormat("K:mm a,z"));
System.out.println(easyDateFormat("yyyy.MMMMM.dd GGG hh:mm aaa"));
}
public String easyDateFormat (String format) {
Date today = new Date();
SimpleDateFormat formatter = new SimpleDateFormat(format);
String datenewformat = formatter.format(today);
return datenewformat;
}
}
public class Now {
public static void main(String arg[]) {
/*
** on some JDK, the default TimeZone is wrong
** we must set the TimeZone manually!!!
** Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("EST"));
*/
Calendar cal = Calendar.getInstance(TimeZone.getDefault());
String DATE_FORMAT = "yyyy-MM-dd HH:mm:ss";
java.text.SimpleDateFormat sdf =
new java.text.SimpleDateFormat(DATE_FORMAT);
/*
** on some JDK, the default TimeZone is wrong
** we must set the TimeZone manually!!!
** sdf.setTimeZone(TimeZone.getTimeZone("EST"));
*/
sdf.setTimeZone(TimeZone.getDefault());
System.out.println("Now : " + sdf.format(cal.getTime()));
}
}
Here some formatting possibilities available through the SimpleDateFormat class.
Thanks to T. Guirado for the tip. import java.util.*;
import java.text.*;
public class ShowToday {
public static void main(String args[]) {
ShowToday st = new ShowToday();
st.demo();
}
public void demo() {
System.out.println(easyDateFormat("dd MMMMM yyyy"));
System.out.println(easyDateFormat("yyyyMMdd"));
System.out.println(easyDateFormat("dd.MM.yy"));
System.out.println(easyDateFormat("MM/dd/yy"));
System.out.println(easyDateFormat("yyyy.MM.dd G 'at' hh:mm:ss z"));
System.out.println(easyDateFormat("EEE, MMM d, ''yy"));
System.out.println(easyDateFormat("h:mm a"));
System.out.println(easyDateFormat("H:mm:ss:SSS"));
System.out.println(easyDateFormat("K:mm a,z"));
System.out.println(easyDateFormat("yyyy.MMMMM.dd GGG hh:mm aaa"));
}
public String easyDateFormat (String format) {
Date today = new Date();
SimpleDateFormat formatter = new SimpleDateFormat(format);
String datenewformat = formatter.format(today);
return datenewformat;
}
}
#1
ok
#2
go
#3
能否谈谈日期类型的转换?
#4
java中各种数据类型转换已经封装好了,用起来比VC方便
#5
有什么问题?
#6
StringToDate如何做?
#7
import java.util.*;
public class Now {
public static void main(String arg[]) {
/*
** on some JDK, the default TimeZone is wrong
** we must set the TimeZone manually!!!
** Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("EST"));
*/
Calendar cal = Calendar.getInstance(TimeZone.getDefault());
String DATE_FORMAT = "yyyy-MM-dd HH:mm:ss";
java.text.SimpleDateFormat sdf =
new java.text.SimpleDateFormat(DATE_FORMAT);
/*
** on some JDK, the default TimeZone is wrong
** we must set the TimeZone manually!!!
** sdf.setTimeZone(TimeZone.getTimeZone("EST"));
*/
sdf.setTimeZone(TimeZone.getDefault());
System.out.println("Now : " + sdf.format(cal.getTime()));
}
}
Here some formatting possibilities available through the SimpleDateFormat class.
Thanks to T. Guirado for the tip. import java.util.*;
import java.text.*;
public class ShowToday {
public static void main(String args[]) {
ShowToday st = new ShowToday();
st.demo();
}
public void demo() {
System.out.println(easyDateFormat("dd MMMMM yyyy"));
System.out.println(easyDateFormat("yyyyMMdd"));
System.out.println(easyDateFormat("dd.MM.yy"));
System.out.println(easyDateFormat("MM/dd/yy"));
System.out.println(easyDateFormat("yyyy.MM.dd G 'at' hh:mm:ss z"));
System.out.println(easyDateFormat("EEE, MMM d, ''yy"));
System.out.println(easyDateFormat("h:mm a"));
System.out.println(easyDateFormat("H:mm:ss:SSS"));
System.out.println(easyDateFormat("K:mm a,z"));
System.out.println(easyDateFormat("yyyy.MMMMM.dd GGG hh:mm aaa"));
}
public String easyDateFormat (String format) {
Date today = new Date();
SimpleDateFormat formatter = new SimpleDateFormat(format);
String datenewformat = formatter.format(today);
return datenewformat;
}
}
public class Now {
public static void main(String arg[]) {
/*
** on some JDK, the default TimeZone is wrong
** we must set the TimeZone manually!!!
** Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("EST"));
*/
Calendar cal = Calendar.getInstance(TimeZone.getDefault());
String DATE_FORMAT = "yyyy-MM-dd HH:mm:ss";
java.text.SimpleDateFormat sdf =
new java.text.SimpleDateFormat(DATE_FORMAT);
/*
** on some JDK, the default TimeZone is wrong
** we must set the TimeZone manually!!!
** sdf.setTimeZone(TimeZone.getTimeZone("EST"));
*/
sdf.setTimeZone(TimeZone.getDefault());
System.out.println("Now : " + sdf.format(cal.getTime()));
}
}
Here some formatting possibilities available through the SimpleDateFormat class.
Thanks to T. Guirado for the tip. import java.util.*;
import java.text.*;
public class ShowToday {
public static void main(String args[]) {
ShowToday st = new ShowToday();
st.demo();
}
public void demo() {
System.out.println(easyDateFormat("dd MMMMM yyyy"));
System.out.println(easyDateFormat("yyyyMMdd"));
System.out.println(easyDateFormat("dd.MM.yy"));
System.out.println(easyDateFormat("MM/dd/yy"));
System.out.println(easyDateFormat("yyyy.MM.dd G 'at' hh:mm:ss z"));
System.out.println(easyDateFormat("EEE, MMM d, ''yy"));
System.out.println(easyDateFormat("h:mm a"));
System.out.println(easyDateFormat("H:mm:ss:SSS"));
System.out.println(easyDateFormat("K:mm a,z"));
System.out.println(easyDateFormat("yyyy.MMMMM.dd GGG hh:mm aaa"));
}
public String easyDateFormat (String format) {
Date today = new Date();
SimpleDateFormat formatter = new SimpleDateFormat(format);
String datenewformat = formatter.format(today);
return datenewformat;
}
}