java常用方法

持续更新中…

String字符串强转long数组

String str = "1,2,3,4";
Long [] list = (Long [])(ConvertUtils.convert(str.split(","), Long.class));

//  stream流的方式
Arrays.stream(libraryId.split(",")).map(Long::valueOf).collect(Collectors.toList());

去掉转义符

String s1="{\"MsgId\":1,\"TotalCount\":10,\"FilterCount\":8,\"SentCount\":7,\"ErrorCount\":1}";
System.out.println(StringEscapeUtils.unescapeJava(s1));

集合转数组

List<String> list = Arrays.asList("QC1907005", "QC1906139");

四舍五入保留小数

DecimalFormat decimalFormat=new DecimalFormat("#.##");
// 效果相同  DecimalFormat decimalFormat=new DecimalFormat("0.00");
//保留2位小数,.后面的#代表小数点后面的位数,保留3位小数就是#.###
System.out.println(decimalFormat.format(3.065f));//输出3.07
System.out.println(decimalFormat.format(3.065));//double类型,输出3.06
Math.round(3.65);  // 输出4

springboot取配置文件数据

@Autowired
    private Environment environment;
    environment.getProperty("名称")

springboot取配置文件数据

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

打开文件

import java.awt.*;

public class OpenFile {
    public static void main(String[] args) throws IOException {
        File file = new File("D:\\123");
        Desktop.getDesktop().open(file );
    }
}

实体类转map

// 第一种,这种会将对象的所有字段全部转换,string = null, int = 0
import org.springframework.cglib.beans.BeanMap;
BeanMap.create(entityObj); 

// 第二种,只保留有数据的键值对
JSON.parseObject(JSON.toJSONString(deviceDto), Map.class)

获取文件后缀名

// 参考cn.hutool.core.io;可以是文件路径和网络地址。通过最后一个"."截取
FileUtil.extName("123.jpg")
// 源码
public static String extName(String fileName) {
        if (fileName == null) {
            return null;
        } else {
            int index = fileName.lastIndexOf(".");
            if (index == -1) {
                return "";
            } else {
                String ext = fileName.substring(index + 1);
                return StrUtil.containsAny(ext, new char[]{'/', '\\'}) ? "" : ext;
            }
        }
    }

时间字符串映射

@DateTimeFormat(pattern = "yyyy-MM-dd'T'HH:mm:ss") //入参
@JsonFormat(timezone = "GMT+8", pattern = "yyyy-MM-dd'T'HH:mm:ss") //出参
private Date enDate;


@JsonDeserialize(using = LocalDateTimeDeserializer.class)
@JsonSerialize(using = LocalDateTimeSerializer.class)
@DateTimeFormat(pattern="yyyy-MM-dd'T'HH:mm:ss")
private LocalDateTime stDate;
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值