一、int 和 String 相互转换
1、int 转 String
//int 转 String(3种)
1) String s = String.valueOf(i); //直接使用String类的静态方法,只产生一个对象
2) String s = Integer.toString(i);
3) String s = "" + i; //会产生两个String对象
2、String 转 int
//String 转 int
1). int i = Integer.parseInt([String]); 或 i = Integer.parseInt([String],[int radix]); //直接使用静态方法,不会产生多余的对象,但会抛出异常
2). int i = Integer.valueOf(my_str).intValue(); //Integer.valueOf(s) 相当于 new Integer(Integer.parseInt(s)),也会抛异常,但会多产生一个对象
二、String 和 Date 相互转换
1、String 和 Date
方法1:
Date date=new Date("2008-04-14");
方法2:
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");//小写的mm表示的是分钟
String dstr="2008-4-24";
java.util.Date date=sdf.parse(dstr);
2、Date 转 String
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
Date date=new Date();
String str=sdf.format(date);
三、时间 和 时间戳 相互转换
1、时间戳 转 时间
public static String stampToDate(String s){
String res;
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
long lt = new Long(s);
Date date = new Date(lt);
res = simpleDateFormat.format(date);
return res;
}
2、时间 转 时间戳
public static String dateToStamp(String s) throws ParseException{
String res;
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date = simpleDateFormat.parse(s);
long ts = date.getTime();
res = String.valueOf(ts);
return res;
}
四、String(金额) 转 带千位分隔符
public String change4Thousand(String num){
num = num.replaceAll(",","");
StringBuffer buffer = new StringBuffer();
for (int i = num.length()-1;i >= 0;i--){
buffer.append(num.charAt(i));
int j =num.length()-i;
if ((num.length()-i)!= 0 && (num.length()-i)%3==0 && i!= 0){
buffer.append(",");
}
}
return buffer.reverse().toString();
}
五、关于进制转换
//16进制 -> byte[]
private byte[] HexStringToByteArray(String s) {
String[] ByteStrings;
int TmpIndex;
ByteStrings = s.split(" ");
byte[] ByteOut;
ByteOut = new byte[ByteStrings.length];
int tem = 0;
try {
for (int i = 0; i <= ByteStrings.length - 1; i++) {
TmpIndex = ByteStrings[i].toLowerCase().indexOf("0x");
if (TmpIndex >= 0) {
tem = Integer.parseInt(ByteStrings[i].toLowerCase().replace("0x", ""), 16);
ByteOut[i] = (byte) tem;
} else {
if (!"".equals(ByteStrings[i])) {
tem = Integer.parseInt(ByteStrings[i], 16);
ByteOut[i] = (byte) tem;
} else {
ByteOut[i] = 0;
}
}
}
} catch (Exception ex) {
ex.printStackTrace();
}
return ByteOut;
}
//整数转16进制
private String IntegerToHexString(int data) {
String s = Integer.toHexString(data);
return s.length() < 2 ? s = "0" + s : s;
}
//string -> 中文格式bytes
public byte[] getGbkStr(String stText) {
byte[] returnText = stText.getBytes("GBK"); // GBK or gb2312
return returnText;
}
六、bitmap和base64转换
/**
* bitmap转为base64
*
* @param bitmap
* @return
*/
public static String bitmapToBase64(Bitmap bitmap) {
String result = null;
ByteArrayOutputStream baos = null;
try {
if (bitmap != null) {
baos = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
baos.flush();
baos.close();
byte[] bitmapBytes = baos.toByteArray();
result = Base64.encodeToString(bitmapBytes, Base64.DEFAULT);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (baos != null) {
baos.flush();
baos.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return result;
}
/**
* base64转为bitmap
* @param base64Data
* @return
*/
public static Bitmap base64ToBitmap(String base64Data) {
byte[] bytes = Base64.decode(base64Data, Base64.DEFAULT);
return BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
}
七、dip -> px -> dp
//将像素转换为px
public static int dip2px(Context context, float dpValue) {
final float scale = context.getResources().getDisplayMetrics().density;
return (int) (dpValue * scale + 0.5f);
}
//将px转换为dp
public static int px2dp(Context context, float pxValue) {
final float scale = context.getResources().getDisplayMetrics().density;
return (int) (pxValue / scale + 0.5f);
}