开发常识

tech2023-09-12  93

判断LIst list为不为空 CollectionUtils.isEmpty(list) 导入包 import org.springframework.util.CollectionUtils;

判断String name为不为空 StringUtils.isBlank(name) 导入包 import org.apache.commons.lang3.StringUtils;

场景 一个类两个字段部分数据重复

for (int i = 0; i < supplyChainCommoditySubclassAddReq.size(); i++) { SupplyChainCommoditySubclassAddReq chainCommoditySubclassAddReq = supplyChainCommoditySubclassAddReq.get(i); String relatedParenIds=chainCommoditySubclassAddReq.getRelatedParentIds(); Long relatedId=chainCommoditySubclassAddReq.getRelatedCid(); String relatedCid = Long.toString(relatedId); String relatedParentIds = relatedParenIds.replace(relatedCid, ""); chainCommoditySubclassAddReq.setRelatedParentIds(relatedParentIds); }

List数据分页

List<Integer> list = new ArrayList<>(); list.add(1); list.add(2); list.add(3); list.add(4); list.add(5); list.add(6); list.add(7); list.add(8); list.add(9); list.add(11); list.add(12); list.add(13); list.add(14); list.add(15); int pageNo=(pageNo-1)*pageSize; //每页的起始索引 int pageSize=5; // 每页记录数 Integer sum = list.size(); //记录总数 if (pageNo+pageSize > sum) { list = list.subList(pageNo,sum); }else { list = list.subList(pageNo,pageNo+pageSize); } System.out.println( list.size());

@SneakyThrows 处理异常

测试代码运行时间

long startTime = System.currentTimeMillis(); //获取开始时间 long endTime = System.currentTimeMillis(); //获取结束时间 System.out.println("程序运行时间:" + (endTime - startTime) + "ms")

KB MB GB转换

public static void main(String[] args) { long size=2753L; DecimalFormat df = new DecimalFormat("#.00"); String fileSizeString = ""; if (size < 1024) { fileSizeString = df.format((double) size) + "KB"; } else if (size < 1048576) { fileSizeString = df.format((double) size / 1024) + "MB"; } else if (size < 1073741824) { fileSizeString = df.format((double) size / 1048576) + "GB"; } System.out.println(fileSizeString); }

图片url读取 当url错误时解决方案

@Override public String getBase64ByImgUrl(String imgUrl){ getUrlJude(imgUrl); try { URL urlConet = new URL(imgUrl); URLConnection con = urlConet.openConnection(); InputStream in = con.getInputStream(); byte[] buffer = new byte[4 * 1024]; int n = 0; ByteArrayOutputStream out = new ByteArrayOutputStream(); while ((n = in.read(buffer)) != -1) { out.write(buffer, 0, n); } byte[] bs = out.toByteArray(); String s = Base64.getEncoder().encodeToString(bs); String file=s.substring(0,4); if (!file.equals("/9j/")){ throw new GlobalException(EnumStatusCode.SAVE_FILE_URL_MISTAKE); } return s; } catch (IOException e) { throw new GlobalException(EnumStatusCode.SAVE_FILE_URL_MISTAKE); } } /** * 读取url 并判断大小 */ private void getUrlJude(String urlString){ int length=0; URL url; try { url = new URL(urlString); HttpURLConnection urlcon=(HttpURLConnection)url.openConnection();//打开连接 //根据响应获取文件大小 length=urlcon.getContentLength(); urlcon.disconnect();//关闭连接 }catch(Exception e){ throw new GlobalException(EnumStatusCode.SAVE_FILE_URL_MISTAKE); } long lengths = (long)length; if (lengths < 1024) { } else if (lengths < 1048576) { } else if (lengths < 1073741824) { double data3=(double) lengths / 1048576; BigDecimal data1 = new BigDecimal("3"); BigDecimal data2 = new BigDecimal(data3); if (data1.compareTo(data2) < 0) { throw new GlobalException(EnumStatusCode.SAVE_FILE_URL_SIZE_OUT); } }else{ throw new GlobalException(EnumStatusCode.SAVE_FILE_URL_SIZE_OUT); } }

年月日的获取

Calendar calendar = Calendar.getInstance(); int monthNow = calendar.get(Calendar.MONTH) + 1; int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK); int dayOfMonth = calendar.get(Calendar.DAY_OF_MONTH); int dayNow = calendar.get(Calendar.DATE); int yearNow = calendar.get(Calendar.YEAR); // 获取当前小时 int hour = calendar.get(Calendar.HOUR_OF_DAY); // 获取当前分钟 int minute = calendar.get(Calendar.MINUTE);

mysql 设置表-字段-为空 ALTER TABLE 表名称 MODIFY 字段名称 数据类型 DEFAULT NULL; 设置表-字段-为不为空 LTER TABLE 表 ALTER COLUMN 字段名 数据类型 NOT NULL;

string切割 String str = “abc,ac,ab,0”; String[] strs=str.split(","); for(int i=0,len=strs.length;i<len;i++){ System.out.print(strs[i].toString()+" "); }

最新回复(0)