记性不好,写下来,以后还会慢慢补充
- 记录一些码农经常用到的小方法
- 一、java部分
- 1. 日期处理工具
- 2. 集合去重
- 2. 格式化小数位
- 3. list的使用
- 4. stream流的使用
提示:以下是本篇文章正文内容,下面案例可供参考
一、java部分 1. 日期处理工具public class DateUtils extends PropertyEditorSupport { public final static String DATE_PATTERN = "yyyy-MM-dd"; public final static String PATTERN = "yyyyMMdd"; public final static String DATE_TIME_PATTERN = "yyyy-MM-dd HH:mm:ss"; public static String getDate(String pattern) { return DateFormatUtils.format(new Date(), pattern); } public static String format(Date date) { return format(date, DATE_PATTERN); } public static String format(Date date, String pattern) { if (date != null) { SimpleDateFormat df = new SimpleDateFormat(pattern); return df.format(date); } return null; } public static Date stringToDate(String strDate, String pattern) { if (StringUtils.isBlank(strDate)) { return null; } DateTimeFormatter fmt = DateTimeFormat.forPattern(pattern); return fmt.parseLocalDateTime(strDate).toDate(); } public static Date[] getWeekStartAndEnd(int week) { DateTime dateTime = new DateTime(); LocalDate date = new LocalDate(dateTime.plusWeeks(week)); date = date.dayOfWeek().withMinimumValue(); Date beginDate = date.toDate(); Date endDate = date.plusDays(6).toDate(); return new Date[]{beginDate, endDate}; } //根据年份和当年的第几周,获取这一周的日期 public static String getDayOfWeek(int year, int weekindex) { SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); Calendar c = Calendar.getInstance(); c.setWeekDate(year, weekindex, 1); int dayOfWeek = c.get(Calendar.DAY_OF_WEEK) - 2; c.add(Calendar.DATE, -dayOfWeek); // 得到本周的第一天 String begin = sdf.format(c.getTime()); c.add(Calendar.DATE, 6); // 得到本周的最后一天 String end = sdf.format(c.getTime()); String range = begin + "," + end; return range; } public static int getWeekNumByTime(String start, String end){ SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); try { long from = df.parse(start).getTime(); long to = df.parse(end).getTime(); int num = (int) ((to-from)/(1000*3600*24*7)); return num; } catch (ParseException e) { e.printStackTrace(); } return 0; } public static Date addDateSeconds(Date date, int seconds) { DateTime dateTime = new DateTime(date); return dateTime.plusSeconds(seconds).toDate(); } public static Date addDateMinutes(Date date, int minutes) { DateTime dateTime = new DateTime(date); return dateTime.plusMinutes(minutes).toDate(); } public static Date addDateHours(Date date, int hours) { DateTime dateTime = new DateTime(date); return dateTime.plusHours(hours).toDate(); } public static Date addDateDays(Date date, int days) { DateTime dateTime = new DateTime(date); return dateTime.plusDays(days).toDate(); } public static Date addDateWeeks(Date date, int weeks) { DateTime dateTime = new DateTime(date); return dateTime.plusWeeks(weeks).toDate(); } public static Date addDateMonths(Date date, int months) { DateTime dateTime = new DateTime(date); return dateTime.plusMonths(months).toDate(); } public static Date addDateYears(Date date, int years) { DateTime dateTime = new DateTime(date); return dateTime.plusYears(years).toDate(); } public static String getBeforeByHourTime(int ihour) { String returnstr = ""; Calendar calendar = Calendar.getInstance(); calendar.set(Calendar.HOUR_OF_DAY, calendar.get(Calendar.HOUR_OF_DAY) - ihour); SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); returnstr = df.format(calendar.getTime()); return returnstr; } // 获取当前时间和hour小时前的时间 public static Map2. 集合去重dealTime(int hour) { Calendar calendar = Calendar.getInstance(); Calendar calendar2 = Calendar.getInstance(); SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); SimpleDateFormat simpleDateFormat2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); calendar2.add(Calendar.MINUTE, 1); //往前1分钟 String etime = simpleDateFormat2.format(calendar2.getTime()); int a = Calendar.HOUR_OF_DAY; calendar.set(Calendar.HOUR_OF_DAY, calendar.get((Calendar.HOUR_OF_DAY)) - hour); String stime = simpleDateFormat.format(calendar.getTime()); Map map = new HashMap (); map.put("stime", stime); map.put("etime", etime); return map; } public static List getDatesBetweenTwoDate(String beginDate, String endDate) throws ParseException, java.text.ParseException { SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); List lDate = new ArrayList (); Date date = null; try { date = new SimpleDateFormat("yyyy-MM-dd").parse(beginDate); } catch (java.text.ParseException e) { e.printStackTrace(); } String dateString = sdf.format(date); lDate.add(dateString); Calendar cal = Calendar.getInstance(); cal.setTime(sdf.parse(beginDate)); boolean bContinue = true; while (bContinue) { cal.add(Calendar.DAY_OF_MONTH, 1); if (sdf.parse(endDate).after(cal.getTime())) { lDate.add(sdf.format(cal.getTime())); } else { break; } } try { date = new SimpleDateFormat("yyyy-MM-dd").parse(beginDate); } catch (java.text.ParseException e) { e.printStackTrace(); } String entrs = sdf.format(date); if (!dateString.equals(entrs)) { lDate.add(entrs); } return lDate; } //返回两个时间段之间的所有月包含最后一个月 public static List getMonthBetweenTwoDateIncludeEndDate(String beginDate, String endDate) throws ParseException, java.text.ParseException { SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM"); List lDate = new ArrayList (); Date date = null; try { date = new SimpleDateFormat("yyyy-MM").parse(beginDate); } catch (java.text.ParseException e) { e.printStackTrace(); } String dateString = sdf.format(date); lDate.add(dateString); Calendar cal = Calendar.getInstance(); cal.setTime(sdf.parse(beginDate)); boolean bContinue = true; while (bContinue) { cal.add(Calendar.MONTH, 1); if (sdf.parse(endDate).after(cal.getTime())) { lDate.add(sdf.format(cal.getTime())); } else { break; } } try { date = new SimpleDateFormat("yyyy-MM").parse(endDate); } catch (java.text.ParseException e) { e.printStackTrace(); } String entrs = sdf.format(date); if (!dateString.equals(entrs)) { lDate.add(entrs); } return lDate; } //返回两个时间段之间的所有日期包含最后一天 public static List getDatesBetweenTwoDateIncludeEndDate(String beginDate, String endDate) throws ParseException, java.text.ParseException { SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); List lDate = new ArrayList (); Date date = null; try { date = new SimpleDateFormat("yyyy-MM-dd").parse(beginDate); } catch (java.text.ParseException e) { e.printStackTrace(); } String dateString = sdf.format(date); lDate.add(dateString); Calendar cal = Calendar.getInstance(); cal.setTime(sdf.parse(beginDate)); boolean bContinue = true; while (bContinue) { cal.add(Calendar.DAY_OF_MONTH, 1); if (sdf.parse(endDate).after(cal.getTime())) { lDate.add(sdf.format(cal.getTime())); } else { break; } } try { date = new SimpleDateFormat("yyyy-MM-dd").parse(endDate); } catch (java.text.ParseException e) { e.printStackTrace(); } String entrs = sdf.format(date); if (!dateString.equals(entrs)) { lDate.add(entrs); } return lDate; } public static List getHoursBetweenTwoDate(String beginDate, String endDate) throws ParseException, java.text.ParseException { SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH"); List lDate = new ArrayList (); Date date = null; try { date = new SimpleDateFormat("yyyy-MM-dd HH:mm").parse(beginDate); } catch (java.text.ParseException e) { e.printStackTrace(); } String dateString = sdf.format(date); lDate.add(dateString); Calendar cal = Calendar.getInstance(); cal.setTime(sdf.parse(beginDate)); boolean bContinue = true; while (bContinue) { cal.add(Calendar.HOUR, 1); if (sdf.parse(endDate).after(cal.getTime())) { lDate.add(sdf.format(cal.getTime())); } else { break; } } try { date = new SimpleDateFormat("yyyy-MM-dd HH:mm").parse(endDate); } catch (java.text.ParseException e) { e.printStackTrace(); } String endstr = sdf.format(date); lDate.add(endstr); return lDate; } public static List getMinuteBetweenTwoDate(String beginDate, String endDate) throws ParseException, java.text.ParseException { SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm"); List lDate = new ArrayList (); lDate.add(beginDate);// 把开始时间加入集合 Calendar cal = Calendar.getInstance(); // 使用给定的 Date 设置此 Calendar 的时间 cal.setTime(sdf.parse(beginDate)); boolean bContinue = true; while (bContinue) { // 根据日历的规则,为给定的日历字段添加或减去指定的时间量 cal.add(Calendar.MINUTE, 1); // 测试此日期是否在指定日期之后 if (sdf.parse(endDate).after(cal.getTime())) { lDate.add(sdf.format(cal.getTime())); } else { break; } } lDate.add(endDate);// 把结束时间加入集合 return lDate; } public static List getMinuteBetweenTwoDate(String beginDate, String endDate,Integer min) throws ParseException, java.text.ParseException { SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm"); List lDate = new ArrayList (); // 判定beginData 是否是5的倍数 Date dt = sdf.parse(beginDate); int minute = dt.getMinutes(); minute = minute % min; beginDate = DateUtils.formatDate(DateUtils.getRightDate(dt, -minute+min),"yyyy-MM-dd HH:mm"); lDate.add(beginDate);// 把开始时间加入集合 Calendar cal = Calendar.getInstance(); // 使用给定的 Date 设置此 Calendar 的时间 cal.setTime(sdf.parse(beginDate)); boolean bContinue = true; while (bContinue) { // 根据日历的规则,为给定的日历字段添加或减去指定的时间量 cal.add(Calendar.MINUTE, min); // 测试此日期是否在指定日期之后 if (sdf.parse(endDate).after(cal.getTime())) { lDate.add(sdf.format(cal.getTime())); } else { break; } } lDate.add(endDate);// 把结束时间加入集合 return lDate; } public static List getMinutes(String day) throws ParseException, java.text.ParseException { List minuteList = getMinuteBetweenTwoDate(day + " 00:00", day + " 23:59"); return minuteList; } public static String getSecond(int i) { long l = Math.round(Math.random() * i); if (l < 10) { return "0" + l; } else { if (l != i) { return "" + l; } else { return l - 1 + ""; } } } public static String getRandom(int i) { double d = Math.random() * i; return String.format("%.2f", d); } //获取两个时间之间的分钟数 public static int getMinNums(String stime, String etime) { SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); Date now = null; Date date = null; try { now = df.parse(etime); date = df.parse(stime); } catch (ParseException e) { e.printStackTrace(); } long l = now.getTime() - date.getTime(); int day = (int) l / (24 * 60 * 60 * 1000); int hour = (int) (l / (60 * 60 * 1000)); int min = (int) ((l / (60 * 1000))); int s = (int) (l / 1000); // System.out.println(""+day+"天"+hour+"小时"+min+"分"+s+"秒"); return min; } //获取两个时间之间的小时数 public static int getHourNums(String stime, String etime) { SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); Date now = null; Date date = null; try { now = df.parse(etime); date = df.parse(stime); } catch (ParseException e) { e.printStackTrace(); } long l = now.getTime() - date.getTime(); int day = (int) l / (24 * 60 * 60 * 1000); int hour = (int) (l / (60 * 60 * 1000)); int min = (int) ((l / (60 * 1000))); int s = (int) (l / 1000); // System.out.println(""+day+"天"+hour+"小时"+min+"分"+s+"秒"); return hour; } //获取两个时间之间的天数 public static int getDayNums(String stime, String etime) { SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); Date now = null; Date date = null; try { now = df.parse(etime); date = df.parse(stime); } catch (ParseException e) { e.printStackTrace(); } long l = now.getTime() - date.getTime(); int day = (int) l / (24 * 60 * 60 * 1000); int hour = (int) (l / (60 * 60 * 1000)); int min = (int) ((l / (60 * 1000))); int s = (int) (l / 1000); // System.out.println(""+day+"天"+hour+"小时"+min+"分"+s+"秒"); return day; } public static List getDatesBetweenT(String beginDate, String endDate, String simpleFormat) throws ParseException, java.text.ParseException { SimpleDateFormat sdf = new SimpleDateFormat(simpleFormat); List lDate = new ArrayList (); Date date = null; try { date = new SimpleDateFormat(simpleFormat).parse(beginDate); } catch (java.text.ParseException e) { e.printStackTrace(); } String dateString = sdf.format(date); lDate.add(dateString); Calendar cal = Calendar.getInstance(); cal.setTime(sdf.parse(beginDate)); boolean bContinue = true; while (bContinue) { cal.add(Calendar.DAY_OF_MONTH, 1); if (sdf.parse(endDate).after(cal.getTime())) { lDate.add(sdf.format(cal.getTime())); } else { break; } } try { date = new SimpleDateFormat(simpleFormat).parse(beginDate); } catch (java.text.ParseException e) { e.printStackTrace(); } String entrs = sdf.format(date); if (!dateString.equals(entrs)) { lDate.add(entrs); } return lDate; } public static List getMonthBetween(String minDate, String maxDate) throws ParseException { ArrayList result = new ArrayList (); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM"); Calendar min = Calendar.getInstance(); Calendar max = Calendar.getInstance(); min.setTime(sdf.parse(minDate)); min.set(min.get(Calendar.YEAR), min.get(Calendar.MONTH), 1); max.setTime(sdf.parse(maxDate)); max.set(max.get(Calendar.YEAR), max.get(Calendar.MONTH), 2); Calendar curr = min; while (curr.before(max)) { result.add(sdf.format(curr.getTime())); curr.add(Calendar.MONTH, 1); } return result; } public static int daysOfTwo_2(String stime, String etime) throws ParseException { SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); Date fDate = sdf.parse(stime); Date oDate = sdf.parse(etime); Long a = ((oDate.getTime() - fDate.getTime()) / (1000 * 3600 * 24)); int days = a.intValue(); return days; } public static String getNowDate() { SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); return df.format(new Date()); } public static int getDaysOfMonth(Date date) { Calendar calendar = Calendar.getInstance(); calendar.setTime(date); return calendar.getActualMaximum(Calendar.DAY_OF_MONTH); } public static Date getCurrYearFirst(int year) { Calendar calendar = Calendar.getInstance(); calendar.clear(); calendar.set(Calendar.YEAR, year); Date currYearFirst = calendar.getTime(); return currYearFirst; } public static Date getCurrYearLast(int year) { Calendar calendar = Calendar.getInstance(); calendar.clear(); calendar.set(Calendar.YEAR, year); calendar.roll(Calendar.DAY_OF_YEAR, -1); Date currYearLast = calendar.getTime(); return currYearLast; } public static int getRightMinute(Date date) { Calendar c = Calendar.getInstance(); c.setTime(date); return c.get(Calendar.MINUTE); } public static Date getRightDate(Date date, int minute) { Calendar c = Calendar.getInstance(); c.setTime(date); c.add(Calendar.MINUTE, minute); return c.getTime(); } public static String getYear() { return formatDate(new Date(), "yyyy"); } public static String formatDate(Date date, Object... pattern) { String formatDate = null; if (pattern != null && pattern.length > 0) { formatDate = DateFormatUtils.format(date, pattern[0].toString()); } else { formatDate = DateFormatUtils.format(date, "yyyy-MM-dd"); } return formatDate; } public static String getMonth() { return formatDate(new Date(), "MM"); } public static String getDay() { return formatDate(new Date(), "dd"); } public static Map getYearToMonthStartEnd(String dateStr){ SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); Calendar c = Calendar.getInstance(); Calendar c1 = Calendar.getInstance(); try { c.setTime(sdf.parse(dateStr)); c.add(Calendar.MONTH, 0); //设置为1号,当前日期既为本月第一天 c.set(Calendar.DAY_OF_MONTH, 1); c1.setTime(sdf.parse(dateStr)); c1.set(Calendar.DAY_OF_MONTH, c.getActualMaximum(Calendar.DAY_OF_MONTH)); c1.set(Calendar.HOUR_OF_DAY, 23); c1.set(Calendar.MINUTE, 59); c1.set(Calendar.SECOND, 59); } catch (ParseException e) { e.printStackTrace(); } Map map = new HashMap<>(); map.put("stime",DateUtils.formatDate(c.getTime(),"yyyy-MM-dd HH:mm:ss")); map.put("etime",DateUtils.formatDate(c1.getTime(),"yyyy-MM-dd HH:mm:ss")); return map; } }
//方法1 public List2. 格式化小数位
public static String formatNum(Float value,Integer n) { NumberFormat nf = NumberFormat.getNumberInstance(); nf.setMaximumFractionDigits(n); nf.setRoundingMode(RoundingMode.HALF_UP); nf.setGroupingUsed(false); String num = nf.format(value); return num; } public static String formatNum(Double value,Integer n) { NumberFormat nf = NumberFormat.getNumberInstance(); nf.setMaximumFractionDigits(n); nf.setRoundingMode(RoundingMode.HALF_UP); nf.setGroupingUsed(false); String num = nf.format(value); return num; }3. list的使用
//根据条件出去空值 list.removeIf(p->(p.getPointCode() == null)); //List是你需要换位置的List。 i是你当前需要换位置的元素的索引 j是你需要将这个元素换到哪个索引 Collections.swap(List,i,j) //根据amount降序排序 Collections.sort(returnList, Comparator.comparing(ExportExhaustDataPO::getAmount).reversed()); //给List4. stream流的使用排序 Collections.sort(list,new Comparator >() { //降序排序 public int compare(Map o1, Map o2) { Date s = (Date) o1.get("monitorTime");//由于从map里面取出来的值为Object类型,无法直接转换为Integer类型,需要转换为double Date d = (Date) o2.get("monitorTime"); //使用Entry类中的值来比较大小 return s.compareTo(d);//s1在前面是升序,s1在后面是降序 // //使用Entry类中的键来比较大小 // return o2.getKey().compareTo(o1.getKey()); } }); //降序 monitorMapEntityList.sort((a, b) -> Float.compare(b.getMaxValue(), a.getMaxValue()));
//对集合按照单个属性分组 Map> detailsMap01 = dtos1.stream() .collect(Collectors.groupingBy(EntryDeliveryDetailywk::getskuId)); //对集合按照多个属性分组 Map > detailmap = details.stream() .collect(Collectors.groupingBy(d -> fetchGroupKey(d) )); private String fetchGroupKey(EntryDeliveryDetailywk detail){ return detail.getSkuId().toString() + detail.getItemsName() + detail.getWarehouseId().toString() + detail.getSupplierId().toString(); } //stream流去重用(可以根据属性去重 list = list.stream().filter(distinctByKey(b -> b.getName())) .forEach(b -> System.out.println(b.getName()+ "," + b.getPrice())); private static Predicate distinctByKey(Function super T, ?> keyExtractor) { Map
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)