[关闭]
@songhanshi 2020-10-19T12:51:16.000000Z 字数 4369 阅读 556

Java小tips

Java小工具


1. J8 stream

Map转List>

  1. List<Map<String,String>> B
  2. = A.entrySet().stream().map(m->{
  3. Map<String,String> u = new Map<String,String>();
  4. u.setKey(m.getKey());
  5. u.setTitle(m.getValue());
  6. return u;
  7. }).collect(Collectors.toList());

Map>转Map

  1. Map<String, List<String>> parameters;
  2. Map<String, String[]> collect = parameters.entrySet().stream()
  3. .collect(Collectors.toMap(entry-> entry.getKey(),entry -> entry.getValue().toArray()));

null/nutNull

String

  1. str.isEmpty()

不为空

  1. StringUtils.isNotBlank(str)

List

  1. if(list == null || list.size() ==0 ){
  2.   //为空的情况
  3. }else{
  4.   //不为空的情况
  5. }
  1. if(list!=null && !list.isEmpty()){
  2.    //不为空的情况
  3. }else{
  4.    //为空的情况
  5. }

容器操作

1.List去重

数据类型

1. 判断是否为正数

  1. public static boolean isIntegerForDouble(double obj) {
  2. double eps = 1e-10; // 精度范围
  3. return obj-Math.floor(obj) < eps;
  4. }

2. 判断精度

  1. private static boolean doubleAccuracy(String str, int doubleSize) {
  2. try {
  3. double num = Double.valueOf(str);//把字符串强制转换为数字
  4. if (str.trim().contains(".")) {
  5. int currentPlace = str.trim().length() - str.trim().indexOf(".") - 1;
  6. return currentPlace < doubleSize;
  7. }
  8. return true;
  9. } catch (Exception e) {
  10. return false;//如果抛出异常,返回False
  11. }
  12. }

参考

时间

获取最近30分钟

  1. // 获取前30分钟日期
  2. Calendar calendar = Calendar.getInstance();
  3. Date late = calendar.getTime();
  4. calendar.add(Calendar.MINUTE, -30);
  5. Date early = calendar.getTime();
  6. query.setStartTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(early));
  7. query.setEndTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(late));

String转Date

  1. String end = "2020-06-27";
  2. // SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
  3. SimpleDateFormat simpleDateFormat = new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss " );
  4. Date endTime = simpleDateFormat.parse(end);

注:例如MM是月份,mm是分;HH是24小时制,而hh是12小时制。

Date转String

  1. SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  2. String str = format.format(date);

Date比较

  1. Date endDate = DateUtil.parseDate(endStr);
  2. Date startDate = DateUtil.parseDate(startStr);
  3. if (DateUtil.compare(endDate, startDate) < 0) {
  4. return stringBuilder.append("结束时间不能早于开始时间").toString();
  5. }

改变Date时分秒

  1. Date now = new Date();
  2. Calendar calendar = Calendar.getInstance();
  3. calendar.setTime(now);
  4. calendar.set(Calendar.HOUR_OF_DAY, 23);
  5. calendar.set(Calendar.MINUTE, 59);
  6. calendar.set(Calendar.SECOND, 59);
  7. Date now2= calendar.getTime();
  8. System.out.println(now2);

文件

Hutool

https://www.hutool.cn/

File->MultipartFile

  1. File file = new File("F:/hejing/InstrumentJsonData.txt");
  2. if(file.isFile() && file.exists()) {
  3. ...
  4. }
  5. public static void main(String[] args) throws Exception {
  6. // File file = new File("F:/hejing/InstrumentJsonData.txt");
  7. String strUrl = "F:/hejing/InstrumentJsonData.txt";
  8. File file = new File(strUrl);
  9. InputStream inputStream = new FileInputStream(file);
  10. MultipartFile multipartFile = new MockMultipartFile(file.getName(), inputStream);
  11. log.info("file转multipartFile成功. {}",multipartFile);
  12. }
    XSSFWorkbook xssfWorkbook = new XSSFWorkbook(inputStream);
    XSSFSheet xssfSheet = xssfWorkbook.getSheetAt(0);
    int rowSize = xssfSheet.getLastRowNum();

MultipartFile->File

  1. CommonsMultipartFile cmf = (CommonsMultipartFile)file;
  2. DiskFileItem dfi = (DiskFileItem)cmf.getFileItem();
  3. File f = dfi.getStoreLocation();

MultipartFile->inputStream

  1. CommonsMultipartFile commonsMultipartFile = (CommonsMultipartFile)file;
  2. DiskFileItem fileItem = (DiskFileItem)commonsMultipartFile.getFileItem();
  3. InputStream fileInputStream = fileItem.getInputStream();

Excel

流程

  1. 配置路径 yml

    1. dataplatform:
    2. filePath: D:\file\
  2. 创建config类

    1. @Configuration(value = "filePathConfig")
    2. @ConfigurationProperties(prefix = "dataplatform")
    3. @Data
    4. public class FilePathConfig {
    5. public static final String SEPARATOR;
    6. static {
    7. SEPARATOR = FileUtil.isWindows() ? "\\" : "/";
    8. }
    9. String filePath;
    10. }
  3. 设置标记
    设置颜色:https://blog.csdn.net/yuefenghui/article/details/67673994
    颜色: https://vimsky.com/zh-tw/examples/detail/java-method-org.apache.poi.ss.usermodel.Font.setColor.html

前后端

前端:array->后端接收

  1. ["1","2","3"]
  2. public String save(@Param("switch")boolean switch1,String slider,String input,String redio,String checkbox){
  3. JSONArray obj = JSON.parseArray(checkbox);
  4. sout(JSON.parseArray(checkbox).size());
  5. fori(){
  6. obj.get(i);
  7. }
  8. }

curl请求form、json

  1. application/x-www-form-urlencoded
  1. curl -X POST 'http://localhost:8080/formPost' -'id=1&name=foo&mobile=13612345678'
  1. application/json
  1. curl -X POST -"Content-Type: application/json" 'http://localhost:8080/jsonPost' -'{"id":2,"name":"foo","mobile":"13656635451"}'

https://blog.csdn.net/weixin_34400525/article/details/86254617

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注