Java 数据类型转换

本文节选自《Netkiller Java 手札》

作者:netkiller

1.5. 类型

1.5.1. String

1.5.1.1. 随机字符串

	public String randomString(String chars, int length) {
		Random rand = new Random();
		StringBuilder buf = new StringBuilder();
		for (int i = 0; i < length; i++) {
			buf.append(chars.charAt(rand.nextInt(chars.length())));
		}
		return buf.toString();
	}			

1.5.1.2. 字符串替换处理

			public class Test {

	public Test() {
		// TODO Auto-generated constructor stub
	}

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		System.out.println("2010-09-11T20:00:30".replace("T", " "));
	}
}			
{"status":0,"message":"","bankcode":"ABOC;IBC;CCTB;ICBC"}
转换后
{\"status\":0,\"message\":\"\",\"bankcode\":\"ABOC;IBC;CCTB;ICBC\"}			
			package test;

public class str {

	public static void main(String[] args) {
		String jsonString = "{\"status\":0,\"message\":\"\",\"bankcode\":\"ABOC;IBC;CCTB;ICBC\"}";
		System.out.println(jsonString);
		System.out.println(jsonString.replace("\"", "\\\""));
	}

}			

1.5.1.3. substring

			例如:
String str = "helloword!!!";

System.out.println(str.substring(1,4));

System.out.println(str.substring(3,5));

System.out.println(str.substring(0,4));

将得到结果为:

ell

lo 

hell			

1.5.2. 类型转换

1.5.2.1. Long to String

	public class MainClass {

	  public static void main(String[] arg) {
	    long b = 12L;
	    System.out.println(String.valueOf(b));   
	
	  }
	}			

1.5.3. ArrayList

判断元素是否存在

		import java.util.ArrayList;

public class arraylist {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		ArrayList<String> whitelist = new ArrayList<String>();
		whitelist.add("Neo");
		whitelist.add("Jam");
		whitelist.add("Sam");

		if (whitelist.contains("Neo")) {
			System.out.println("Found!");
		}else{
			System.out.println("Not Found!");
		}
	}

}		
		package cn.netkiller.type;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class ArrayListExample {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		List<String> list = new ArrayList<String>();
		list.add("Jack");
		list.add("Jet");
		list.add("Jack");
		list.add("Mike");
		list.add("Kitty");
		list.add("Tom");
		
		//while 循环
		Iterator<String> it = list.iterator();
		while (it.hasNext()) {
			System.out.println(it.next());
		}

		for (Iterator<String> it1 = list.iterator(); it1.hasNext();) {
			System.out.println(it1.next());
		}

		// for 循环
		for (int i = 0; i < list.size(); i++) {
			System.out.println(list.get(i));
		}

		// for 循环加强版
		for (String i : list) {
			System.out.println(i);
		}

	}

}	

1.5.4. Date

1.5.4.1. SimpleDateFormat

public static void main(String[] args) {

    DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
    //get current date time with Date()
    Date date = new Date();
    System.out.println(dateFormat.format(date));

    //get current date time with Calendar()
    Calendar cal = Calendar.getInstance();
    System.out.println(dateFormat.format(cal.getTime()));

}			

1.5.5. String to Array

		package cn.netkiller.java;

public class StringToArray {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		String str="a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z";
		String[] array = null;   
		array = str.split(",");
		for(int i=0; i<array.length; i++){
			System.out.println(array[i]);
		 
		}
	}

}		

1.5.6. Array to String

		package cn.netkiller.java;

import java.util.Arrays;

public class ArrayToString {

	public static void main(String[] args) {
		String[] array = {"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"};
		System.out.println(Arrays.toString(array));
		System.out.println(Arrays.toString(array).replaceAll(", |\\[|\\]", ""));
	}

}		

1.5.7. string to timestamp

Timestamp转化为String:

SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");	//定义格式,不显示毫秒
Timestamp now = new Timestamp(System.currentTimeMillis());			//获取系统当前时间
String str = df.format(now);		

String转化为Timestamp:

SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String time = df.format(new Date());
Timestamp ts = Timestamp.valueOf(time);		

原文发布于微信公众号 - Netkiller(netkiller-ebook)

原文发表时间:2016-09-20

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

发表于

我来说两句

0 条评论
登录 后参与评论

相关文章

来自专栏专知

2018年SCI期刊最新影响因子排行,最高244,人工智能TPAMI9.455

2018年6月26日,最新的SCI影响因子正式发布,涵盖1万2千篇期刊。CA-Cancer J Clin 依然拔得头筹,其影响因子今年再创新高,达244.585...

1472
来自专栏MelonTeam专栏

Bitmap 源码阅读笔记

导语: Android 系统上的图片的处理,跟Bitmap 这个类脱不了关系,我们有必要去深入阅读里面的源码,以便在工作中能更好的处理Bitmap相关的问题...

2608
来自专栏Ryan Miao

ehcache报错

jfinal2.0+tomcat7+ehcache2.6.11+Linux Linux version 2.6.18-164.el5 (mockbuild@x8...

3749
来自专栏生信技能树

基因名变化太快,比如PAM50

当然准备把这些基因跟ensembl数据库的ID对应的时候我发现少了3个,然后我搜索发现它们的symbol其实被修改了,可以说变化比较快啦,才几年时间,3 of ...

1082
来自专栏增长技术

App Guide相关

##TourGuide https://github.com/worker8/TourGuide

812
来自专栏码匠的流水账

聊聊HystrixThreadPool

hystrix-core-1.5.12-sources.jar!/com/netflix/hystrix/HystrixThreadPool.java

921
来自专栏Golang语言社区

Knapsack problem algorithms for my real-life carry-on knapsack

I'm a nomad and live out of one carry-on bag. This means that the total weight o...

1182
来自专栏我和未来有约会

简练的视图模型 ViewModel

patterns & practices Developer Center 发布了 Unity Application Block 1.2 for Silver...

2339
来自专栏码匠的流水账

java9系列(五)Stack-Walking API

java9新增这个类的目的是提供一个标准API用于访问当前线程栈,之前只有Throwable::getStackTrace、Thread::getStackTr...

471
来自专栏跟着阿笨一起玩NET

c# 使用timer定时器操作,上次定时到了以后,下次还未执行完怎么处理

------解决方案-------------------------------------------------------- 开始的时候,禁用定时器,你...

3011

扫码关注云+社区