---
# System prepended metadata

title: Java

---

# Java 

## json 操作
```
JSONObject object = new JSONObject();   //new 空的JSONObject
JSONArray array = new JSONArray();        //new JSONArray

JSONArray res = dbpool.toJSONArray(sql);   
    
for (int i = 0; i < res.length(); i++) {
    JSONObject obj = res.getJSONObject(i);
    
    JSONArray arrays = obj.getJSONArray("arrays");
}
```
## Regex
```
String.matches("regex")
String testString = "ABCaaabcaac";
System.out.println("String.matches():\t"+testString.matches(".*aa.*"));
```

## Timestamp to Date
```
long currentTime = System.currentTimeMillis();  
Date date = new Date(currentTime);
SimpleDateFormat formatter = new SimpleDateFormat("yyyy年-MM月dd日-HH时mm分ss秒");
System.out.println(formatter.format(date));
```


## java-jwt
創建token
```
Algorithm al = Algorithm.HMAC256("secretkey");
    String token = JWT.create()
            .withIssuer("簽發者")
            .withSubject("用戶")
            .withClaim("userid", "string123")
            .withExpiresAt(new Date(System.currentTimeMillis()+360000))
            .sign(al);

```
解析token
```
Algorithm algorithm = Algorithm.HMAC256("emailauthority");
        JWTVerifier verifier = JWT.require(algorithm).build();
        DecodedJWT jwt = verifier.verify(key);

        long exp = jwt.getExpiresAt().getTime();
        String account = jwt.getClaim("userid").asString();
```

## Hashmap
```
HashMap<String,Integer> hashMap = new HashMap<>();
```
遍歷元素
```
Iterator iterator = hashMap.keySet().iterator();
        while (iterator.hasNext()){
            String key = (String)iterator.next();
            System.out.println(key+"="+hashMap.get(key));
        }

---------------------

本文来自 wxgxgp 的CSDN 博客 ，全文地址请点击：https://blog.csdn.net/wxgxgp/article/details/79194360?utm_source=copy 
```