java short-code-sheet

2019-02-28  本文已影响0人  泠泉

get request


HttpServletRequest request = 
((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();

Map use Stream


Map<String, String> x;
Map<String, Integer> y =
    x.entrySet().stream()
        .collect(Collectors.toMap(
            e -> e.getKey(),
            e -> Integer.parseInt(e.getValue())
        ));

JsonFormat少一天


@JsonFormat(pattern = "yyyy-MM-dd", timezone = "GMT+8") //定义时区

注解报错


@SuppressWarnings("SpringJavaAutowiringInspection")

Excel下载


@PostMapping(value="exportxxxDetails",  consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE,
            produces = {MediaType.APPLICATION_ATOM_XML_VALUE, MediaType.APPLICATION_JSON_VALUE})
@ResponseBody
public ResponseEntity<Resource> exportxxxDetails(String classIds,String conditionStr) throws Exception{

    final String prefix = "xxx详细数据";
    final String suffix = ".xlsx";
    final String time = String.valueOf(new Date().getTime());
    String rawFilename = prefix+time+suffix;
    String filename = new String(rawFilename.getBytes("gbk"), "iso8859-1");
    Resource file = ExcelUtils.exportExcel(filename,colMap,resultJSONArray);
    return ResponseEntity
            .ok()
            .header(HttpHeaders.CONTENT_TYPE,"octets/stream")
            .header(HttpHeaders.ACCEPT_CHARSET, "utf-8")
            .header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\""+filename+"\"")
            .body(file);
}

filename


private String filename(String rawFilename) {
    try {
        return new String(rawFilename.getBytes("gbk"), "iso8859-1");
    } catch (UnsupportedEncodingException e) {
        return rawFilename;
    }
}

stream max()


int maxRowNum = finances.values().stream().map(List::size)
                    .collect(Collectors.toList())
                    .stream().max(Integer::compareTo).get();

BasicDBObject ref


 BasicDBObject query = new BasicDBObject().append("level", level).append("round", String.valueOf(round));
BasicDBObject projection = new BasicDBObject().append("_id", 0).append("mode", 0);
xxxCollection.find(query,projection).toArray()

stream boxed()


List<String> collect = IntStream.rangeClosed(1, 10)
        .boxed()
        .map(i -> "item".concat(String.valueOf(i)))
        .collect(Collectors.toList());
collect.forEach(System.out::println);

mongo-java-driver ref


SimpleDateFormat fmt = new SimpleDateFormat("dd/MM/yy");
Date first = fmt.parse("01/01/2014");
Date second = fmt.parse("01/01/2015");
Document doc = new Document("startDate", new Document("$gt", first).append("$lt", second));
System.out.println(doc.toJson(JsonWriterSettings.builder().outputMode(SHELL).build()));

String.format ref


String.format("%.2f%%") // 保留2位小数,接%号
上一篇 下一篇

猜你喜欢

热点阅读