Java小知识点备忘录

1、Map和JSON的相互转化

Map map = new HashMap<>();

map.put("username","tom");

map.put("age","15");

 

String str = JSON.toJSONString(map);//map转Json

Map map1 = JSON.parseObject(str);//json转map

for(Map.Entry  map : Set>){

System.out.println(map.getKey+"  "+map.getValue());

}

 

Map process = (Map)JSON.parse(str); //json转map

for(String key : process.keySet()){

System.out.println(key+":"+process.get(key));

}

2、 时间日期转Date转Calendar(日历)

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

Date date = sdf.parse(timeString);

Calendar ca = Calendar.getInstance();

ca.setTime(date);

3、 int 与String互转

String str = “123”; 
int a = Integer.parseInt(str);

int b = 1; 
String str = String.valueOf(b);

4、long与String互转

String str = "1111";

long l = Long.parseLong(str);

long l =11111;

String str = String.valueOf(l);

你可能感兴趣的:(Java)