zoukankan      html  css  js  c++  java
  • JAVA数据转换常用方法

    时间格式化与运算

    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
    Calendar calendar=sdf.getCalendar();
    calendar.setTime(sdf.parse(sdf.format(new Date())));
    calendar.add(Calendar.DATE,1);
    System.out.println(calendar.getTime());

    集合与数组的互换

    ArrayList<String> list=new ArrayList<String>();
    list.add("a");
    list.add("b");
    list.add("c");
    String[] strs1=list.toArray(new String[]{});
    String[] strs2=list.toArray(new String[list.size()]);
    List<String> list = new ArrayList<String>(Arrays.asList(str2));
    //list= Arrays.asList(str2); //错误写法 返回的是java.util.Arrays.ArrayList 没有add方法
    list.add("add");
    System.out.println(list);
    com.alibaba.fastjson与数组、字符串互换
    //字符串转JSONArray
    String result="["12312","123123122"]";
    JSONArray jsonArray=JSONArray.parseArray(result);
    System.out.println(jsonArray.toJSONString());
    //JSONArray 转 数组
    String [] strings=jsonArray.toArray(new String[jsonArray.size()]);
    
    // 数组 转 JSONObject
    JSONObject jsonObject=new JSONObject();
    String[] arr=new String[]{"123","456"};
    jsonObject.put("data",arr);
    System.out.println(jsonObject.toJSONString());
    
    // 数组 转 JSONArray
    JSONArray dn=new JSONArray();
    dn.addAll(Arrays.asList(arr));
    System.out.println(dn.toJSONString());

     

  • 相关阅读:
    苹果 01背包
    Robberies 01背包变形 hdoj
    01背包
    小希的迷宫--并查集
    德克萨斯长角牛 --最短路径
    1596 最短路径的变形
    hibernate重要知识点总结
    Apache与Tomcat整合的配置
    java串口通讯环境配置
    使用spring的aop对Struts2的Action拦截后出现依赖注入为空问题
  • 原文地址:https://www.cnblogs.com/wait/p/5871782.html
Copyright © 2011-2022 走看看