将字符串按逗号进行拆分成字符串数组
String str = "id1,id2,id3";
String[] strArray = str.split(",");
for(int i=0;i<strArray.length;i++){
System.out.println(strArray[i]);
}
/**
* 输出结果:
* id1
* id2
* id3
*/
关于时间格式输出,以及时间秒数的加减(负数为减,正数是加)
LocalDateTime end = LocalDateTime.now();
System.out.println(end);
LocalDateTime start = end.plusSeconds(-Integer.parseInt("5"));
System.out.println(start);
DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
String startTime = df.format(start);
String endTime = df.format(end);
System.out.println("startTime:" + startTime + ", endTime:" + endTime);
/**
* 输出结果:
* 2021-07-28T15:18:53.440
* 2021-07-28T15:18:48.440
* startTime:2021-07-28 15:18:48, endTime:2021-07-28 15:18:53
*/
map集合的putAll方法
//两个map具有不同的key
HashMap map1=new HashMap();
map1.put("1", "A");
HashMap map2 = new HashMap();
map2.put("2", "B");
map2.put("3", "C");
map1.putAll(map2);
System.out.println(map1);
//两个map具有重复的key
HashMap map3=new HashMap();
map3.put("1", "A");
HashMap map4 = new HashMap();
map4.put("1", "B");
map4.put("3", "C");
map3.putAll(map4);
System.out.println(map3);
/**
* 输出结果:
* {1=A, 2=B, 3=C}
* {1=B, 3=C}
*/
将map集合转化成url的参数字符串输出
public static void main(String[] args) {
Map<String, String> paramMap = new HashMap<>();
paramMap.put("startTime", "2021-07-28 15:18:48");
paramMap.put("endTime", "2021-07-28 15:18:53");
paramMap.put("serialNum", "1008611");
String urlParams = null;
try {
urlParams = HttpUtil.getUrlParams(paramMap);
} catch (IOException e) {
e.printStackTrace();
}
System.out.println(urlParams);
/**
* 输出结果:
* key : serialNum value : 1008611
* [serialNum=1008611]
* key : startTime value : 2021-07-28 15:18:48
* [serialNum=1008611, startTime=2021-07-28 15:18:48]
* key : endTime value : 2021-07-28 15:18:53
* [serialNum=1008611, startTime=2021-07-28 15:18:48, endTime=2021-07-28 15:18:53]
* serialNum=1008611&startTime=2021-07-28+15%3A18%3A48&endTime=2021-07-28+15%3A18%3A53
* serialNum=1008611&startTime=2021-07-28+15%3A18%3A48&endTime=2021-07-28+15%3A18%3A53
*/
}
public static String getUrlParams(Map<String, String> headerMap) throws IOException {
List<NameValuePair> params = Lists.newArrayList();
//第一种遍历map的方法,通过加强for循环map.keySet(),然后通过键key获取到value值
for (String s : headerMap.keySet()) {
params.add(new BasicNameValuePair(s, headerMap.get(s)));
System.out.println("key : " + s + " value : " + headerMap.get(s));
System.out.println(params);
}
String str = "";
//转换为键值对
str = EntityUtils.toString(new UrlEncodedFormEntity(params, Consts.UTF_8));
System.out.println(str);
return str;
}
日志输出:(使用{},对变量进行输出)
logger.error("invoke root api failed, fullUrl:{}, result:{}", fullUrl, result);