基础语法
可变参数
public void variableParam(String ... str){
// str为数组类型
String[] arr= str;
// 数组转List
List<String> list = Arrays.asList(str);
}
java时间操作
时间加减
// 获取一个时间操作对象
Calendar calendar = Calendar.getInstance();
// 放入需要操作的时间
calendar.setTime(new Date());
//加一天
calendar.add(Calendar.DATE, 1);
// calendar.add(Calendar.DATE, -1); 为负就是减
// 加一月
calendar.add(Calendar.MONTH, 1);
// 加一年
calendar.add(Calendar.YEAR, 1);
// 取出操作后的时间
calendar.getTime();
时间格式化,字符串时间转化
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
// 时间转字符串
String format = sdf.format(new Date());
// 字符串转时间
Date parse = sdf.parse(format);
反射
peopleInfo entry = new peopleInfo();
// 反射获取实体类字段集合
Field[] declaredFields = entry.getClass().getDeclaredFields();
// 获取字段名
String name = declaredField[0].getName();
// 第一个字母改成大写
name = name.substring(0, 1).toUpperCase() + name.substring(1);
// 拼接出get方法
Method meth = entry.getClass().getDeclaredMethod("get" + name);
// 执行方法获取值
Object invoke = meth.invoke(entry);
注解
@filedInterface:
package com.security.annotation.IAnnotation;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
public @interface filedInterface {
/**
* 别名
*/
String alias() default "";
}
// 判断类头上是否有此注解
if (entry.getClass().isAnnotationPresent(classInterface.class)){
// 反射获取实体类字段集合
Field[] declaredFields = entry.getClass().getDeclaredFields();
for (Field declaredField : declaredFields) {
// 获取字段名
String name = declaredField.getName();
// 第一个字母改成大写
name = name.substring(0, 1).toUpperCase()
+ name.substring(1);
// 拼接出get方法
Method meth = entry.getClass().getDeclaredMethod("get" + name);
// 执行方法获取值
Object invoke = meth.invoke(entry);
// 判断字段上是否有此注解
if (declaredField.isAnnotationPresent(filedInterface.class)){
// 获取注解类容
filedInterface annotation = declaredField.getAnnotation(filedInterface.class);
String alias = annotation.alias();
if (!StringUtils.hasText(alias)){
alias = name;
}
String json = "{"+alias+":"+"'"+invoke+"'"+"}";
System.out.println(json);
}
}
}
entry.getClass().isAnnotationPresent(classInterface.class)
instanceof
// 是否为String类型
Boolean isString = invoke instanceof String;
// 是否为int 或 Integer 类型
Boolean isInt = invoke instanceof Integer;
List
API
// 数组转list
String[] arr= {"a","b","c"};
List<String> list = Arrays.asList(arr);
数组stream操作(jdk1.8)
List 分组
// 假设entry里有很多值
List<Entry> entry= ...,
// jdk1.8 stream方法;Entry::type【实体类:分类字段】;结果:Map<分类字段, 一组集合>
Map<String, List<Entry>> collect = entry.stream().collect(Collectors.groupingBy(Entry::type));
for (Map.Entry<String, List<Entry>> entry : collect.entrySet()) {
// 每一个集合里type字段值都是一样的
List<Entry> vos = collect.get(entry.getKey());
}
泛型转换 对象a <一> 对象b
List<Menu> list = ...;
// 使用new 对象必须有下述方法
Set<MenuVo> result = list.stream().map(MenuVo::new).collect(Collectors.toSet()); // 可以toList()
// 对象类
public class MenuVo {
// 必有方法
public MenuVo(Menu menu) { BeanUtils.copyProperties(menu, this);}
/**
* 菜单名称
*/
private String name;
...
泛型转换 对象a <一> 非对象
List<Menu> list = ...;
List<String> nos = list .stream().map(Menu::no).collect(Collectors.toList());
Map
遍历Map
// 有值的map
Map<String, Entry> entryMap = ...;
for (Map.Entry<String, Entry> entry : entryMap.entrySet()) {
// 每一个集合里type字段值都是一样的
List<Entry> vos = collect.get(entry.getKey());
}
赋值
// BeanUtils.copyProperties(Object 源数据, Object 目标数据);user => userVo
// apache和spring框架有此方法 ,相同字段自动赋值
BeanUtils.copyProperties(user, userVo);
// k,可变参数,忽略字段(不赋值)
BeanUtils.copyProperties(user, userVo, k1, k2, k3, ...);
16进制转byte[]
public byte[] hexStringToByte1(String hex) {
int len = (hex.length() / 2);
byte[] result = new byte[len];
char[] achar = hex.toCharArray();
for (int i = 0; i < len; i++) {
int pos = i * 2;
result[i] = (byte) ((byte) "0123456789abcdef".indexOf(achar[pos]) << 4 | (byte) "0123456789abcdef".indexOf(achar[pos + 1]));
}
System.out.println(Arrays.toString(result));
return result;
}