//导出
public void excel(List<Long> ids, HttpServletResponse response) {
List<StockPageVo> excel = stockMapper.excel(ids);
try {
ExportParams params = new ExportParams();
params.setSheetName("历史入库导出");//设置sheet名
Workbook workbook = ExcelExportUtil.exportExcel(params, StockPageVo.class, excel);
//返回头设置下载,并设置文件名,返回
DownloadUtils.setExportExcelFormat(response, workbook, "历史入库");
} catch (Exception e) {
e.printStackTrace();
}
}
public static void setExportExcelFormat(HttpServletResponse response, Workbook workbook, String fileName) throws Exception {
response.reset();
response.setContentType("application/vnd.ms-excel");//下载
fileName = fileName + new SimpleDateFormat("yyyyMMddHHmmss").format(new Date());
response.setHeader("Content-disposition", "attachment; filename=" + new String(fileName.getBytes(StandardCharsets.UTF_8), StandardCharsets.ISO_8859_1) + ".xls");
try (ServletOutputStream outStream = response.getOutputStream()) {
workbook.write(outStream);
}
} //导入
public void test123(MultipartFile file) throws Exception {
//创建导入对象
ImportParams params = new ImportParams();
params.setTitleRows(1); //表格标题行数,默认0
params.setHeadRows(1); //表头行数,默认1 //获取导入数据
InputStream inputStream = file.getInputStream();
List<User> users = ExcelImportUtil.importExcel(inputStream,User.class, params);
for (User user : users) {
System.out.println(user.toString());
}
} //图片上传
public static String upload(MultipartFile file) {
if (file == null) throw new BusinessException("图片为空");
String extension = FilenameUtils.getExtension(file.getOriginalFilename());
String uuid = Utility.generateShortUuid() + "." + extension;
try {
//图片上传的路径
String path = IntegerFaceConfig.EXCEP_Path;
file.transferTo(new File(path + uuid));
return path + uuid;
} catch (IOException e) {
e.printStackTrace();
}
return throw new BusinessException("图片上传失败");
} //图片的删除
File file123 = new File(ImgUser);//图片的路径
file123.delete(); //校验id和端口
public static boolean checkIp(String ipAddress) {
String ip = "([1-9]|[1-9]\\d|1\\d{2}|2[0-4]\\d|25[0-5])(\\.(\\d|[1-9]\\d|1\\d{2}|2[0-4]\\d|25[0-5])){3}";
Pattern pattern = Pattern.compile(ip);
Matcher matcher = pattern.matcher(ipAddress);
return matcher.matches();
} public static boolean checkPort(String port) {
//端口号验证 1 ~ 65535
String regex = "^([1-9]|[1-9]\\d{1,3}|[1-6][0-5][0-5][0-3][0-5])$";
return Pattern.matches(regex, port);
} //校验电话号码
public static boolean isMobileNo(String mobiles) {
// ^ 匹配输入字符串开始的位置
// \d 匹配一个或多个数字,其中 \ 要转义,所以是 \\d
// $ 匹配输入字符串结尾的位置
String regExp = "^((13[0-9])|(14[5,7,9])|(15[0-3,5-9])|(166)|(17[3,5,6,7,8])" +
"|(18[0-9])|(19[8,9]))\\d{8}$";
Pattern p = Pattern.compile(regExp);
Matcher m = p.matcher(mobiles);
return m.matches();
} //对参数格式的校验
public static Boolean isNull(String str) {
if (str==null) return false;
boolean blank = StringUtils.isBlank(str);
if (!blank) {
return !str.equals("''");
} else {
return false;
}
} //获取当前时间
public static String getTimestamps() {
Date date = new Date();
DateTime dateTime = new DateTime();
String endOfDay = getEndOfDay(date);
/*SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd kk:mm:ss");
String formats = sdf.format(endOfDay);*/
return endOfDay;
} public static String getEndOfDay(Date date) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
/*calendar.set(Calendar.HOUR_OF_DAY, 23);
calendar.set(Calendar.MINUTE, 59);
calendar.set(Calendar.SECOND, 59);
calendar.set(Calendar.MILLISECOND, 999);*/
return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(calendar.getTime());
} //获取当前年月日
public static String getDate() {
Date date = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
return sdf.format(date);
} //获取本年
public static String getYear() {
Calendar c = Calendar.getInstance();
String getCurrentTime = new SimpleDateFormat("yyyy-MM-dd").format(c.getTime());
return String.valueOf(c.get(Calendar.YEAR));
}
//获取当月的第一天和最后一天
public static Map<String, String> getFirLast() {
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
//获取当前月第一天:
Calendar c = Calendar.getInstance();
c.add(Calendar.MONTH, 0);
c.set(Calendar.DAY_OF_MONTH, 1);//设置为1号,当前日期既为本月第一天
String first = format.format(c.getTime());
System.out.println("===============first:" + first);
//获取当前月最后一天
Calendar ca = Calendar.getInstance();
ca.set(Calendar.DAY_OF_MONTH, ca.getActualMaximum(Calendar.DAY_OF_MONTH));
String last = format.format(ca.getTime());
System.out.println("===============last:" + last);
Map<String, String> map = new HashMap<>();
map.put("first", first);
map.put("last", last);
return map;
}
//计算两个日期相差的月份
public static BigDecimal getMonth(Date start, Date end) {
//前大 后小 计算前到后相差的月份
return BigDecimal.valueOf(getMonth1(start, end));
} static int getMonth1(Date start, Date end) {
if (start.after(end)) {
Date t = start;
start = end;
end = t;
}
Calendar startCalendar = Calendar.getInstance();
startCalendar.setTime(start);
Calendar endCalendar = Calendar.getInstance();
endCalendar.setTime(end);
Calendar temp = Calendar.getInstance();
temp.setTime(end);
temp.add(Calendar.DATE, 1);
int year = endCalendar.get(Calendar.YEAR) - startCalendar.get(Calendar.YEAR);
int month = endCalendar.get(Calendar.MONTH) - startCalendar.get(Calendar.MONTH);
if ((startCalendar.get(Calendar.DATE) == 1) && (temp.get(Calendar.DATE) == 1)) {
return year * 12 + month + 1;
} else if ((startCalendar.get(Calendar.DATE) != 1) && (temp.get(Calendar.DATE) == 1)) {
return year * 12 + month;
} else if ((startCalendar.get(Calendar.DATE) == 1) && (temp.get(Calendar.DATE) != 1)) {
return year * 12 + month;
} else {
return (year * 12 + month - 1) < 0 ? 0 : (year * 12 + month);
}
} //计算两个日期相差的天数
public static int differentDays(String date11, String date22) {
//SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); Date date1 = null;
Date date2 = null;
try {
date1 = sdf.parse(date11);
date2 = sdf.parse(date22);
} catch (ParseException e) {
e.printStackTrace();
}
Calendar cal1 = Calendar.getInstance();
cal1.setTime(date1); Calendar cal2 = Calendar.getInstance();
cal2.setTime(date2);
int day1 = cal1.get(Calendar.DAY_OF_YEAR);
int day2 = cal2.get(Calendar.DAY_OF_YEAR); int year1 = cal1.get(Calendar.YEAR);
int year2 = cal2.get(Calendar.YEAR);
if (year1 != year2) //同一年
{
int timeDistance = 0;
for (int i = year1; i < year2; i++) {
if (i % 4 == 0 && i % 100 != 0 || i % 400 == 0) //闰年
{
timeDistance += 366;
} else {
//不是闰年
timeDistance += 365;
}
} return timeDistance + (day2 - day1);
} else {
//不同年
//System.out.println("判断day2 - day1 : " + (day2 - day1));
return day2 - day1;
}
} //计算两个数占得百分比
public static String getPercent(int x, int y) {
double d1 = x * 1.0;
double d2 = y * 1.0;
NumberFormat percentInstance = NumberFormat.getPercentInstance();
// 设置保留几位小数,这里设置的是保留两位小数
percentInstance.setMinimumFractionDigits(2);
return percentInstance.format(d1 / d2);
} //根据日期获得是周几
public static String getCycle(String date) {
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
Date date1 = null;
try {
date1 = format.parse(date);
String[] weekDays = {"7", "1", "2", "3", "4", "5", "6"};
Calendar cal = Calendar.getInstance();
cal.setTime(date1);
int w = cal.get(Calendar.DAY_OF_WEEK) - 1;
if (w < 0)
w = 0;
//System.out.println(weekDays[w]);
return weekDays[w];
} catch (ParseException e) {
throw new BusinessException("错误信息");
}
} //对两个相同的集合进行操作
public static List<ChangeVo> mergeChange(List<ChangeVo> list) {
if (list.size() != 0) {
List<ChangeVo> result = list.stream()
.collect(Collectors.toMap(ChangeVo::getMonth, a -> a, (o1, o2) -> {
o1.setNumber(o1.getNumber() + o2.getNumber());
o1.setPrice(o1.getPrice().add(o2.getPrice()));
return o1;
})).values().stream().collect(Collectors.toList());
return result;
} else {
return new ArrayList<>();
}
}

