zoukankan      html  css  js  c++  java
  • 日常编码小技巧

    Java基础:

    List转Map集合:

    /**
     * 将List转为Map
     *
     * @param list         原数据
     * @param keyExtractor Key的抽取规则
     * @param <K>          Key
     * @param <V>          Value
     * @return
     */
    public static <K, V> Map<K, V> listToMap(List<V> list, Function<V, K> keyExtractor) {
        /*等于空就直接返回空的map*/
        if (list == null || list.isEmpty()) {
            return new HashMap<>();
        }
        Map<K, V> map = new HashMap<>(list.size());
        for (V element : list) {
            /*设置map的K为list的值*/
            K key = keyExtractor.apply(element);
            if (key == null) {
                continue;
            }
            /*设置map的V为list的值*/
            map.put(key, element);
        }
        return map;
    }

    使用:

    1 public static void main(String[] args) {
    2     // 1.创建List集合
    3     List<? extends Serializable> list = new ArrayList<>(Arrays.asList(1, 2, 42, "23"));
    4     // 2.调用方法将list集合转map集合
    5     Map<? extends Serializable, ? extends Serializable> map = listToMap(list, Function.identity());
    6     // 3.遍历map集合
    7     Set<? extends Map.Entry<? extends Serializable, ? extends Serializable>> entries = map.entrySet();
    8     entries.forEach(System.out::println);
    9 }

    输出:

    封装好的日期时间类 

    package com.mixky.mobile.policy.util;
    
    import java.text.SimpleDateFormat;
    import java.util.Calendar;
    import java.util.Date;
    import java.util.Locale;
    
    import com.mixky.toolkit.DateUtil;
    
    public class DateFormatUtil {
        
         public static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        /**
         * 格式化结束日期,并加一天
         * @param endtime
         * @return
         */
        public static String formatEndDate(String endtime){
            Date endDate=DateUtil.parseDate(endtime, "yyyy-MM-dd");
            Calendar c = Calendar.getInstance();  
            c.setTime(endDate);
            c.add(Calendar.DAY_OF_MONTH, 1);// 今天+1天  
            String endDateStr=DateUtil.format(c.getTime(), "yyyy-MM-dd");
            return endDateStr;
        }
        
        /**
         * 格式化结束日期 不做任何处理
         * @param endtime
         * @return
         */
        public static String formatEndDateNow(String endtime){
            Date endDate=DateUtil.parseDate(endtime, "yyyy-MM-dd");
            Calendar c = Calendar.getInstance();  
            c.setTime(endDate);
            //c.add(Calendar.DAY_OF_MONTH, 1);// 今天+1天  
            String endDateStr=DateUtil.format(c.getTime(), "yyyy-MM-dd");
            return endDateStr;
        }
        
        /**
         * 日期加/减**天
         * @param date
         * @param day
         * @return
         */
        public static String formateDateHandleDay(String date, int day){
            Date endDate=DateUtil.parseDate(date, "yyyy-MM-dd");
            Calendar c = Calendar.getInstance();  
            c.setTime(endDate);
            c.add(Calendar.DAY_OF_MONTH, day);
            String endDateStr=DateUtil.format(c.getTime(), "yyyy-MM-dd");
            return endDateStr;
        }
        
        /**
         * 日期加/减**月
         * @param date
         * @param month
         * @return
         */
        public static String formateDateHandleMonth(String date, int month, String formate){
            Date endDate=DateUtil.parseDate(date, formate);
            Calendar c = Calendar.getInstance();  
            c.setTime(endDate);
            c.add(Calendar.MONTH, month);
            String endDateStr=DateUtil.format(c.getTime(), formate);
            return endDateStr;
        }
        
        /**
         * 日期加/减**年
         * @param date
         * @param year
         * @return
         */
        public static String formateDateHandleYear(String date, int year){
            Date endDate=DateUtil.parseDate(date, "yyyy");
            Calendar c = Calendar.getInstance();  
            c.setTime(endDate);
            c.add(Calendar.YEAR, year);
            String endDateStr=DateUtil.format(c.getTime(), "yyyy");
            return endDateStr;
        }
        
         
        /**
         * 获取当前月的第一天
         * @return
         */
        public static String formateDateFirstDay(){
            Calendar cale =  Calendar.getInstance();
            String firstday;  
            //获取前月的第一天  
            cale = Calendar.getInstance();  
            cale.add(Calendar.MONTH, 0);  
            cale.set(Calendar.DAY_OF_MONTH, 1); 
            
            //将小时至0
            cale.set(Calendar.HOUR_OF_DAY, 0);
            //将分钟至0
            cale.set(Calendar.MINUTE, 0);
            //将秒至0
            cale.set(Calendar.SECOND,0);
            //将毫秒至0
            cale.set(Calendar.MILLISECOND, 0);  
            
            firstday = sdf.format(cale.getTime());
            return firstday;
        }
        
        /**
         * 获取当前月的最后一天
         * @return
         */
        public static String formateDateLastDay(){
            Calendar cale =  Calendar.getInstance();
            String lastday;  
            //获取前月的最后一天  
            cale = Calendar.getInstance();  
            cale.add(Calendar.MONTH, 1);  
            cale.set(Calendar.DAY_OF_MONTH, 0); 
           
            //将小时至23
            cale.set(Calendar.HOUR_OF_DAY, 23);
            //将分钟至59
            cale.set(Calendar.MINUTE, 59);
            //将秒至59
            cale.set(Calendar.SECOND,59);
            //将毫秒至59
            cale.set(Calendar.MILLISECOND, 59);
            lastday = sdf.format(cale.getTime());
            return lastday;
        }
        
        //获取当前月的天数
        public static int getDayOfMonth(){
           Calendar aCalendar = Calendar.getInstance(Locale.CHINA);
           int day=aCalendar.getActualMaximum(Calendar.DATE);
           return day;
        }
        
        //获取当天凌晨0点0分0秒Date
        public Date getZeroOfDay(){
            Calendar calendar = Calendar.getInstance();
            calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH),
                        0, 0, 0);
            Date beginOfDate = calendar.getTime();
            return beginOfDate;
        }
        
        //获取当天23点59分59秒Date
        public static Date getLastOfDay(){
            Calendar calendar = Calendar.getInstance();
            calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH),
                        23, 59, 59);
            Date endOfDate  = calendar.getTime();
            return endOfDate ;
        }
        
     
        /**
         * 获取当前时间的前一天同一时间
         * 
         */
        private String getBeforeDay() {        
            Calendar calendar = Calendar.getInstance();
            int dd2 = calendar.get(Calendar.DATE);
            calendar.set(Calendar.DATE, dd2 - 1);
            calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH),
                    23, 59, 59);
            String beforeDay = sdf.format(calendar.getTime());
            return beforeDay;
        }
    
        /**
         * 获取当前时间的后一天同一时间
         * 
         */
        private String getAfterDay() {
            Calendar calendar = Calendar.getInstance();
            int dd2 = calendar.get(Calendar.DATE);
            calendar.set(Calendar.DATE, dd2 + 1);
            String afterDay = sdf.format(calendar.getTime());
            return afterDay;
        }
     
        /**
         * 获取当前时间的前一天0点0分0秒
         * 
         */
        public String getZeroOfBeforeDay() {        
            Calendar calendar = Calendar.getInstance();
            int dd2 = calendar.get(Calendar.DATE);
            calendar.set(Calendar.DATE, dd2 - 1);
            calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH),
                    0, 0, 0);
            String beforeDay = sdf.format(calendar.getTime());
            return beforeDay;
        }
     
        /**
         * 获取当前时间的前一天23点59分59秒
         * 
         */
        public String getLastOfBeforeDay() {        
            Calendar calendar = Calendar.getInstance();
            int dd2 = calendar.get(Calendar.DATE);
            calendar.set(Calendar.DATE, dd2 - 1);
            calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH),
                    23, 59, 59);
            String beforeDay = sdf.format(calendar.getTime());
            return beforeDay;
        }
        
        /**
         * 获取当前时间后一天0点0分0秒
         * 
         */
        private String getZeroOfAfterDay() {
            Calendar calendar = Calendar.getInstance();
            int dd2 = calendar.get(Calendar.DATE);
            calendar.set(Calendar.DATE, dd2 + 1);
            calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH),
                    0, 0, 0);
            String afterDay = sdf.format(calendar.getTime());
            return afterDay;
        }
        
        /**
         * 获取当前时间后一天23点59分59秒
         * 
         */
        private String getLastOfAfterDay() {
            Calendar calendar = Calendar.getInstance();
            int dd2 = calendar.get(Calendar.DATE);
            calendar.set(Calendar.DATE, dd2 + 1);
            calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH),
                    23, 59, 59);
            String afterDay = sdf.format(calendar.getTime());
            return afterDay;
        }
        
        /**
         * 获取当前月的第count天0分0秒0分
         * @return
         */
        public String getZeroOfDays(int count){
            Calendar cale =  Calendar.getInstance();
            //获取前月的第一天  
            cale = Calendar.getInstance();  
            cale.add(Calendar.MONTH, 0);  
            cale.set(Calendar.DAY_OF_MONTH, 1); 
            
            int dd2 = cale.get(Calendar.DATE);
            cale.set(Calendar.DATE, dd2 + count);
            cale.set(cale.get(Calendar.YEAR), cale.get(Calendar.MONTH), cale.get(Calendar.DAY_OF_MONTH),
                    0, 0, 0);
            String day = sdf.format(cale.getTime());
            return day;
        }
        
        /**
         * 获取当前月的第count天23分59秒59分
         * @return
         */
        public String getLastOfDays(int count){
            Calendar cale =  Calendar.getInstance(); 
            //获取前月的第一天  
            cale = Calendar.getInstance();  
            cale.add(Calendar.MONTH, 0);  
            cale.set(Calendar.DAY_OF_MONTH, 1); 
            
            int dd2 = cale.get(Calendar.DATE);
            cale.set(Calendar.DATE, dd2 + count);
            cale.set(cale.get(Calendar.YEAR), cale.get(Calendar.MONTH), cale.get(Calendar.DAY_OF_MONTH),
                    23, 59, 59);
            String day = sdf.format(cale.getTime());
            return day;
        }
        
        /**
         * 获取当前月的第count天
         * @return
         */
        public static Date getDays(int count){
            Calendar cale =  Calendar.getInstance(); 
            //获取前月的第一天  
            cale = Calendar.getInstance();  
            cale.add(Calendar.MONTH, 0);  
            cale.set(Calendar.DAY_OF_MONTH, 1); 
            
            int dd2 = cale.get(Calendar.DATE);
            cale.set(Calendar.DATE, dd2 + count);
            Date date = cale.getTime();
            return date;
        }
        
        
        /**
         * 获取当前时间的前一月同一时间
         * 
         */
        public static String getBeforeMonth(Date date) {        
            Calendar calendar = Calendar.getInstance();
             //过去一月
            calendar.setTime(date);
            calendar.add(Calendar.MONTH, -1);
            Date m = calendar.getTime();
            String mon = sdf.format(m);
            return mon;
        }
        
        /**
         * 获取上个月的第一天
         * @return
         */
        public static String getBeforeMonthFirstDay(){
            Calendar cale =  Calendar.getInstance();
            String firstday;  
            //获取上个月的第一天  
            cale = Calendar.getInstance();  
            cale.add(Calendar.MONTH, -1);  
            cale.set(Calendar.DAY_OF_MONTH, 1); 
            
            //将小时至0
            cale.set(Calendar.HOUR_OF_DAY, 0);
            //将分钟至0
            cale.set(Calendar.MINUTE, 0);
            //将秒至0
            cale.set(Calendar.SECOND,0);
            //将毫秒至0
            cale.set(Calendar.MILLISECOND, 0);  
            
            firstday = sdf.format(cale.getTime());
            return firstday;
        }
        
        /**
         * 获取下个月的最后一天
         * @return
         */
        public static String getBeforeMonthLastDay(){
            Calendar cale =  Calendar.getInstance();
            String lastday;  
            //获取下个月的最后一天  
            cale = Calendar.getInstance();  
            cale.add(Calendar.MONTH, 0);  
            cale.set(Calendar.DAY_OF_MONTH, 0); 
           
            //将小时至23
            cale.set(Calendar.HOUR_OF_DAY, 23);
            //将分钟至59
            cale.set(Calendar.MINUTE, 59);
            //将秒至59
            cale.set(Calendar.SECOND,59);
            //将毫秒至59
            cale.set(Calendar.MILLISECOND, 59);
            lastday = sdf.format(cale.getTime());
            return lastday;
        }
      
        
    }

     正确停止一个线程:

    终止一个线程可以调用线程的stop方法,但是stop这个方法会强行终止,没有保存的数据将会丢失,实际开发中一般会采用boolean标记的方式进行终止:

    public class FileDemo {
        static boolean flag = false;
    
        public static void setFlag(boolean flag) {
            FileDemo.flag = flag;
        }
    
        public static void main(String[] args) throws InterruptedException {
            final Thread t1 = new Thread(new Runnable() {
                @Override
                public void run() {
                    for (int i = 0; i < 10; i++) {
                        if (flag) {
                            return;
                        }
                        System.out.println(Thread.currentThread().getName() + "==》" + i);
                        try {
                            Thread.sleep(1000);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }, "t1");
            t1.start();
            
            Thread.sleep(5000);
            /*5S后终止线程*/
            setFlag(true);
        }
    }

    正则匹配IP的合法性

    /**
     * 判断IP地址的合法性,这里采用了正则表达式的方法来判断
     * return true,合法
     */
    public static boolean ipCheck(String text) {
        if (text != null && !text.isEmpty()) {
            // 定义正则表达式
            String regex = "^(1\d{2}|2[0-4]\d|25[0-5]|[1-9]\d|[1-9])\." +
                    "(1\d{2}|2[0-4]\d|25[0-5]|[1-9]\d|\d)\." +
                    "(1\d{2}|2[0-4]\d|25[0-5]|[1-9]\d|\d)\." +
                    "(1\d{2}|2[0-4]\d|25[0-5]|[1-9]\d|\d)$";
            // 判断ip地址是否与正则表达式匹配
            if (text.matches(regex)) {
                // 返回判断信息
                return true;
            } else {
                // 返回判断信息
                return false;
            }
        }
        return false;
    }
    

      

  • 相关阅读:
    字符串与字典常用命令
    Python学习之路:字符串常用操作
    Python学习之路:购物车实例
    面试题2017
    c#语法学习
    结构化设计模式-桥接模式
    结构型设计模式-适配器模式
    .Net Cache
    设计模式的六大原则
    uml类图关系
  • 原文地址:https://www.cnblogs.com/zhangzhixi/p/14849998.html
Copyright © 2011-2022 走看看