作为一名开发人员,从前端 接收到各种各样的数据需要转变成为我们所用的格式,比如前端如果传来一个date,那我们需要获取到之后进行一系列的操作,那接下来就分成几块来记录关于格式的转换。
一、时间的转换
/**
* 先回顾一下:
* Date内部是一个long类型的值,System.currentTimeMillis()
* Date是表示时刻,是绝对时间与年月日无关
* Calendar是表示年历,其有表示公历的子类GregorianCalendar
* DateFormat:格式化,可将日期与时间与字符串进行相互转化(常用SimpleDateFormat)
*yyyy表示四位的年;MM表示两位的月;dd表示两位的日 HH:mm:ss 时分秒 大写H为24小时,小写为12小时
*/
//将时间值转化为String
SimpleDateFormat date2StrFormat = new SimpleDateFormat("yyyy-MM-dd");
String date2Str = date2StrFormat.format(new Date());
System.out.println(new Date());//Thu Nov 01 14:35:57 CST 2018
System.out.println(date2Str);//2018-11-01
//将字符串转为Date和想要的格式
String dateStr = "2018-02-28 21:05:12";
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:ss:mm");
Date str2Date = dateFormat.parse(dateStr);
System.out.println(str2Date);//Wed Feb 28 21:12:05 CST 2018
//先将字符串转为Date类型,再将其格式化
SimpleDateFormat date22StrFormat = new SimpleDateFormat("yyyy年MM月dd日 HH时ss分mm秒");
System.out.println(date22StrFormat.format(str2Date));//2018年02月28日 21时05分12秒
//获取Date 将其转化为自己的格式
GregorianCalendar gregorianCalendar = new GregorianCalendar();
System.out.println(gregorianCalendar.getTime());//Thu Nov 01 14:35:57 CST 2018
SimpleDateFormat yMFormat = new SimpleDateFormat("yyyy/MM");
String yM = yMFormat.format(gregorianCalendar.getTime());
System.out.println(yM);//2018/11
//对Calendar进行根据Date或者毫秒值、指定字段等来设置时间 可用于一些定时任务
Calendar calendar = Calendar.getInstance();
System.out.println(calendar.getTime());//Thu Nov 01 14:35:57 CST 2018
calendar.add(Calendar.DAY_OF_MONTH,1);//加一天 需要用的时候看API太多了
System.out.println(calendar.getTime());//Fri Nov 02 14:35:57 CST 2018
calendar.set(Calendar.YEAR,2022);//设成指定月份
System.out.println(calendar.getTime());//Wed Nov 02 14:41:43 CST 2022
Calendar 的A .compareTo(B) A早于返回-1 ,晚于返回1,时间相同返回0
public int compareTo(Date anotherDate) {
long thisTime = getMillisOf(this);
long anotherTime = getMillisOf(anotherDate);
return (thisTime<anotherTime ? -1 : (thisTime==anotherTime ? 0 : 1));
}
二、各种基本类型之间转化
/**
* 比较常用的是在String和各个基本类型之间的转化
* String中有valueOf可以将任何东西为其所同
* 其他包装类用valueOf将
*/
String str = "";
System.out.println(str.isEmpty());
String s = String.valueOf(1);
//String --》包装类
Integer integer = Integer.valueOf("12");
System.out.println(integer.getClass());
//string类 --》 基本类
int int2 = Integer.parseInt("12");
// 其他类型-->String
String s1 = String.valueOf(1);
String s2 = Integer.toString(12);
String s3 = "" + 21;
三、集合之间转化
1、比如平常的map里面的Value是一个List
Map<Integer, List<XxxxBO>> exampleMap = new HashMap<>();
exampleMap.put(1, new ArrayList<XxxxBO>());
exampleMap.put(2, new ArrayList<XxxxBO>());
//将其values转为List
List<Integer> departmentIds = new ArrayList<>(exampleMap.keySet());
2、List转set
String[] strArray = {"aaa", "bbb", "ccc","aaa"};
List<String> strList = Arrays.asList(strArray);
System.out.println(strList);//[aaa, bbb, ccc, aaa]
//set 可以去重
Set strSet = new HashSet();
strSet.addAll(strList);
System.out.println(strSet);//[aaa, ccc, bbb]
//一般Collections的使用场景不多,需要Collection转数组 使用其toArray()方法即可
注:https://blog.csdn.net/LCF_lxf_ldy/article/details/83111579
3、对象数组转成List 使用数组的asList方法
Element[] array = {new Element(1),new Element(2),new Element(3)};
ArrayList<Element> arrayList = new ArrayList<Element>(Arrays.asList(array));
/**
ArrayList(Collection < ? extends E > c) : 构造一个list,用迭代器将指定collection中的元素按顺序放入其中。
这个构造方法的实现过程:
1. 将collection c的元素转换成一个数组
2. 将这个数组复制给ArrayList的成员变量数组“elementData”
3.使用Arrays.asList这种方法生成的list是不能使用list.add remove等方法的,因为Arrays.asList()方法返回的是一个和ArrayList同名的自定义内部类
**/
四、Json之间的转换
fastJson对于json格式字符串的解析主要用到了一下三个类:
JSON:fastJson的解析器,用于JSON格式字符串与JSON对象及javaBean之间的转换。
JSONObject:fastJson提供的json对象。
JSONArray:fastJson提供json数组对象。
/**
* 我们通过HttpServletRequest在前端接收到的json参数总是需要通过后台的实体类来接收
* 一般使用的都是阿里的fastjson来转换,当然还有其他方式,但是本案例只用了fastjson
* 直接使用parseObject实现json类--》实体类
*/
BufferedReader br = new BufferedReader(new InputStreamReader(req.getInputStream(), CharEncoding.UTF_8));
String line;
StringBuilder reqBody = new StringBuilder();
while ((line = br.readLine()) != null) {
reqBody.append(line);
}
//VO是接收前端传过来的数据的实体类
XxxVO xxxVO = JSONObject.parseObject(reqBody, XxxVO.class);
//将需要与前端交互的数据以json数据格式传给前端
String jsonString = JSONObject.toJSONString(XxxVO.class);
String json = " {" +
" \"ids\": [133]," +
" \"id\": 12" +
" }";
JSONObject jsonObject = JSON.parseObject(json);
String ids = jsonObject.getString("ids");
int id = jsonObject.getInteger("id");
System.out.println(ids+ids.getClass());
System.out.println(id);
//复杂格式json字符串 根据不同的key取值
String jsonStr = "{\"teacherName\":\"crystall\",\"teacherAge\":27,\"course\":{\"courseName\":\"english\",\"code\":1270},\"students\":[{\"studentName\":\"lily\",\"studentAge\":12},{\"studentName\":\"lucy\",\"studentAge\":15}]}";
JSONObject jsonObject1 = JSON.parseObject(jsonStr);
//JSONObject jsonObject1 = JSONObject.parseObject(COMPLEX_JSON_STR);//因为JSONObject继承了JSON,所以这样也是可以的
String teacherName = jsonObject1.getString("teacherName");
Integer teacherAge = jsonObject1.getInteger("teacherAge");
JSONObject course = jsonObject1.getJSONObject("course");
JSONArray students = jsonObject1.getJSONArray("students");
System.out.println(students);
//为了方便和统一,将其json统一为String类型