Java工具类

Posted mature1021

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Java工具类相关的知识,希望对你有一定的参考价值。

package com.data2;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.temporal.ChronoUnit;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

public class Util {
    /**
     * 对象序列化
     * 
     * @param t
     * @return
     * @throws Exception
     */
    public <T> byte[] toBytes(T t) throws Exception {
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        ObjectOutputStream outputStream = new ObjectOutputStream(out);
        outputStream.writeObject(t);
        byte[] bytes = out.toByteArray();
        if (outputStream != null) {
            outputStream.close();
        }
        if (out != null) {
            out.close();
        }
        return bytes;
    }

    /**
     * 反序列
     * 
     * @param buffer
     * @return
     * @throws ClassNotFoundException
     * @throws IOException
     */
    public Object byteToClass(byte[] buffer) throws ClassNotFoundException, IOException {
        ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(buffer);
        ObjectInputStream in = new ObjectInputStream(byteArrayInputStream);
        Object obj = in.readObject();
        if (in != null) {
            in.close();
        }
        if (byteArrayInputStream != null) {
            byteArrayInputStream.close();
        }
        return obj;

    }

    /**
     * 获取当天零点时间
     * 
     * @return
     */
    private static long getDay00AM() {
        Calendar cal = Calendar.getInstance();
        cal.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), cal.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
        Date beginOfDate = cal.getTime();
        return beginOfDate.getTime();
    }

    /**
     * 获取当天零点后n分钟后的long时间
     * 
     * @return
     */
    public static long getFutureToTime(int minutes) {
        long MILLIS_PER_MINUTE = 60 * 1000;
        return (getDay00AM() + (MILLIS_PER_MINUTE * minutes));
    }

    /**
     * 判断某当前时间是否在一个区间内
     * 
     * @param sourceTime
     *            时间区间,半闭合,如 10:00-20:00
     * @param curTime
     *            需要判断的时间 如10:00
     * @return
     * 
     */
    public static boolean isInTime(String curTime, String sourceTime) {
        if (sourceTime == null || !sourceTime.contains("-") || !sourceTime.contains(":")) {
            throw new IllegalArgumentException("Illegal Argument arg:" + sourceTime);
        }
        if (curTime == null || !curTime.contains(":")) {
            throw new IllegalArgumentException("Illegal Argument arg:" + curTime);
        }
        String[] args = sourceTime.split("-");
        SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
        try {
            long now = sdf.parse(curTime).getTime();
            long start = sdf.parse(args[0]).getTime();
            long end = sdf.parse(args[1]).getTime();
            if (args[1].equals("00:00")) {
                args[1] = "24:00";
            }
            if (end < start) {
                if (now >= end && now < start) {
                    return false;
                } else {
                    return true;
                }
            } else {
                if (now >= start && now < end) {
                    return true;
                } else {
                    return false;
                }
            }
        } catch (ParseException e) {
            e.printStackTrace();
            throw new IllegalArgumentException("Illegal Argument arg:" + sourceTime);
        }

    }

    /**
     * 获取当前时间到0:00的剩余时间
     * 
     * @return
     */
    public static long getCountDown() {
        LocalDateTime midnight = LocalDateTime.now().plusDays(1).withHour(0).withMinute(0).withSecond(0).withNano(0);
        long seconds = ChronoUnit.SECONDS.between(LocalDateTime.now(), midnight);
        return seconds;
    }

    /**
     * list复制
     * 
     * @param src
     * @return
     */
    @SuppressWarnings("unchecked")
    public static <T> List<T> deepCopy(List<T> src) {
        ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
        ObjectOutputStream out = null;
        List<T> dest = null;
        ByteArrayInputStream byteIn = null;
        ObjectInputStream in = null;
        try {
            out = new ObjectOutputStream(byteOut);
            out.writeObject(src);
            byteIn = new ByteArrayInputStream(byteOut.toByteArray());
            in = new ObjectInputStream(byteIn);
            dest = (List<T>) in.readObject();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } finally {
            try {
                byteOut.close();
                out.close();
                byteIn.close();
                in.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return dest;
    }  
}

 

以上是关于Java工具类的主要内容,如果未能解决你的问题,请参考以下文章

Java多线程与并发库高级应用-工具类介绍

Java多线程与并发库高级应用-工具类介绍

java 代码片段

java代码在片段活动中不起作用

如何将工具栏添加到android中的片段?

AWS S3工具类