码迷,mamicode.com
首页 > 编程语言 > 详细

Java工具类

时间:2019-02-22 18:08:34      阅读:129      评论:0      收藏:0      [点我收藏+]

标签:exce   simple   NPU   ora   begin   row   otf   private   gettime   

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工具类

标签:exce   simple   NPU   ora   begin   row   otf   private   gettime   

原文地址:https://www.cnblogs.com/mature1021/p/10419839.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!