常见类型和格式转换函数(持续更新)

一、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);
}
 

 

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

KWMax

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值