Skip to content

序列化json字符串,大小写的问题,默认会将属性转成小写,JsonProperty注解处理此类问题

java
import com.fasterxml.jackson.annotation.JsonProperty;
public class MsiResponse<T> {
    @JsonProperty("ErrorCode")
    String errorCode;
    @JsonProperty("ErrorMessage")
    String errorMessage;
    @JsonProperty("Data")
    T data;
}

LocalDateTime转Date

java
java.sql.Date.from(java.time.Instant.ofEpochSecond(java.time.LocalDateTime.now().toEpochSecond(java.time.ZoneOffset.of("+8"))))
//这种写法也行
Date.from(LocalDateTime.now().atZone(ZoneId.systemDefault()).toInstant())

时间戳(秒)转 LocalDateTime

java
Long remindTime = 1534825831L//时间戳
LocalDateTime dateTime = LocalDateTime.ofEpochSecond(remindTime,0, ZoneOffset.ofHours(8));

时间戳(毫秒)转 LocalDateTime

java
Long remindTime = 1594629582725;//时间戳毫秒
LocalDateTime localDateTime = new Date(remindTime).toInstant().atOffset(ZoneOffset.of("+8")).toLocalDateTime();

字符串按格式转LocalDateTime

java
LocalDateTime.parse("2020-08-26 18:20:00", DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));

字符串按格式转Date

java
//注意:SimpleDateFormat对象是线程不安全的,大量并发场景下,对象内部字符串转化成日期对象的容器会被互相覆盖,导致报错
java.util.Date dste = new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse("2020-08-22 13:48:08");

tomcat添加默认的并发流线程数

java
-Djava.util.concurrent.ForkJoinPool.common.parallelism=30

java并发流(Groovy)

java
/**
 * 自定义并发流
 * @param list 需要多线程并发执行的数据
 * @param threadNum 线程数,为null或小于等于0时使用系统公用线程池,否则使用独立的线程池
 * @param closure 任务闭包
 */
//@CompileStatic //静态编译
static <T> void customParallelStream (List<T> list, Integer threadNum, Closure closure){
    if(threadNum == null || threadNum <= 0){
        //使用系统公用的线程池,默认数量为cpu核数,可以通过配置文件修改
        list.parallelStream().parallel().forEach(closure)
    }else {
        //使用独立的指定数量的线程池资源,不与其他任务共享
        new ForkJoinPool(threadNum).invoke(
                ForkJoinTask.adapt({
                    list.parallelStream().parallel().forEach(closure)
                }) as ForkJoinTask
        )
    }
}

获取线程安全的List

java
List<Long> ids = Collections.synchronizedList(new ArrayList<Long>());

静态块初始化获取本地主机的MAC地址

java
/** 本地主机IP地址 */
final static String LOCAL_IP = getLocalIp()
/** 获取本地主机IP地址 */
static String getLocalIp(){
    try{
        return Inet4Address.getLocalHost().getHostAddress()
    }catch(Exception e){
        e.printStackTrace()
        return null
    }
}

/** 本地主机MAC地址 */
final static String LOCAL_MAC = getLocalMac()
/** 获取本地主机MAC地址 */
static String getLocalMac(){
    try{
        //获取mac数组,转16进制之后,左补全两位0,然后拼接"-"转大写输出
        byte[] macArrs = NetworkInterface.getByInetAddress(InetAddress.getLocalHost()).getHardwareAddress()
        StringBuilder sb = new StringBuilder()
        for(int i = 0; i<macArrs.length; i++){
            if(i > 0){
                sb.append("-")
            }
            sb.append(StringUtils.leftPad(Integer.toHexString((int) (macArrs[i] & 0xff)), 2, "0"))
        }
        return StringUtils.upperCase(sb.toString())
    }catch(Exception e){
        e.printStackTrace()
        return null
    }
}

/** 本地主机外网IP地址(好像不能获取,待验证) */
final static String PUBLIC_IP = getPublicIp()
/** 获取本地主机外网IP地址 */
static String getPublicIp(){
    try{
        Enumeration<NetworkInterface> networks = NetworkInterface.getNetworkInterfaces()
        if (networks != null){
            while (networks.hasMoreElements()) {
                Enumeration<InetAddress> enumeration = networks.nextElement().getInetAddresses()
                if(enumeration != null){
                    InetAddress ip
                    while (enumeration.hasMoreElements()){
                        ip = enumeration.nextElement()
                        if(ip != null && ip instanceof Inet4Address && ip.isSiteLocalAddress() && !StringUtils.equalsIgnoreCase(ip.getHostAddress(), LOCAL_IP)){
                            return ip.getHostAddress()
                        }
                    }
                }
            }

        }
    }catch(Exception e){
        e.printStackTrace()
    }
    return LOCAL_IP
}

页脚:版权前显示的信息