1、将JSON字符串转换成List集合

List<Cart> cartList_cookie=JSON.parseArray(cartListString,Cart.class);

2、将map集合转成JSON字符串

JSON.toJSONString(map)

3、将list集合转成JSON字符串

JSON.toJSONString(itemList);

4、将对象(或Map)转成JSON字符串

String teachplanString = JSON.toJSONString(teachplanNode); 

5、将JSON字符串转为map集合

Map map=JSON.parseObject(metadata,Map.class);

6、将JSON字符串转成对象

AuthToken authToken=JSON.parseObject(userTokenString,AuthToken.class); 

7、将JSON对象转换成HashMap

HashMap userMap = (HashMap) userResult.getData();

8、用objectMapper工具类将对象转成JSON字符串

private static ObjectMapper objectMapper=new ObjectMapper();
String resultString = objectMapper.writeValueAsString(result);

9、将数组转成list集合

Long[] goodsIds=(Long[])ObjectMessage.getObject();
Arrays.asList(goodsIds)

10、将字符串转成Long类型

String text =textMessage.getText();
Long.parseLong(text)

11、将文件转成字节数组

byte[] bytes= multipart.getBytes();

12、将字符串转成字节数组

byte[] bytes="abc".getBytes();

13、将字符串转成字符数组

char[] chars="abc".toCharArray();

14、将字符数组转成字符串

char[] charArray={979899}
String str=new String(byteArray)

15、将字节数组转成字符串

byte[] byteArray={'A''B''C'}
String str=new String(charArray)
// 字节数组转成字符串是还可以指定字符名charsetname
String str=new String(charArray,"utf-8")

16、将字符数组转成字符串

char[] chars={'a','b','c','d','e',};
String s = new String(chars);
// 或将字符数组的一部分转成字符串
String s = new String(chars, 0, len);

17、将字符串按照给定的regex(规则)拆分成字符串数组

String[] source_field_array = source_field.split(",");

18、将集合转成数组

Predicate[] predicates = list.toArray(predicateArray);

19、将字符串数字转成double

Double.parseDouble(String s)
Double completeDty1 = Double.valueOf(StringUtils.isEmpty(productFeedERPDetailInfo.getCompleteQty()) ? "0" : productFeedERPDetailInfo.getCompleteQty());

20、将double类型转换成字符串

output.setCompleteQty(String.valueOf(BigDecimalUtil.add(completeDty1, completeDty2)));

21、将字符串类型的日期转换成Date类型的日期

Date date=new Date(String)

推荐以下方式转换

import cn.hutool.core.date.DateUtil;
Date orderTime = DateUtil.parseDateTime(orderTimeStr);

例子

String orderTimeStr = "2023-05-18 10:10:10";
Date orderTime = DateUtil.parseDateTime(orderTimeStr);

结果:2023-05-18 10:10:10

日期类型转为字符串

public static void main(String[] args) {
        System.out.println(orderTime);
        String today = DateUtil.today();
        System.out.println(today);
        String now = DateUtil.now();
        System.out.println(now);
    }

结果:

2023-05-18
2023-05-18 12:08:17

22、将字符串类型转成Integer类型

Integer.parseInt(String)

23、将整型转换成字符串类型

String b=a.toString()

24、将Long类型转换为int类型

int id = medProduct.getId().intValue();

25、将set集合转换成字符串数组

String[] strsTrue = hashSet.toArray(new String[hashSet.size()]);

26、将字符串类型的日期转换成Date类型的日期

// 日期字符串转LocalDateTime
        String today = "2022-08-24 18:00:10";
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        // 转为自定义格式
        LocalDateTime localDateTime = LocalDateTime.parse(today, dateTimeFormatter);
        String format = DateTimeFormatter.ofPattern("yyyyMMddHHmmss").format(localDateTime);
        System.out.println("localDateTime:" + localDateTime);
        System.out.println("自定义格式:" + format);

下面是以前的做法:

output.setProductionDate(DateUtil.stringToDate(productFeedERPDetailInfo.getProductionDate(), "yyyy/MM/dd"));

即:

SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
Date planDeliverDate = simpleDateFormat.parse(planDeliverDate);

27、字符串数组转换成字符串集合

String[] arr = str.split(",");
List<String> list = Arrays.asList(arr);

28、将数组中的字符串用逗号连接起来

String user_permission_string  = StringUtils.join(user_permission.toArray(), ",");

29、将用逗号连接的字符串通过逗号断开然后放到字符串数组中

@Value("${xuecheng.media.source_field}")
private String media_source_field;
String[] media_source_field_array = media_source_field.split(",");

30、将字符串转为BigDecimal

String purchasePrice = jsonObject1.getString("purchasePrice");
BigDecimal purchasePrice1 = new BigDecimal(purchasePrice);

31、将List转换成JSONArray,即先转成Json字符串,在转成JSONArray

List<ExternalUser> externalUsers = externalUserDao.selectByExample(example);
JSONArray jsonArray = JSONArray.parseArray(JSON.toJSONString(externalUsers));

将JSONArray转成List

List<String> list = JSONObject.parseArray(JSON.toJSONString(jsonArray), String.class);

32、将Date类型转成Calendar

Calendar calendar = Calendar.getInstance();
calendar.setTime(extUserLog.getRequestTime());

33、将String[]转成List<Long>

String[] inDetailIdsString = inDetailIdString.split(",");
List<Long> inDetailIds = Arrays.stream(inDetailIdsString)
      .map(s -> Long.parseLong(s.trim())).collect(Collectors.toList());

34、将map转成JSONObject

com.alibaba.fastjson.JSONObject jsonInfo = new com.alibaba.fastjson.JSONObject(requestMap);

35、将List<String>转为List<Integer>

String[] strs = areaIds.split(",");
List<String> list1 = Arrays.asList(strs);
// 将List<String>转为List<Integer>
List<Integer> areaIdList = list1.stream().map(Integer::parseInt).collect(Collectors.toList());

36、将对象的List集合通过map方法转成字符串的List集合

List<Organization> organizations = organizationDao.selectTreeParentByNo(item.getOrgNo());
List<String> parentList = organizations.stream().map(Organization::getOrgNo).collect(Collectors.toList());

37、将字符串的List集合通过StringUtils的join方法转成逗号拼接的字符串

List<String> parentList = organizations.stream().map(Organization::getOrgNo).collect(Collectors.toList());
String join = StringUtils.join(parentList, ",");

不仅是字符串类型的List集合可以转成逗号拼接的字符串,Long类型的List集合也可以,如下所示:

List<Long> entityIdList = enterpriseList.stream().map(Enterprise::getEntityId).collect(Collectors.toList());
String entityIds = StringUtils.join(entityIdList, ",");

38、将输入流InputStream转成字节数组

InputStream body = httpInputMessage.getBody();
            byte[] data = FileUtil.streamToByteArray(body);

39、将字符串数组转为逗号分隔得字符串

import org.apache.commons.lang3.StringUtils;
String str = StringUtils.join(list, ",");

40、BigDecimal转成Integer

BigDecimal bigDecimal = new BigDecimal("123.45");
Integer integer = Integer.valueOf(bigDecimal.intValue());

结果为123

 

posted on 2020-09-14 11:49  周文豪  阅读(257)  评论(0编辑  收藏  举报