字符串之间的各种转换

、.获取系统的时间

1.)
SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMddHHmmssSSS");
    String time = formatter.format(new Date());//获取到当前的系统时间精确到年月日精确到毫秒
结果:20170425112526513

2.)
时间戳的默认单位是毫秒,System.currentTimeMillis(); 返回的就是以毫秒为单位的系统时间戳
Long time= System.currentTimeMillis();//获取当前的系统时间精确到毫秒 Long型(用于时间戳)
结果:1493089570240

3.)

long currentTime = System.currentTimeMillis();

SimpleDateFormat formatter = new SimpleDateFormat("yyyy年-MM月dd日-HH时mm分ss秒");

Date date = new Date(currentTime);

System.out.println(formatter.format(date));

运行结果如下:

当前时间:2011年-08月10日-14时11分46秒

时间转换:

SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm");
 holder.order_time.setText(format.format(mList.get(position).getCreateTime().toString()));
运行结果如下:2016-11-28 19:20

二、字符串的转换
3.)Long型转成string

String time=Long.toString(time);


4.)把java对象转成json

Been been = new Been();
been.setTradingId(mTradingId);
String str = new Gson().toJson(been);
 
5.)string转byte[]
String str="aaaaaaaaaa";
byte[] srtbyte =str.getBytes();
byte[] srtbyte = str.getBytes("UTF-8");
6.)byte[]转string
byte[] encodedData = RSACoder.encryptByPublicKey(data, publicKey);
String param = new String(encodedData);//不带编码格式的
String param = new String(encodedData,"UTF-8");//带编码格式的

三、)字符串的处理
字符串截取
1.)
String url=""https://image.baidu.com/"";
url = baseUrl.substring(0, baseUrl.lastIndexOf("/")) + ":newNetLottery_web3g/index.html";
结果:https://image.baidu.com:newNetLottery_web3g/index.html
2.)
String imageUrl="https://www.baidu.com/img1.jpg,https://image.baidu.com/img2jpg,https://image.baidu.com/img3jpg";
String[] imgs = imageUrl.split(",");
for (int i = 0; i < imgs.length; i++) {
    paths.add(imgs[i]);
}
结果:
https://www.baidu.com/img1.jpg  
https://image.baidu.com/img2jpg 
https://image.baidu.com/img3jpg


四、)加密,解密
        String str = new Gson().toJson(been);
        byte[] data = str.getBytes();
        //加密
         String publicKey = DataCenter.getCommonData().PUBLIC_KEY;
        byte[] encodedData = RSACoder.encryptByPublicKey(data, publicKey);
        String param = byteArr2HexStr(encodedData);

       //解密
       byte[] ss=hexStr2ByteArr(param);
       String private_key = DataCenter.getCommonData().PRIVATE_KEY;
       byte[] privateDate=decryptByPrivateKey(ss, private_key);
       String aa = new String(privateDate);
       Log.e("TAG", "解密param++++++++++++" + aa);

1、ArrarList 转换为 string[] :

  ArrayList list = new ArrayList();

  list.Add("aaa");

  list.Add("bbb");

  string[] arrString = (string[])list.ToArray(typeof( string)) ;

2、string[] 转换为 ArrarList :

  ArrayList list = new ArrayList(new string[] { "aaa", "bbb" });

3、ArrayList 转换为 string :

  ArrayList list = new ArrayList();

  list.Add("aaa");

  list.Add("bbb");

  string str= string.Join(",", (string[])list.ToArray(typeof( string)));

4、string 转换为 ArrayList :

  string str="1,2,3,4,5";

  ArrayList b = new ArrayList( str.Split(',') ) ;



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值