Javaのプログラミングのスキル経験の概要

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を

UNIXタイムスタンプを生成します。2.(精度:秒)

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)

第二の方法:ランダムな16進数の色コードを生成します

 //随机生成颜色代码
    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の相互変換

以下の操作はダブル、フロート、ロングと文字列の操作に似て、私はsの文字列に整形されます。

INT文字列に(1)

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

int型列に(2):

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

(3)整数はint型に変換しました:

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

(4)INTは整数に変換しました:

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

(5)文字列を整数に変換するには:

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

(6)整数列に変換しました:

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

(8)文字列の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);
公開された51元の記事 ウォンの賞賛184 ・は 30000 +を見て

おすすめ

転載: blog.csdn.net/CUFEECR/article/details/103341711