Java编程 经验技巧汇总

时间:2022-07-22
本文章向大家介绍Java编程 经验技巧汇总,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

文章目录

  • 1.JSONArray数组如何循环遍历
  • 2.生成UNIX时间戳(精度:秒)
  • 3.随机生成时间
  • 4.随机生成颜色
  • 5.java正则表达式取出匹配字符串
  • 6.Java整数和字符串的相互转化
  • 7.获取当前时间日期字符串
  • 8.生成指定范围的随机数
  • 9.快速生成10位时间戳

1.JSONArray数组如何循环遍历

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时间戳(精度:秒)

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.随机生成时间

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.随机生成颜色

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

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)

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

 //随机生成颜色代码
    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;
    }

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

举例如下,

package javatest;


import java.util.regex.Matcher;
import java.util.regex.Pattern;
 
public class JavaTest
{
    public static void main( String args[] ){
 
      // 指定模式
      String line = "This order was placed for QT3000! OK?";
      String pattern = "(\D*)(\d+)(.*)";
 
      // 创建 Pattern 对象
      Pattern r = Pattern.compile(pattern);
 
      // 创建 matcher 对象
      Matcher m = r.matcher(line);
      if (m.find( )) {
         System.out.println("Found value: " + m.group(1) );
         System.out.println("Found value: " + m.group(2) );
         System.out.println("Found value: " + m.group(3) ); 
      } else {
         System.out.println("NO MATCH");
      }
   }
}

打印

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

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

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

(1)把int转化为String

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

(2)把String转化为int型:

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

(3)Integer转换成int:

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

(4)int转换成Integer:

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

(5)String转换成Integer:

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

(6)Integer转换成String:

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

(8)String转换成BigDecimal:

BigDecimal bd = new BigDecimal(str);

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

使用Date类

package javatest;

/**
 *
 * @author Lenovo
 */
import java.text.SimpleDateFormat;
import java.util.Date;
 
public class JavaTest
{
    public static void main( String args[] ){
        
        Date d = new Date();
        SimpleDateFormat date = new SimpleDateFormat("yyyy-MM-dd");
        String str1 = date.format(d);
        SimpleDateFormat datetime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String str2 = datetime.format(d);
        System.out.println(str1);
        System.out.println(str2);
        
   }
}

结果

2019-12-11
2019-12-11 11:21:21

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

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

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

9.快速生成10位时间戳

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