Java编程 经验技巧汇总
1.JSONArray数组如何循环遍历出来
[Java] 纯文本查看 复制代码
| 
 01 
02 
03 
04 
05 
06 
07 
08 
09 
10 
11 
12 
13 
14 
15 
16 
17 
18 
19 
20 
 | 
package xxx;import net.sf.json.JSONArray;import net.sf.json.JSONObject;public class Test {public static void main(String[] args) {        /*author:命运的信徒         * date:2019/5/18         */        String str ="[{'otitle':'会','source':'7'},{'otitle':'不会','source':'3'}]";        //1.把字符串类型的json数组对象转化JSONArray        JSONArray json=JSONArray.fromObject(str);        //2、循环遍历这个数组         for(int i=0;i<json.size();i++){                 //3、把里面的对象转化为JSONObject                  JSONObject job = json.getJSONObject(i);                     // 4、把里面想要的参数一个个用.属性名的方式获取到                  System.out.println(job.get("otitle")+":"+job.get("source")) ;                   }        }} | 
可参考https://blog.csdn.net/qq_37591637/article/details/90319229
2.生成UNIX时间戳(精度:秒)
[Java] 纯文本查看 复制代码
| 
 01 
02 
03 
04 
05 
06 
07 
08 
09 
10 
11 
12 
13 
14 
 | 
public class Test {    public static void main(String[] args) {        //生成随机时间        long offset = Timestamp.valueOf("2012-01-01 00:00:00").getTime();        long end = Timestamp.valueOf("2013-01-01 00:00:00").getTime();        long diff = end - offset + 1;        Timestamp rand = new Timestamp(offset + (long)(Math.random() * diff));        System.out.println(rand);        //下面两个是一样的结果,都是当前时间(UNIX时间戳)        System.out.println(System.currentTimeMillis() / 1000);        System.out.println(Calendar.getInstance().getTimeInMillis() / 1000);    }} | 
3.随机生成时间
[AppleScript] 纯文本查看 复制代码
| 
 01 
02 
03 
04 
05 
06 
07 
08 
09 
10 
11 
 | 
Random   rand   =   new   Random();SimpleDateFormat   format   =   new   SimpleDateFormat( "yyyy-MM-dd ");Calendar   cal   =   Calendar.getInstance();cal.set(1900,   0,   1);long   start   =   cal.getTimeInMillis();cal.set(2008,   0,   1);long   end   =   cal.getTimeInMillis();for(int   i   =   0;   i   <   10;   i++)   {Date   d   =   new   Date(start   +   (long)(rand.nextDouble()   *   (end   -   start)));System.out.println(format.format(d));} | 
4.随机生成颜色
方式一: 给定范围获得随机颜色
[Java] 纯文本查看 复制代码
| 
 01 
02 
03 
04 
05 
06 
07 
08 
09 
10 
11 
12 
13 
 | 
private Color getRandColor(int fc, int bc) {       Random random = new Random();      if (fc > 255)         fc = 255;      if (bc > 255)         bc = 255;      int r = fc + random.nextInt(bc - fc);      int g = fc + random.nextInt(bc - fc);      int b = fc + random.nextInt(bc - fc);      return new Color(r, g, b);     }getRandColor(200, 250) | 
方式二:生成随机十六进制颜色代码
[Java] 纯文本查看 复制代码
| 
 01 
02 
03 
04 
05 
06 
07 
08 
09 
10 
11 
12 
13 
14 
15 
16 
17 
18 
19 
20 
21 
22 
23 
24 
25 
26 
27 
 | 
//随机生成颜色代码    public String getColor(){        //红色        String red;        //绿色        String green;        //蓝色        String blue;        //生成随机对象        Random random = new Random();        //生成红色颜色代码        red = Integer.toHexString(random.nextInt(256)).toUpperCase();        //生成绿色颜色代码        green = Integer.toHexString(random.nextInt(256)).toUpperCase();        //生成蓝色颜色代码        blue = Integer.toHexString(random.nextInt(256)).toUpperCase();        //判断红色代码的位数        red = red.length()==1 ? "0" + red : red ;        //判断绿色代码的位数        green = green.length()==1 ? "0" + green : green ;        //判断蓝色代码的位数        blue = blue.length()==1 ? "0" + blue : blue ;        //生成十六进制颜色值        String color = "#"+red+green+blue;        return color;    } | 
更多java学习资料可关注:itheimaGZ获取
                    
                
                
            
        
浙公网安备 33010602011771号