# Utils **Repository Path**: juer2017/utils ## Basic Information - **Project Name**: Utils - **Description**: Util各种工具库 - **Primary Language**: Unknown - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 2 - **Forks**: 0 - **Created**: 2023-02-27 - **Last Updated**: 2024-10-18 ## Categories & Tags **Categories**: Uncategorized **Tags**: 实用工具类 ## README ### 使用 ``` //引入依赖(最新版本请看发行版) implementation 'com.gitee.juer2017:utils:1.1.3' ``` ``` allprojects { repositories { google() jcenter() //引入maven仓库地址 maven { url 'https://jitpack.io' } } } ``` #### AppVersionNameAndCode ``` //版本名称获取 AppVersionNameAndCode.getVersionName(this); 版本号获取 AppVersionNameAndCode.getVersionName(this); ``` #### ChinaDate、ChinaDate2 ``` //公历转农历 //年月日 String ymd = TimeUtils.dateToString(TimeUtils.getTimeStame(), "yyyy-MM-dd"); //年 String y = TimeUtils.dateToString(TimeUtils.getTimeStame(), "yyyy"); //农历年、月、日 String ny, nm, nd; try { //公历日期转换为农历日期 ny = ChinaDate2.solarToLunar(ymd, false).substring(0, 4); nm = ChinaDate2.solarToLunar(ymd, false).substring(5, 7); nd = ChinaDate2.solarToLunar(ymd, false).substring(8, 10); } catch (Exception e) { throw new RuntimeException(e); } Calendar today = Calendar.getInstance(); @SuppressLint("SimpleDateFormat") SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd"); try { Date time = simpleDateFormat.parse(ymd); if (time != null) { today.setTime(time); } } catch (ParseException e) { throw new RuntimeException(e); } ChinaDate lunar = new ChinaDate(today); //公历转yyyy-MM-dd格式的农历(传参:公历年月日,是否显示闰字) ChinaDate2.solarToLunar(ymd, true); ``` #### CopyAndPaste ``` //复制 CopyAndPaste.copyClieck(this, "复制内容"); //粘贴 String str = CopyAndPaste.pasteClieck(this); if ("".equals(str.trim())) { Toast.makeText(this, "粘贴内容为空", Toast.LENGTH_SHORT).show(); } else { binding.et.setText(str); } ``` #### FileUtils ``` //文件后缀名(不带点)与文件名获取 FileUtils.getExtensionName(file.getName()); ``` #### ImgDonwload ``` //网络图片下载(传参:上下文,图片url, 图片名称) private static String[] PERMISSIONS_STORAGE = {"android.permission.READ_EXTERNAL_STORAGE","android.permission.WRITE_EXTERNAL_STORAGE"}; private static final int REQUEST_EXTERNAL_STORAGE = 1; final int permission = ActivityCompat.checkSelfPermission(this,"android.permission.WRITE_EXTERNAL_STORAGE"); //检测是否有写的权限 if (permission != PackageManager.PERMISSION_GRANTED) { // 没有写的权限,去申请写的权限,会弹出对话框 ActivityCompat.requestPermissions(this, PERMISSIONS_STORAGE, REQUEST_EXTERNAL_STORAGE); } else { ImgDonwload.donwloadImg(PreviewActivity.this, img, id); } @Override public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { super.onRequestPermissionsResult(requestCode, permissions, grantResults); if (requestCode == REQUEST_EXTERNAL_STORAGE && grantResults[0] == PackageManager.PERMISSION_GRANTED) { ImgDonwload.donwloadImg(MainActivity.this, url, String.valueOf(System.nanoTime())); } else { Toast.makeText(this, "同意权限后方能下载", Toast.LENGTH_SHORT).show(); } } ``` #### IprangeUtils() ``` //判断网络IP是否在指定网段中(传参:指定网段,当前网络ip) IprangeUtils.isContains("172.30.29.0/25","172.30.29.129"); ``` #### LoadingDialog ``` //显示加载框 LoadingDialog loadingDialog = new LoadingDialog(this); loadingDialog.setMessage("加载中..."); loadingDialog.show(); //关闭加载框 loadingDialog.dismiss(); ``` #### LunarUtils ``` //将农历年月日转换为公历年月日:月(若为闰月则传入负数)(传参:农历年,农历月,农历日) LunarUtils.getTranslateSolarString(Integer.parseInt(ny), Integer.parseInt(nm),Integer.parseInt(nd)); //判断公历年是否为闰年(传参:公里年) LunarUtils.isLeapYear(Integer.parseInt(y); //查询农历年的总天数(传参:要查的年份) LunarUtils.daysInLunarYear(Integer.parseInt(ny)); //查询农历年闰哪个月1-12,没闰传回0 String[] ry = new String[]{"不闰月", "闰正月", "闰二月", "闰三月", "闰四月", "闰五月", "闰六月", "闰七月","闰八月", "闰九月", "闰十月", "闰冬月", "闰腊月"}; ry[LunarUtils.leapMonth(Integer.parseInt(ny))]; ``` #### MarketUtils ``` //本机应用市场搜索应用 MarketUtils.openMarket(this); //本机应用市场搜索包名对应的应用 MarketUtils.getTools().openMarket(MainActivity.this,"com.quark.browser"); //指定应用市场搜索包名对应的应用 MarketUtils.getTools().openMarket(MainActivity.this, "com.quark.browser", MarketUtils.PACKAGE_NAME.HUAWEI_PACKAGE_NAME); //跳转到夸克并打开首页 //打开指定应用并打开相应页面(需要传入应用包名,类名) //注意:打开相应应用的页面配置文件中一定要加如下配置,不然会报Permission Denial: starting Intent 错误 MarketUtils.getTools().openInstalledApp(MainActivity.this, "com.quark.browser","com.ucpro.MainActivity"); /跳转到浏览器首页并打开链接 MarketUtils.getTools().openInstalledAppInURL(MainActivity.this, "com.quark.browser", "com.ucpro.MainActivity", "https://www.baidu.com"); //检测是否安装某应用(需要传入应用包名) MarketUtils.getTools().isAppInstalled(MainActivity.this, "com.quark.browser"); ``` #### MoneyValueFilter ``` //输入金额限制(保留小数点后两位有效数字) etPrice.setFilters(new InputFilter[]{new MoneyValueFilter().setDigits(2)}); ``` #### NetworkUtils ``` //是否有网络连接 NetworkUtils.isConnected(this); //WIFI是否开启 NetworkUtils.getWifiEnabled(this); //开启、关闭WIFI NetworkUtils.setWifiEnabled(this, true); //WIFI是否连接 NetworkUtils.isWifiConnected(this); //WIFI是否可用 NetworkUtils.isWifiAvailable(this); //是否使用移动数据 NetworkUtils.isMobileData(this); //是否使用4G NetworkUtils.is4G(this); //网络返回类型 NetworkUtils.getNetworkType(this); //ipv4地址 NetworkUtils.getIPAddress(true); //通过wifi返回ip地址 NetworkUtils.getIpAddressByWifi(this); ``` #### OneClickThree ``` //防止重复点击 binding.btnOneclick.setOnClickListener(v -> { if (!OneClickThree.isFastClick()) { ToastUtil.s(this, "点击了我"); } else { ToastUtil.s(this, "请不要连续操作"); } }); ``` #### PhoneInformation ``` //手机厂商 PhoneInformation.getManufacturer(); //手机型号 PhoneInformation.getModel(); //屏幕的宽度 PhoneInformation.getDeviceWidth(this); //屏幕的高度 PhoneInformation.getDeviceHeight(this); //屏幕的可用宽度 PhoneInformation.getDeviceKYWidth(this); //屏幕的可用高度 PhoneInformation.getDeviceKYHeight(this); //状态栏高度 PhoneInformation.getStatusBarHeight(this); ``` #### ReaderUtils ``` //选择的content://xxx文件路径转Storage文件路径 ReaderUtils.getPath(this, data.getData()); ``` #### RegularVerification ``` //验证手机号 if (!RegularVerification.isMobileNO(binding.etPhone.getText().toString())) { ToastUtil.s(this, "手机号格式不正确"); } else { ToastUtil.s(this, "验证成功"); } //8位数字加字母(包含大写)验证码验证 RegularVerification.isCode(codeNum); //3-16位用户名数字加字母(包含大写)验证 RegularVerification.isUserName(userName); //6-18位数字加字母(不包含大写)密码验证 RegularVerification.isPassWord(passWord); //8-16位数字、字母、特殊符号组合正则验证 RegularVerification.isPassWordZuhe(passWord); //8-16位数字、大小写字母、特殊符号(可自定义)组合正则验证 RegularVerification.isPassWordZuhe2(passWord); //8-16位数字、大小写字母、特殊符号(不可自定义)组合正则验证 RegularVerification.isPassWordZuheDaxiaoxie(passWord); //电子邮箱验证 RegularVerification.isEmail(email); //url链接验证 RegularVerification.isUrl(url); //IP地址验证 RegularVerification.isIP(ip); //中文字符验证 RegularVerification.isCN(cn); //正则判断emoji表情 RegularVerification.isEmoji(input); ``` #### TimeUtils ``` //时间戳转换成字符串,默认为"yyyy-MM-dd HH:mm:ss" TimeUtils.dateToString(time); //获取当前的时间戳 TimeUtils.getTimeStame(); //时间转换成字符串,指定格式 TimeUtils.dateToString(time,"yyyy.MM.dd HH:mm"); //待判断的时间 Calendar time = Calendar.getInstance(); time.set(2023, 9, 20); //时间区间的开始时间 Calendar startTime = Calendar.getInstance(); startTime.set(2023, 9, 10); // 时间区间的结束时间 Calendar endTime = Calendar.getInstance(); endTime.set(2023, 9, 20); //待判断的时间 Date currentDate = DateUtil.parseTime("2023-10-19", "yyyy-MM-dd"); //时间区间的开始时间 Date startDate = DateUtil.parseTime("2023-10-10", "yyyy-MM-dd"); //时间区间的结束时间 Date endDate = DateUtil.parseTime("2023-10-20", "yyyy-MM-dd"); Date date = DateUtil.parseTime("2023-10-20 16:40:51", "yyyy-MM-dd HH:mm:ss"); //当前时间为本月的第几周 TimeUtils.getCurrentWeekOfMonth(); //当前时间为本周的第几天 TimeUtils.getCurrentDayOfWeek(); //当前日期是星期几 TimeUtils.getCurrentDayOfWeekText(); //指定时间是否在时间区间内 TimeUtils.isTimeInRange(time, startTime, endTime); //指定时间是否在时间区间内 TimeUtils.isInTimeRange(currentDate, startDate, endDate); //求两个日期相差天数 TimeUtils.calculateDaysDifference(startDate, endDate); //返回友好时间跨度 TimeUtils.calculateTimeDifference(currentDate); //获取当前时间的字符串表示 TimeUtils.getCurrentDate("yyyy-MM-dd HH:mm:ss"); //将时间戳格式化为指定格式的字符串 TimeUtils.formatToStr(System.currentTimeMillis(), "yyyy-MM-dd HH:mm:ss"); //将日期对象格式化为指定格式的字符串 TimeUtils.formatToStr(date, "yyyy-MM-dd HH:mm:ss"); //格式化字符串时间为指定格式 TimeUtils.formatStringDate("2023-10-20 16:40:51", "yyyy.MM.dd HH:mm:ss"); //获取当前时间的日期对象 TimeUtils.getCurrentTime(); //将日期对象格式化为指定格式的时间字符串 TimeUtils.formatTime(DateUtil.getCurrentTime(), "yyyy.MM.dd HH:mm:ss"); //解析指定格式的时间字符串为日期对象 TimeUtils.parseTime("2023-10-20 16:40:51", "yyyy-MM-dd HH:mm:ss"); //计算两个日期之间的时间差,返回指定时间单位的差值 TimeUtils.getTimeDifference(startDate, endDate, TimeUnit.MINUTES); //判断指定时间是否在给定时间区间内 TimeUtils.isInTimeRange(currentDate, startDate, endDate); //判断指定年份是否为闰年 TimeUtils.isLeapYear(2024); //获取指定日期对象的年份 TimeUtils.getYearFromDate(date); //获取指定日期对象的月份 TimeUtils.getMonthFromDate(date); //获取指定日期对象的星期 TimeUtils.getWeekdayFromDate(date); ``` #### ToastUtil ``` //Toast.LENGTH_SHORT类型弹窗 ToastUtil.s(this, "msg"); //Toast.LENGTH_LONG类型弹窗 ToastUtil.l(this, "msg"); //带图片Toast.LENGTH_SHORT类型弹窗 ToastUtil.imageLS(MainActivity.this, "msg", R.drawable.ic); //带图片Toast.LENGTH_LONG类型弹窗 ToastUtil.imageLL(MainActivity.this, "msg", R.drawable.ic); ``` #### WaterMarkUtil ``` //添加水印 WaterMarkUtil.showWatermarkView(this, "Hello World!"); ``` #### WebSocketUtils ``` //需要的地方初始化WebSocket WebSocketUtils.getInstance().initSocket("ws://..."); //关闭WebSocket WebSocketUtils.getInstance().closeSocket(); ```