Java编程 经验技巧汇总

1.JSONArray数组如何循环遍历

1package xxx; 2import net.sf.json.JSONArray; 3import net.sf.json.JSONObject; 4public class Test { 5public static void main(String[] args) { 6 /*author:命运的信徒 7 * date:2019/5/18 8 */ 9 String str ="[{'otitle':'会','source':'7'},{'otitle':'不会','source':'3'}]"; 10 //1.把字符串类型的json数组对象转化JSONArray 11 JSONArray json=JSONArray.fromObject(str); 12 //2、循环遍历这个数组 13 for(int i=0;i<json.size();i++){ 14 //3、把里面的对象转化为JSONObject 15 JSONObject job = json.getJSONObject(i); 16 // 4、把里面想要的参数一个个用.属性名的方式获取到 17 System.out.println(job.get("otitle")+":"+job.get("source")) ; 18 } 19 } 20}

可参考https://blog.csdn.net/qq_37591637/article/details/90319229

2.生成UNIX时间戳(精度:秒)

1public class Test { 2 public static void main(String[] args) { 3 //生成随机时间 4 long offset = Timestamp.valueOf("2012-01-01 00:00:00").getTime(); 5 long end = Timestamp.valueOf("2013-01-01 00:00:00").getTime(); 6 long diff = end - offset + 1; 7 Timestamp rand = new Timestamp(offset + (long)(Math.random() * diff)); 8 System.out.println(rand); 9 //下面两个是一样的结果,都是当前时间(UNIX时间戳) 10 System.out.println(System.currentTimeMillis() / 1000); 11 System.out.println(Calendar.getInstance().getTimeInMillis() / 1000); 12 13 } 14}

3.随机生成时间

1Random rand = new Random(); 2SimpleDateFormat format = new SimpleDateFormat( "yyyy-MM-dd "); 3Calendar cal = Calendar.getInstance(); 4cal.set(1900, 0, 1); 5long start = cal.getTimeInMillis(); 6cal.set(2008, 0, 1); 7long end = cal.getTimeInMillis(); 8for(int i = 0; i < 10; i++) { 9Date d = new Date(start + (long)(rand.nextDouble() * (end - start))); 10System.out.println(format.format(d)); 11}

4.随机生成颜色

方式一: 给定范围获得随机颜色

1private Color getRandColor(int fc, int bc) { 2 Random random = new Random(); 3 if (fc > 255) 4 fc = 255; 5 if (bc > 255) 6 bc = 255; 7 int r = fc + random.nextInt(bc - fc); 8 int g = fc + random.nextInt(bc - fc); 9 int b = fc + random.nextInt(bc - fc); 10 return new Color(r, g, b); 11 } 12 13getRandColor(200, 250)

方式二:生成随机十六进制颜色代码

1 //随机生成颜色代码 2 public String getColor(){ 3 //红色 4 String red; 5 //绿色 6 String green; 7 //蓝色 8 String blue; 9 //生成随机对象 10 Random random = new Random(); 11 //生成红色颜色代码 12 red = Integer.toHexString(random.nextInt(256)).toUpperCase(); 13 //生成绿色颜色代码 14 green = Integer.toHexString(random.nextInt(256)).toUpperCase(); 15 //生成蓝色颜色代码 16 blue = Integer.toHexString(random.nextInt(256)).toUpperCase(); 17 18 //判断红色代码的位数 19 red = red.length()==1 ? "0" + red : red ; 20 //判断绿色代码的位数 21 green = green.length()==1 ? "0" + green : green ; 22 //判断蓝色代码的位数 23 blue = blue.length()==1 ? "0" + blue : blue ; 24 //生成十六进制颜色值 25 String color = "#"+red+green+blue; 26 return color; 27 }

5.java正则表达式取出匹配字符串

举例如下,

1 2package javatest; 3 4 5import java.util.regex.Matcher; 6import java.util.regex.Pattern; 7 8public class JavaTest 9{ 10 public static void main( String args[] ){ 11 12 // 指定模式 13 String line = "This order was placed for QT3000! OK?"; 14 String pattern = "(\\D*)(\\d+)(.*)"; 15 16 // 创建 Pattern 对象 17 Pattern r = Pattern.compile(pattern); 18 19 // 创建 matcher 对象 20 Matcher m = r.matcher(line); 21 if (m.find( )) { 22 System.out.println("Found value: " + m.group(1) ); 23 System.out.println("Found value: " + m.group(2) ); 24 System.out.println("Found value: " + m.group(3) ); 25 } else { 26 System.out.println("NO MATCH"); 27 } 28 } 29}

打印

1Found value: This order was placed for QT 2Found value: 3000 3Found value: ! OK?

6.Java整数和字符串的相互转化

以下是把整形地i转化为字符串s,把Double、Float、Long与字符串操作的操作类似。

(1)把int转化为String

1String s=""+i; 2String s=Integer.toString(i); 3String s=String.valueOf(i);

(2)把String转化为int型:

1int i = Integer.intValue(s); 2int i=Integer.parsenInt(s); 3int i=Integer.valueOf(s).intValue();

(3)Integer转换成int:

1Integer i = new Integer(10); 2int k = i.intValue(); 3//即Integer.intValue();

(4)int转换成Integer:

1int i = 10; 2Integer it = new Integer(i);

(5)String转换成Integer:

1String str = "10"; 2Integer it = Integer.valueOf(str);

(6)Integer转换成String:

1Integer it = new Integer(10); 2String str = it.toString();

(8)String转换成BigDecimal:

1BigDecimal bd = new BigDecimal(str);

7.获取当前时间日期字符串

使用Date类

1package javatest; 2 3/** 4 * 5 * @author Lenovo 6 */ 7import java.text.SimpleDateFormat; 8import java.util.Date; 9 10public class JavaTest 11{ 12 public static void main( String args[] ){ 13 14 Date d = new Date(); 15 SimpleDateFormat date = new SimpleDateFormat("yyyy-MM-dd"); 16 String str1 = date.format(d); 17 SimpleDateFormat datetime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 18 String str2 = datetime.format(d); 19 System.out.println(str1); 20 System.out.println(str2); 21 22 } 23}

结果

12019-12-11 22019-12-11 11:21:21

8.生成指定范围的随机数

生成 MIN~MAX 之间的随机数:

1Random rand = new Random(); 2int randNumber =rand.nextInt(MAX - MIN + 1) + MIN; // randNumber 将被赋值为一个 MIN 和 MAX 范围内的随机数

9.快速生成10位时间戳

1Long newTime = System.currentTimeMillis(); 2String time = newTime.toString().substring(0, 10);

本文原文首发来自博客专栏Java专栏,由本人转发至https://www.helloworld.net/p/kdzhM1h0nCjb,其他平台均属侵权,可点击https://blog.csdn.net/CUFEECR/article/details/103341711查看原文,也可点击https://blog.csdn.net/CUFEECR浏览更多优质原创内容。

点赞
收藏

评论区

加载中...

相关推荐

MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1

文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s

Oracle 分组与拼接字符串同时使用

SELECTT.,ROWNUMIDFROM(SELECTT.EMPLID,T.NAME,T.BU,T.REALDEPART,T.FORMATDATE,SUM(T.S0)S0,MAX(UPDATETIME)CREATETIME,LISTAGG(TOCHAR(

MySQL部分从库上面因为大量的临时表tmp_table造成慢查询

背景描述Time:20190124T00:08:14.70572408:00User@Host:@Id:Schema:sentrymetaLast_errno:0Killed:0Query_time:0.315758Lock_

皕杰报表之UUID

​在我们用皕杰报表工具设计填报报表时,如何在新增行里自动增加id呢?能新增整数排序id吗?目前可以在新增行里自动增加id,但只能用uuid函数增加UUID编码,不能新增整数排序id。uuid函数说明:获取一个UUID,可以在填报表中用来创建数据ID语法:uuid()或uuid(sep)参数说明:sep布尔值,生成的uuid中是否包含分隔符'',缺省为

手写Java HashMap源码

HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程22

2020年前端实用代码段,为你的工作保驾护航

有空的时候,自己总结了几个代码段,在开发中也经常使用,谢谢。1、使用解构获取json数据let jsonData  id: 1,status: "OK",data: 'a', 'b';let  id, status, data: number   jsonData;console.log(id, status, number )