Java常用类
文本
String,StringBuffer和StringBuild
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 import org.junit.Test;public class StringBufferBuilder { @Test public void test1 () { StringBuffer sb1 = new StringBuffer("abc" ); sb1.setCharAt(0 ,'m' ); System.out.println(sb1); StringBuffer sb2 = new StringBuffer(); System.out.println(sb2.length()); } @Test public void test2 () { StringBuffer s1 = new StringBuffer("abc" ); s1.append(1 ); s1.append('1' ); System.out.println(s1); String s2 = s1.substring(1 , 3 ); System.out.println(s1); System.out.println(s1.length()); System.out.println(s2); } @Test public void test3 () { long startTime = 0L ; long endTime = 0L ; String text = "" ; StringBuffer buffer = new StringBuffer("" ); StringBuilder builder = new StringBuilder("" ); startTime = System.currentTimeMillis(); for (int i = 0 ; i < 20000 ; i++) { buffer.append(String.valueOf(i)); } endTime = System.currentTimeMillis(); System.out.println("StringBuffer的执行时间:" + (endTime - startTime)); startTime = System.currentTimeMillis(); for (int i = 0 ; i < 20000 ; i++) { builder.append(String.valueOf(i)); } endTime = System.currentTimeMillis(); System.out.println("StringBuilder的执行时间:" + (endTime - startTime)); startTime = System.currentTimeMillis(); for (int i = 0 ; i < 20000 ; i++) { text = text + i; } endTime = System.currentTimeMillis(); System.out.println("String的执行时间:" + (endTime - startTime)); } }
数字和数学运算
在 Java 8 中处理日期和时间
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 import org.junit.Test;import java.text.ParseException;import java.text.SimpleDateFormat;import java.util.Calendar;import java.util.Date;public class DateTime { @Test public void test1 () { long time = System.currentTimeMillis(); System.out.println(time); } @Test public void test2 () { Date date1 = new Date(); System.out.println(date1.toString()); System.out.println(date1.getTime()); Date date2 = new Date(1582115784712L ); System.out.println(date2.toString()); java.sql.Date date3 = new java.sql.Date(1582115784712L ); System.out.println(date3); Date date6 = new Date(); java.sql.Date date7 = new java.sql.Date(date6.getTime()); } @Test public void testSimpleDateFormat () throws ParseException { SimpleDateFormat sdf = new SimpleDateFormat(); Date date = new Date(); System.out.println(date); String format = sdf.format(date); System.out.println(format); String str = "20-2-19 下午8:53" ; Date date1 = sdf.parse(str); System.out.println(date1); SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss" ); String format1 = sdf1.format(date); System.out.println(format1); Date date2 = sdf1.parse("2020-02-19 08:53:45" ); System.out.println(date2); } @Test public void testExer () throws ParseException { String birth = "2020-09-08" ; SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd" ); Date date = sdf1.parse(birth); java.sql.Date birthDate = new java.sql.Date(date.getTime()); System.out.println(birthDate); } @Test public void testCalendar () { Calendar calendar = Calendar.getInstance(); int days = calendar.get(Calendar.DAY_OF_MONTH); System.out.println(days); System.out.println(calendar.get(Calendar.DAY_OF_YEAR)); calendar.set(Calendar.DAY_OF_MONTH, 22 ); days = calendar.get(Calendar.DAY_OF_MONTH); System.out.println(days); calendar.add(Calendar.DAY_OF_MONTH, -3 ); days = calendar.get(Calendar.DAY_OF_MONTH); System.out.println(days); Date date = calendar.getTime(); System.out.println(date); Date date1 = new Date(); calendar.setTime(date1); days = calendar.get(Calendar.DAY_OF_MONTH); System.out.println(days); } }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 import org.junit.Test;import java.time.*;import java.time.format.DateTimeFormatter;import java.time.format.FormatStyle;import java.time.temporal.TemporalAccessor;import java.util.Date;public class NewDateTime { @Test public void testDate () { Date date1 = new Date(2020 - 1900 ,9 - 1 ,8 ); System.out.println(date1); } @Test public void test1 () { LocalDate localDate = LocalDate.now(); LocalTime localTime = LocalTime.now(); LocalDateTime localDateTime = LocalDateTime.now(); System.out.println(localDate); System.out.println(localTime); System.out.println(localDateTime); LocalDateTime localDateTime1 = LocalDateTime.of(2020 , 10 , 6 , 13 , 23 , 43 ); System.out.println(localDateTime1); System.out.println(localDateTime.getDayOfMonth()); System.out.println(localDateTime.getDayOfWeek()); System.out.println(localDateTime.getMonth()); System.out.println(localDateTime.getMonthValue()); System.out.println(localDateTime.getMinute()); LocalDate localDate1 = localDate.withDayOfMonth(22 ); System.out.println(localDate); System.out.println(localDate1); LocalDateTime localDateTime2 = localDateTime.withHour(4 ); System.out.println(localDateTime); System.out.println(localDateTime2); LocalDateTime localDateTime3 = localDateTime.plusMonths(3 ); System.out.println(localDateTime); System.out.println(localDateTime3); LocalDateTime localDateTime4 = localDateTime.minusDays(6 ); System.out.println(localDateTime); System.out.println(localDateTime4); } @Test public void test2 () { Instant instant = Instant.now(); System.out.println(instant); OffsetDateTime offsetDateTime = instant.atOffset(ZoneOffset.ofHours(8 )); System.out.println(offsetDateTime); long milli = instant.toEpochMilli(); System.out.println(milli); Instant instant1 = Instant.ofEpochMilli(1550475314878L ); System.out.println(instant1); } @Test public void test3 () { DateTimeFormatter formatter = DateTimeFormatter.ISO_LOCAL_DATE_TIME; LocalDateTime localDateTime = LocalDateTime.now(); String str1 = formatter.format(localDateTime); System.out.println(localDateTime); System.out.println(str1); TemporalAccessor parse = formatter.parse("2019-02-18T15:42:18.797" ); System.out.println(parse); DateTimeFormatter formatter1 = DateTimeFormatter.ofLocalizedDateTime(FormatStyle.LONG); String str2 = formatter1.format(localDateTime); System.out.println(str2); DateTimeFormatter formatter2 = DateTimeFormatter.ofLocalizedDate(FormatStyle.MEDIUM); String str3 = formatter2.format(LocalDate.now()); System.out.println(str3); DateTimeFormatter formatter3 = DateTimeFormatter.ofPattern("yyyy-MM-dd hh:mm:ss" ); String str4 = formatter3.format(LocalDateTime.now()); System.out.println(str4); TemporalAccessor accessor = formatter3.parse("2019-02-18 03:52:09" ); System.out.println(accessor); } }
Math
异常处理
代码校验
Junit单元测试
测试分类:
黑盒测试:指定输入看输出
白盒测试:关注程序执行流程
Junit属于白盒测试
定义一个测试类(测试用例)
测试类名:被测试的类名Test
包名:xxx.xxx.xx.test
定义测试方法:可以独立运行
方法名:test测试的方法名
返回值:void
参数列表:空参
给方法加上@Test,导入Junit依赖环境
1 2 3 4 5 6 7 8 9 @Test public void testAdd () { Calculator calculator = new Calculator(); int result = calculator.add(1 , 2 ); Assert.assertEquals(3 ,result); }
@Before
初始化方法:用于资源申请,所有测试方法在执行之前都会执行该方法
@After
释放资源方法:在所有测试方法执行完后,都会自动执行该方法