工作写代码经常遇到string的数据类型转换,每次都去搜索如何转换很不方便。写篇博客做个总结,以后看这篇就好了。
1.string-bigDecimal
2.string-date
@Testpublic void StringToDate() throws ParseException {Date date = new SimpleDateFormat("yyyy-MM-dd").parse("2020-02-11");System.out.println(date);}@Testpublic void DateToString() {String str = new SimpleDateFormat("yyyyMMdd").format(new Date());System.out.println(str);}
3.string-integer
@Testpublic void StringInteger() {// string->integerInteger a = Integer.parseInt("123");System.out.println(a);// integer->stringString b1 = 123 + "";System.out.println(b1);String b2 = Integer.toString(13445);System.out.println(b2);String b3 = String.valueOf(13985);System.out.println(b3);String b4 = new Integer(13445).toString();System.out.println(b4);String b5 = String.format("%d", 13985);System.out.println(b5);}
参考:https://javadevnotes.com/java-integer-to-string-examples