当前位置 博文首页 > u011767319的博客:Java个人笔记

    u011767319的博客:Java个人笔记

    作者:[db:作者] 时间:2021-09-22 12:55

    java环境搭建

    linux环境

    • jdk1.8

      #编写文件
      vim  /etc/profile
      
      export JAVA_HOME=/usr/local/jdk1.8.0_191
      export CLASSPATH=$JAVA_HOME/lib:$JRE_HOME/lib:
      export PATH=$JAVA_HOME/bin:$PATH
      
      #使配置文件生效
      source /etc/profile
      
    • jdk14

      win10 jdk14 配置已经不需要 jre 和 class_home了

      JAVA_HOME    E:\JAVA\jdk-14
      Path   %JAVA_HOME%\bin
      

      centos jdk14 环境变量配置

      #编写文件
      vim  /etc/profile
      #使配置文件生效
      source /etc/profile
      
      JAVA_HOME=/usr/local/java/jdk-14
      export PATH=$JAVA_HOME/bin:$PATH
      

    TreeSet 对一个实体类进行排序

    HashSet 无序唯一,TreeSet 有序唯一

    HashSet<NavigationTable> navigationTableHashSet = new HashSet<>();
    	....省略(导航栏代码)
    TreeSet<NavigationTable> youxu = new TreeSet<>(navigationTableHashSet);
    
    ------------------------------
    实体类
    public class NavigationTable implements Comparable<NavigationTable> {
    	.....省略实体
    	
    @Override           //对实体类的id进行排序
    	public int compareTo(NavigationTable o) {
    		int a = this.getId() - o.getId();
    		return a;
    	}
    }
    

    Java中Double保留后小数位的几种方法

    Java中Double保留后小数位的几种方法网页地址

    java获取当天,前天,明天,本周,本月,本年的开始日期时间和结束日期时间

    java获取当天,前天,明天,本周,本月,本年的开始日期时间和结束日期时间

    Math

    1. 舍掉小数取整:Math.floor(3.5)=3
    2. 四舍五入取整:Math.rint(3.5)=4
    3. 进位取整:Math.ceil(3.1)=4
    4. 取绝对值:Math.abs(-3.5)=3.5
    5. 取余数:A%B = 余数

    迭代器

    .next()在一个循环了不要出现两次,不然会越界。本人已经体验过了。在此做个记录

    Iterator it = jsonObject.entrySet().iterator();
    List<String> zgBm = new ArrayList<>();
    while (it.hasNext()) {
        Map.Entry entry = (Map.Entry) it.next();
        if (entry.getValue().toString().equals("true")) {
            zgBm.add(entry.getKey().toString());
        }
    }
    

    local日期常用方法

    getYear()    int    获取当前日期的年份
    getMonth()    Month    获取当前日期的月份对象
    getMonthValue()    int    获取当前日期是第几月
    getDayOfWeek()    DayOfWeek    表示该对象表示的日期是星期几
    getDayOfMonth()    int    表示该对象表示的日期是这个月第几天
    getDayOfYear()    int    表示该对象表示的日期是今年第几天
    withYear(int year)    LocalDate    修改当前对象的年份
    withMonth(int month)    LocalDate    修改当前对象的月份
    withDayOfMonth(int dayOfMonth)    LocalDate    修改当前对象在当月的日期
    isLeapYear()    boolean    是否是闰年
    lengthOfMonth()    int    这个月有多少天
    lengthOfYear()    int    该对象表示的年份有多少天(365或者366)
    plusYears(long yearsToAdd)    LocalDate    当前对象增加指定的年份数
    plusMonths(long monthsToAdd)    LocalDate    当前对象增加指定的月份数
    plusWeeks(long weeksToAdd)    LocalDate    当前对象增加指定的周数
    plusDays(long daysToAdd)    LocalDate    当前对象增加指定的天数
    minusYears(long yearsToSubtract)    LocalDate    当前对象减去指定的年数
    minusMonths(long monthsToSubtract)    LocalDate    当前对象减去注定的月数
    minusWeeks(long weeksToSubtract)    LocalDate    当前对象减去指定的周数
    minusDays(long daysToSubtract)    LocalDate    当前对象减去指定的天数
    compareTo(ChronoLocalDate other)    int    比较当前对象和other对象在时间上的大小,返回值如果为正,则当前对象时间较晚,
    isBefore(ChronoLocalDate other)    boolean    比较当前对象日期是否在other对象日期之前
    isAfter(ChronoLocalDate other)    boolean    比较当前对象日期是否在other对象日期之后
    isEqual(ChronoLocalDate other)    boolean    比较两个日期对象是否相等
    
    
    // 获取今天的日期
    LocalDate now = LocalDate.now();
    // 今天是几号
    int dayofMonth = now .getDayOfMonth();
    // 今天是周几(返回的是个枚举类型,需要再getValue())
    int dayofWeek = now .getDayOfWeek().getValue();
    // 今年是哪一年
    int dayofYear = now .getDayOfYear();
    // 取本月第1天:
    LocalDate firstDayOfThisMonth=now .with(TemporalAdjusters.firstDayOfMonth()); 
    // 取本月第2天:
    LocalDate secondDayOfThisMonth = now .withDayOfMonth(2);
    // 取本月最后一天,再也不用计算是28,29,30还是31:
    LocalDate lastDayOfMonth=now .with(TemporalAdjusters.lastDayOfMonth()); 
    // 取下一天:
    LocalDate firstDayOfNextMonth = lastDayOfMonth.plusDays(1);
    // 取2019年1月第一个周一:
    LocalDate firstMondayOf2017 = LocalDate.parse("2019-01-01").with(TemporalAdjusters.firstInMonth(DayOfWeek.MONDAY)); 
    //本月的第一天
    LocalDate monthFirstDay = LocalDate.of(now.getYear(),now.getMonth(),1);
     //本月的最后一天
    LocalDate monthLastDay =now.with(TemporalAdjusters.lastDayOfMonth());
    //上月的第一天
    LocalDate date = now.minusMonths(1).withDayOfMonth(1);
    //上月的最后一天
    LocalDate date1 = date.with(TemporalAdjusters.lastDayOfMonth());
    

    LocalDateTime

    LocalDateTime now = LocalDateTime.now();
    System.out.println("计算两个时间的差:");
    LocalDateTime end = LocalDateTime.now();
    Duration duration = Duration.between(now,end);
    long days = duration.toDays(); //相差的天数
    long hours = duration.toHours();//相差的小时数
    long minutes = duration.toMinutes();//相差的分钟数
    long millis = duration.toMillis();//相差毫秒数
    long nanos = duration.toNanos();//相差的纳秒数
    System.out.println(now);
    System.out.println(end);
    
    System.out.println("发送短信耗时【 "+days+"天:"+hours+" 小时:"+minutes+" 分钟:"+millis+" 毫秒:"+nanos+" 纳秒】");
    
    
    //计算两个日期之间是的差额
    LocalDate localDate = LocalDate.now();
    LocalDate localDate2 = localDate.minusYears(5).minusMonths(10).minusDays(2);
    Period next = Period.between(localDate2,localDate);
    log.info("---------------年--------------------");
    log.info(String.valueOf(next.getYears()));
    log.info("---------------月--------------------");
    log.info(String.valueOf(next.getMonths()));
    log.info("----------------天-------------------");
    log.info(String.valueOf(next.getDays()));
    
    //获取当前系统的时区
    OffsetDateTime.now().getOffset();  //输出 "+08:00" 不同时区输出不同值
    //获取当天开始时间的时间戳
    LocalDateTime.of(LocalDate.now(),LocalTime.MIN).toInstant(OffsetDateTime.now().getOffset()).toEpochMilli();
    //获取当天结束时间的时间戳
    LocalDateTime.of(LocalDate.now(), LocalTime.MAX).toInstant(OffsetDateTime.now().getOffset()).toEpochMilli();
    

    例子-计算时间(可以参考)

    /**
         * 计算时间类型
         *
         * @param days       天
         * @param startDateL 用户开始时间
         * @param endDateL   用户结束时间
         * @return {@link List<Object>}
         */
        public static List<Object> toCalculateTypeTime(Integer days, Long startDateL, Long endDateL) {
            List<Object> res = new ArrayList<>();
            int type = 1; //1查小时 2查天
            LocalDateTime nowDateTime = LocalDateTime.now();
            //返回小时
            if ((Objects.nonNull(days) && days.equals(1))
                    || (Objects.nonNull(startDateL) && Objects.nonNull(endDateL) && startDateL.equals(endDateL))) {
                LocalDateTime startDateTime = null;
                LocalDateTime endDateTime = null;
                if (Objects.nonNull(days)) {
                    startDateTime = nowDateTime.of(LocalDate.now(), LocalTime.MIN).atZone(ZoneOffset.ofHours(8)).toLocalDateTime();
                    endDateTime = nowDateTime.of(LocalDate.now(), LocalTime.MAX).atZone(ZoneOffset.ofHours(8)).toLocalDateTime();
                }
                if (Objects.nonNull(startDateL) && Objects.nonNull(endDateL)) {
                    LocalDate startDate = LocalDateTime.ofEpochSecond(startDateL, 0, ZoneOffset.ofHours(8)).toLocalDate();
                    startDateTime = LocalDateTime
                            .of(startDate,LocalTime.MIN)
                            .atZone(ZoneOffset.ofHours(8))
                            .toLocalDateTime();
                    LocalDate endDate = LocalDateTime.ofEpochSecond(endDateL, 0, ZoneOffset.ofHours(8)).toLocalDate();
                    endDateTime = LocalDateTime
                            .of(endDate, LocalTime.MAX)
                            .atZone(ZoneOffset.ofHours(8))
                            .toLocalDateTime();
                }
                if (Objects.isNull(startDateTime) || Objects.isNull(endDateTime)) {
                    return null;
                }
                res.add(type);
                res.add(startDateTime);
                res.add(endDateTime);
                return res;
            }
            //返回天
            type = 2;
            LocalDate startDate = null;
            LocalDate endDate = null;
            if (Objects.nonNull(days) && (Objects.isNull(startDateL) || Objects.isNull(endDateL))) {
                LocalDateTime minusDays = nowDateTime.minusDays(days);
                startDate = minusDays.atZone(ZoneOffset.ofHours(8)).toLocalDate();
                endDate = nowDateTime.atZone(ZoneOffset.ofHours(8)).toLocalDate();
                res.add(type);
                res.add(startDate);
                res.add(endDate);
                return res;
            } else if (Objects.isNull(days) && Objects.nonNull(startDateL) || Objects.nonNull(endDateL)) {
                startDate = LocalDateTime.ofEpochSecond(startDateL, 0, ZoneOffset.ofHours(8)).toLocalDate();
                endDate = LocalDateTime.ofEpochSecond(endDateL, 0, ZoneOffset.ofHours(8)).toLocalDate();
                res.add(type);
                res.add(startDate);
                res.add(endDate);
                return res;
            }
            type = 0;
            res.add(type);
            return res;
        }
    

    校验身份证

    //校验身份证
        final static Map<Integer, String> zoneNum = new HashMap<>();
        final static int[] PARITYBIT = {'1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2'};
        final static int[] POWER_LIST = {7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10,
                5, 8, 4, 2};
        static {
            zoneNum.put(11, "北京");
            zoneNum.put(12, "天津");
            zoneNum.put(13, "河北");
            zoneNum.put(14, "山西");
            zoneNum.put(15, "内蒙古");
            zoneNum.put(21, "辽宁");
            zoneNum.put(22, "吉林");
            zoneNum.put(23, "黑龙江");
            zoneNum.put(31, "上海");
            zoneNum.put(32, "江苏");
            zoneNum.put(33, "浙江");
            zoneNum.put(34, "安徽");
            zoneNum.put(35, "福建");
            zoneNum.put(36, "江西");
            zoneNum.put(37, "山东");
            zoneNum.put(41, "河南");
            zoneNum.put(42, "湖北");
            zoneNum.put(43, "湖南");
            zoneNum.put(44, "广东");
            zoneNum.put(45, "广西");
            zoneNum.put(46, "海南");
            zoneNum.put(50, "重庆");
            zoneNum.put(51, "四川");
            zoneNum.put(52, "贵州");
            zoneNum.put(53, "云南");
            zoneNum.put(54, "西藏");
            zoneNum.put(61, "陕西");
            zoneNum.put(62, "甘肃");
            zoneNum.put(63, "青海");
            zoneNum.put(64, "新疆");
            zoneNum.put(71, "台湾");
            zoneNum.put(81, "香港");
            zoneNum.put(82, "澳门");
            zoneNum.put(91, "外国");
        }
        /**
         * @Description: 校验身份证是否合法
         * @Param: [certNo]
         * @return: boolean
         * @Author: 刘俊秦
         * @Date: 2019/6/3 0003
         */
        public static boolean isIDCard(String certNo) {
            if (certNo == null || (certNo.length() != 15 && certNo.length() != 18))
                return false;
            final char[] cs = certNo.toUpperCase().toCharArray();
            //校验位数
            int power = 0;
            for (int i = 0; i < cs.length; i++) {
                if (i == cs.length - 1 && cs[i] == 'X')
                    break;//最后一位可以 是X或x
                if (cs[i] < '0' || cs[i] > '9')
                    return false;
                if (i < cs.length - 1) {
                    power += (cs[i] - '0') * POWER_LIST[i];
                }
            }
    
            //校验区位码
            if (!zoneNum.containsKey(Integer.valueOf(certNo.substring(0, 2)))) {
                return false;
            }
    
            //校验年份
            String year = certNo.length() == 15 ? getIdcardCalendar() + certNo.substring(6, 8) : certNo.substring(6, 10);
    
            final int iyear = Integer.parseInt(year);
            if (iyear < 1900 || iyear > Calendar.getInstance().get(Calendar.YEAR))
                return false;//1900年的PASS,超过今年的PASS
    
            //校验月份
            String month = certNo.length() == 15 ? certNo.substring(8, 10) : certNo.substring(10, 12);
            final int imonth = Integer.parseInt(month);
            if (imonth < 1 || imonth > 12) {
                return false;
            }
    
            //校验天数
            String day = certNo.length() == 15 ? certNo.substring(10, 12) : certNo.substring(12, 14);
            final int iday = Integer.parseInt(day);
            if (iday < 1 || iday > 31)
                return false;
    
            //校验"校验码"
            if (certNo.length() == 15)
                return true;
            return cs[cs.length - 1] == PARITYBIT[power % 11];
        }
    
        private static int getIdcardCalendar() {
            GregorianCalendar curDay = new GregorianCalendar();
            int curYear = curDay.get(Calendar.YEAR);
            int year2bit = Integer.parseInt(String.valueOf(curYear).substring(2));
            return year2bit;
        }