zoukankan      html  css  js  c++  java
  • 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;
        }  
    }
  • 相关阅读:
    第一课:js命名空间的介绍,js对象的扩展以及js数组化
    浏览器缓存机制-社招必问知识
    2013年前端校园招聘经历
    GBDT(MART) 迭代决策树简介
    coursera 公开课 文本挖掘和分析(text mining and analytics) week 1 笔记
    Predicting purchase behavior from social media-www2013
    Recommending branded products from social media -RecSys 2013-20160422
    2016年数据挖掘,机器学习可投会议
    java 中遍历hashmap 和hashset 的方法
    NLPIR分词工具的使用(java环境下)
  • 原文地址:https://www.cnblogs.com/mature1021/p/10419839.html
Copyright © 2011-2022 走看看