最新文章

  1. GoLang之基础
  2. 解决linux下unzip中文有乱码的问题
  3. javascript 中的 delete
  4. 使用curl获取Location:重定向后url
  5. IOS项目删除Git
  6. oAuth协议学习
  7. uva 11246 - K-Multiple Free set(数论)
  8. 黑马程序员_Java集合框架
  9. 10,随机等概率的输出m个不重复的数
  10. swap与dd命令使用详解
  11. mysql数据库事务详细剖析
  12. C# Bootstrap table之 分页
  13. 爬虫---爬虫er与反爬虫er之间的斗争 转发
  14. Oarcle之用户管理 与 DCL
  15. 如何用core自动创建model,与数据库连接
  16. python创建数组的方法
  17. Event Recommendation Engine Challenge分步解析第三步
  18. python基础-----异常问题
  19. Java 字符串拼接5种方式性能比较
  20. mongo批量操作存在更新否则插入

热门文章

  1. OpenJudge1.5.17 菲波那契数列
  2. ubuntu20.4 sgx环境配置
  3. Windows平台摄像头或屏幕RTMP推送介绍:OBS VS SmartPublisher
  4. Job And Schedule (V8R6C3)
  5. [Python]-tqdm模块-给for循环加上进度条
  6. Nginx相关模块学习使用实践指南
  7. Elasticsearch:理解 mapping 中的 null_value
  8. 利用python对websocket进行并发压测
  9. 1_JavaWeb引言
  10. 搞透 IOC,Spring IOC 看这篇就够了!