1、List集合去重
package com.ahc.demo; import org.junit.Test; import java.util.ArrayList; import java.util.List; import java.util.stream.Collectors; public class List集合去重 { @Test public void List集合去重(){ List<Integer> integerList = new ArrayList<>(); integerList.add(1); integerList.add(2); integerList.add(3); integerList.add(4); integerList.add(1); integerList.add(2); integerList.add(3); integerList.add(4); System.out.println(integerList); //list去重 List<Object> collect = integerList.stream().distinct().collect(Collectors.toList()); System.out.println(collect); } }
2、Long类型转换成double
package com.ahc.demo; import java.math.BigDecimal; import java.text.SimpleDateFormat; import java.time.Instant; import java.time.LocalDateTime; import java.time.ZoneId; import java.time.format.DateTimeFormatter; import java.util.Date; public class Long类型转成double { public static void main(String[] args) { // long类型的 184238903 转换成 double类型的1.84 Long increOld = 184238903L; String increOldStr = String.valueOf(increOld); BigDecimal decimal = new BigDecimal(increOldStr).divide(new BigDecimal("100000000")); double doubleValue = decimal.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue(); System.out.println(doubleValue); } }
3、定时任务
package com.ahc.task; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.scheduling.annotation.EnableScheduling; import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Component; import java.time.LocalDate; import java.time.LocalDateTime; import java.time.format.DateTimeFormatter; import java.util.List; import java.util.Map; @EnableScheduling @Component public class 定时任务 { //CllDataTask @Autowired //private ITfcPassMidService tfcPassMidService; @Scheduled(cron= "0 */10 * * * ?") //每十分钟 public void getCllData() { /*TfcPassMid tfcPassMid = new TfcPassMid(); //获取当前时间 DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd hh:mm:ss"); LocalDateTime now = LocalDateTime.now(); String nowTime = df.format(now); //当前时间前10分钟 LocalDateTime localDateTime = LocalDateTime.now().minusMinutes(10); String time = df.format(localDateTime); String datasql = "select * from normal.tfc_pass_mid where gcsj <= '"+nowTime+"' and " + " gcsj > '"+time+"'"; List<Map<String, Object>> dataMap = clickHouseClient.selectData(datasql); System.out.println(dataMap); for (Map<String, Object> data : dataMap) { tfcPassMid.setGcxh(data.get("gcxh").toString()); tfcPassMid.setKkbh(data.get("kkbh").toString()); tfcPassMid.setFxlx(data.get("fxlx").toString()); tfcPassMid.setHphm(data.get("hphm").toString()); String capture = data.get("capture_date").toString(); DateTimeFormatter df1 = DateTimeFormatter.ofPattern("yyyy-MM-dd"); LocalDate capture_date = LocalDate.parse(capture, df1); tfcPassMid.setCaptureDate(capture_date); tfcPassMidMapper.insert(tfcPassMid); }*/ } }
4、分页查询
package com.ahc.demo; public class 分页查询 { public static void main(String[] args) { /** * limit len,size * len:偏移量 从len后面一条开始展示 * size:每页显示的行数 * * len = (页数 - 1) * size */ //controller /** * public R<IPage<PersonVO>> page(Person person, Query query) { * IPage<PersonVO> pages = personService.selectPage(person, query); * return R.data(pages); * } */ //service /** * IPage<PersonVO> selectPage(Person person, Query query); */ //serviceImpl /** * public IPage<PersonVO> selectPage(Person person, Query query) { * Page<Person> page = new Page<Person>(); * if (query.getCurrent() == null ) { * query.setCurrent(1); * } * if (query.getSize() == null) { * query.setSize(10); * } * List<Person> list = personMapper.selectPersonPage((query.getCurrent() - 1) * query.getSize(), query.getSize(), person); * page.setRecords(list); * page.setTotal(personMapper.selectPersonCount(person)); * page.setPages(page.getTotal()/query.getSize()); * return PersonWrapper.build().pageVO(page); * } */ //mapper /** * List<Person> selectPersonPage(@Param("start")Integer start, @Param("size") Integer size, @Param("person")Person person); */ //xml /** * <select id="selectPersonPage" resultMap="personResultMap"> * select * from ryzt_pub_ry_info_td where 1 = 1 * <if test="person.gmsfhm != null and person.gmsfhm != ''"> * and gmsfhm like concat('%', #{person.gmsfhm}, '%') * </if> * order by gmsfhm * limit #{start}, #{size} * </select> */ } }
5、根据身份证计算年龄
package com.ahc.demo; import org.junit.Test; import java.text.SimpleDateFormat; import java.util.Date; public class 根据身份证计算年龄 { @Test public void getNl() { String sfzhm = "130927199508250011"; String temp = sfzhm == null ? null : sfzhm.substring(6,10); int i = howOld(temp); System.out.println("age:"+i); } public static int howOld(String birth) { SimpleDateFormat sdf = new SimpleDateFormat("yyyy"); String format = sdf.format(new Date()); int age = Integer.parseInt(format) - Integer.parseInt(birth); return age; } }
6、日期转换和时间格式化
package com.ahc.demo; import org.junit.Test; import java.time.Instant; import java.time.LocalDateTime; import java.time.ZoneId; import java.time.format.DateTimeFormatter; import java.util.Date; public class 日期转换和时间格式化 { @Test public void date类型转换成localDateTime(){ Date date = new Date(); Instant instant = date.toInstant(); ZoneId zoneId = ZoneId.systemDefault(); LocalDateTime localDateTime = instant.atZone(zoneId).toLocalDateTime(); System.out.println(localDateTime); } @Test public void LocalDateTime与String日期互相转换(){ DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");//定义时间格式 LocalDateTime time = LocalDateTime.now();//拿到现在的时间 String localTime = df.format(time);//现在的时间转化为String类型 LocalDateTime ldt = LocalDateTime.parse(localTime, df);//把刚刚转化的String类型再转为LocalDateTime类型 } @Test public void 获取系统当前时间(){ DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy年MM月dd日hh:mm");//时间格式化 dateTimeFormatter.format(LocalDateTime.now()); //获取当前系统时间 } //sql里格式化时间 String str = "SELECT DATE_FORMAT(create_time,'%Y-%m-%d %h:%m:%s') FROM `test`"; //xml里时间对比 /** <if test="param.timeStart != null"> and call_police_time >= #{param.timeStart} </if> <if test="param.timeEnd != null"> and call_police_time <![CDATA[<=]]> #{param.timeEnd} */ }
7、校验手机号工具类
package com.ahc.untils; import org.junit.Test; import org.springframework.util.StringUtils; import java.util.regex.Pattern; /** * @Desc 用于后台手机号的验证工具类 * @Date 2021/7/23 * @Author ahc */ public class 校验手机号工具类 { //MobileUtils private static final String REGEX_MOBILE ="((\\+86|0086)?\\s*)((134[0-8]\\d{7})|(((13([0-3]|[5-9]))|(14[5-9])|15([0-3]|[5-9])|(16(2|[5-7]))|17([0-3]|[5-8])|18[0-9]|19(1|[8-9]))\\d{8})|(14(0|1|4)0\\d{7})|(1740([0-5]|[6-9]|[10-12])\\d{7}))"; /** * 判断是否是手机号 * @param tel 手机号 * @return boolean true:是 false:否 */ public static boolean isMobile(String tel) { if (StringUtils.isEmpty(tel)){ return false;} return Pattern.matches(REGEX_MOBILE, tel); } @Test public void phone(){ String str = "18830430881"; System.out.println(str+":"+isMobile(str)); //错误 String strF = "123456"; System.out.println(strF+":"+isMobile(strF)); } }
8、自动生成编号工具类
package com.ahc.untils; import org.junit.Test; import java.text.DateFormat; import java.text.SimpleDateFormat; import java.util.Date; import java.util.concurrent.atomic.AtomicInteger; public class 自动生成编号工具类 { //SerialRandom private static final AtomicInteger COUNT = new AtomicInteger(); public static String createRand() { DateFormat df = new SimpleDateFormat("yyMMddHHmmss"); String format = df.format(new Date()); long time = Long.parseLong(format); if (time < -1) { time = -1; } return String.valueOf(time * 10000000 + COUNT.incrementAndGet()); } public static String createRand(String prefix, String count, int length) { StringBuffer sb = new StringBuffer(); sb.append(prefix).append(new SimpleDateFormat("yyyyMMdd").format(new Date())); length = length - sb.length() - count.length(); for(int i = length; i > 0; --i) { sb.append("0"); } sb.append(count); return sb.toString(); } @Test public void 实现(){ System.out.println(自动生成编号工具类.createRand()); System.out.println(自动生成编号工具类.createRand("xx","10",15)); } }
9、字符串拼接去掉最后一个逗号
package com.ahc.demo; import org.junit.Test; import java.util.ArrayList; import java.util.List; public class 字符串拼接去掉最后一个逗号 { @Test public void ping(){ String s1 = "我爱我家"; String s2 = "我爱中国"; String s3 = "我爱老婆"; List<String> arrayList = new ArrayList<>(); StringBuffer stringBuffer = new StringBuffer(); String str = new String(); arrayList.add(s1); arrayList.add(s2); arrayList.add(s3); System.out.println(arrayList); for (String arr : arrayList) { stringBuffer.append(arr).append(","); } System.out.println(stringBuffer); str = stringBuffer.deleteCharAt(stringBuffer.length()-1).toString(); System.out.println(str); } }
10、非空判断总结工具
public boolean isNull(Object value) { if (value == null) { return true; } else if (value instanceof String) { if (((String) value).trim().replaceAll("\\s", "").equals("")) { return true; } } else if (value instanceof Collection) { if (((Collection) value).isEmpty()) { return true; } } else if (value.getClass().isArray()) { if (Array.getLength(value) == 0) { return true; } } else if (value instanceof Map) { if (((Map) value).isEmpty()) { return true; } } else { return false; } return false; }
用springboot开发代码模板
浙公网安备 33010602011771号