-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathchapter.java.type.xml
More file actions
2708 lines (2323 loc) · 71.2 KB
/
chapter.java.type.xml
File metadata and controls
2708 lines (2323 loc) · 71.2 KB
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?xml version="1.0" encoding="UTF-8"?>
<chapter id="index"><?dbhtml dir="type" ?>
<title>数据类型</title>
<para>数据类型的最大值和最小值。</para>
<screen>
<![CDATA[
基本类型:int 二进制位数:32
包装类:java.lang.Integer
最小值:Integer.MIN_VALUE= -2147483648 (-2的31次方)
最大值:Integer.MAX_VALUE= 2147483647 (2的31次方-1)
基本类型:short 二进制位数:16
包装类:java.lang.Short
最小值:Short.MIN_VALUE=-32768 (-2的15此方)
最大值:Short.MAX_VALUE=32767 (2的15次方-1)
基本类型:long 二进制位数:64
包装类:java.lang.Long
最小值:Long.MIN_VALUE=-9223372036854775808 (-2的63次方)
最大值:Long.MAX_VALUE=9223372036854775807 (2的63次方-1)
基本类型:float 二进制位数:32
包装类:java.lang.Float
最小值:Float.MIN_VALUE=1.4E-45 (2的-149次方)
最大值:Float.MAX_VALUE=3.4028235E38 (2的128次方-1)
基本类型:double 二进制位数:64
包装类:java.lang.Double
最小值:Double.MIN_VALUE=4.9E-324 (2的-1074次方)
最大值:Double.MAX_VALUE=1.7976931348623157E308 (2的1024次方-1)
]]>
</screen>
<section id="var">
<title>var 本地变量类型推断</title>
<programlisting>
<![CDATA[
var javastack = "javastack";
就等于:
String javastack = "javastack";
]]>
</programlisting>
</section>
<section id="integer">
<title>Integer 整形</title>
<section>
<title>字符串转整数</title>
<programlisting>
<![CDATA[
Integer x =Integer.valueOf("1024");
]]>
</programlisting>
</section>
<section>
<title>进制转换</title>
<programlisting>
<![CDATA[
十进制转成十六进制:
Integer.toHexString(int i)
十进制转成八进制
Integer.toOctalString(int i)
十进制转成二进制
Integer.toBinaryString(int i)
十六进制转成十进制
Integer.valueOf("FFFF",16).toString()
八进制转成十进制
Integer.valueOf("876",8).toString()
二进制转十进制
Integer.valueOf("0101",2).toString()
有什么方法可以直接将2,8,16进制直接转换为10进制的吗?
java.lang.Integer类
parseInt(String s, int radix)
使用第二个参数指定的基数,将字符串参数解析为有符号的整数。
examples from jdk:
parseInt("0", 10) returns 0
parseInt("473", 10) returns 473
parseInt("-0", 10) returns 0
parseInt("-FF", 16) returns -255
parseInt("1100110", 2) returns 102
parseInt("2147483647", 10) returns 2147483647
parseInt("-2147483648", 10) returns -2147483648
parseInt("2147483648", 10) throws a NumberFormatException
parseInt("99",throws a NumberFormatException
parseInt("Kona", 10) throws a NumberFormatException
parseInt("Kona", 27) returns 411787
]]>
</programlisting>
<programlisting>
<![CDATA[
int i=100;
String binStr=Integer.toBinaryString(i);
String otcStr=Integer.toOctalString(i);
String hexStr=Integer.toHexString(i);
System.out.println(binStr);
]]>
</programlisting>
<programlisting>
<![CDATA[
Integer factor = Integer.valueOf(args[0]);
String s;
s = String.format("%d", factor);
System.out.println(s);
s = String.format("%x", factor);
System.out.println(s);
s = String.format("%o", factor);
System.out.println(s);
其他方法:
Integer.toHexString(你的10进制数);
例如
String temp = Integer.toHexString(75);
输出temp就为 4b
]]>
</programlisting>
</section>
<section>
<title>前面补零</title>
<programlisting>
<![CDATA[
public static String frontCompWithZore(int sourceDate,int formatLength) {
String newString = String.format("%0"+formatLength+"d", sourceDate);
return newString;
}
]]>
</programlisting>
</section>
<section id="NumberFormat">
<title>NumberFormat 数字格式化</title>
<programlisting>
<![CDATA[
package cn.netkiller.test;
import java.text.NumberFormat;
import java.util.Locale;
public class Test {
public static void main(String[] args) {
NumberFormat fmt = NumberFormat.getCompactNumberInstance(Locale.ENGLISH, NumberFormat.Style.SHORT);
System.out.println(fmt.format(1000));
System.out.println(fmt.format(100000));
System.out.println(fmt.format(1000000));
fmt = NumberFormat.getCompactNumberInstance(Locale.ENGLISH, NumberFormat.Style.LONG);
System.out.println(fmt.format(1000));
System.out.println(fmt.format(100000));
System.out.println(fmt.format(1000000));
fmt = NumberFormat.getCompactNumberInstance(Locale.CHINESE, NumberFormat.Style.SHORT);
System.out.println(fmt.format(1000));
System.out.println(fmt.format(100000));
System.out.println(fmt.format(1000000));
fmt = NumberFormat.getCompactNumberInstance(Locale.CHINESE, NumberFormat.Style.LONG);
System.out.println(fmt.format(1000));
System.out.println(fmt.format(100000));
System.out.println(fmt.format(1000000));
}
}
]]>
</programlisting>
<screen>
<![CDATA[
1K
100K
1M
1 thousand
100 thousand
1 million
1,000
10万
100万
1,000
10万
100万
]]>
</screen>
</section>
<section>
<title>从字符串中提取数字</title>
<programlisting>
<![CDATA[
import java.util.stream.Collectors;
public class ExtractNumbersStream {
public static void main(String[] args) {
String input = "hello123world456";
// 将字符串转换为字符流,筛选数字并拼接
String result = input.chars()
.filter(Character::isDigit)
.mapToObj(c -> String.valueOf((char) c))
.collect(Collectors.joining());
System.out.println("提取的数字: " + result); // 输出: 123456
}
}
]]>
</programlisting>
</section>
</section>
<section id="string">
<title>String 字符串</title>
<para>Java 11 增加了一系列的字符串处理方法,如以下所示。</para>
<screen>
<![CDATA[
// 判断字符串是否为空白
" ".isBlank(); // true
// 去除首尾空格
" Javastack ".strip(); // "Javastack"
// 去除尾部空格
" Javastack ".stripTrailing(); // " Javastack"
// 去除首部空格
" Javastack ".stripLeading(); // "Javastack "
]]>
</screen>
<section id="string.indexOf">
<title>查找字符重现的位置</title>
<programlisting>
<![CDATA[
package cn.netkiller.test;
public class Test {
public static void main(String[] args) {
String string = new String("http://www.netkiller.cn");
System.out.println("查找字符 . 第一次出现的位置: " + string.indexOf('.'));
System.out.println("从第15个字符位置查找字符 . 出现的位置:" + string.indexOf('.', 15));
}
}
]]>
</programlisting>
</section>
<section id="string.line">
<title>行数统计</title>
<screen>
<![CDATA[
"A\nB\nC".lines().count(); // 3
]]>
</screen>
</section>
<section id="string.repeat">
<title>复制字符串</title>
<programlisting>
<![CDATA[
"Java".repeat(3);// "JavaJavaJava"
]]>
</programlisting>
</section>
<section id="string.random">
<title>随机字符串</title>
<programlisting>
<![CDATA[
public String randomString(String chars, int length) {
Random rand = new Random();
StringBuilder buf = new StringBuilder();
for (int i = 0; i < length; i++) {
buf.append(chars.charAt(rand.nextInt(chars.length())));
}
return buf.toString();
}
/**
* 获取4位随机验证码
* @return
*/
public static String getValidationCode(){
return String.valueOf((new Random().nextInt(8999) + 1000));
}
/**
* 获取6位随机验证码
* @return
*/
public static String getValidationCode(){
return String.valueOf((new Random().nextInt(899999) + 100000));
}
]]>
</programlisting>
<programlisting>
<![CDATA[
import java.util.Random;
public class RandomNumberGenerator {
public static void main(String[] args) {
Random random = new Random();
// 生成1000到9999之间的随机整数
int randomNumber = random.nextInt(9000) + 1000;
System.out.println("生成的4位随机数字是:" + randomNumber);
}
}
public class RandomNumberGenerator {
public static void main(String[] args) {
// 生成1000到9999之间的随机整数
int randomNumber = (int) (Math.random() * 9000) + 1000;
System.out.println("生成的4位随机数字是:" + randomNumber);
}
}
import java.security.SecureRandom;
public class RandomNumberGenerator {
public static void main(String[] args) {
SecureRandom secureRandom = new SecureRandom();
// 生成1000到9999之间的安全随机整数
int randomNumber = secureRandom.nextInt(9000) + 1000;
System.out.println("生成的4位安全随机数字是:" + randomNumber);
}
}
import java.util.Random;
// 说明:此方法会生成一个0-9999的随机整数,然后利用String.format("%04d", number)将其格式化为长度为 4 的字符串,若数字不足 4 位,会在前面补0,例如0001、0123等。
public class RandomNumberGenerator {
public static void main(String[] args) {
Random random = new Random();
// 生成0到9999之间的随机整数
int randomNumber = random.nextInt(10000);
// 格式化为4位字符串,不足4位时在前面补0
String formattedNumber = String.format("%04d", randomNumber);
System.out.println("生成的4位随机数字字符串是:" + formattedNumber);
}
}
]]>
</programlisting>
</section>
<section id="string.replace">
<title>字符串替换处理</title>
<section>
<title>替换转意字符</title>
<programlisting>
<![CDATA[
public class Test {
public Test() {
// TODO Auto-generated constructor stub
}
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println("2010-09-11T20:00:30".replace("T", " "));
}
}
]]>
</programlisting>
<programlisting>
<![CDATA[
{"status":0,"message":"","bankcode":"ABOC;IBC;CCTB;ICBC"}
转换后
{\"status\":0,\"message\":\"\",\"bankcode\":\"ABOC;IBC;CCTB;ICBC\"}
]]>
</programlisting>
<programlisting>
<![CDATA[
package test;
public class str {
public static void main(String[] args) {
String jsonString = "{\"status\":0,\"message\":\"\",\"bankcode\":\"ABOC;IBC;CCTB;ICBC\"}";
System.out.println(jsonString);
System.out.println(jsonString.replace("\"", "\\\""));
}
}
]]>
</programlisting>
</section>
<section id="replaceAll">
<title>replaceAll</title>
<programlisting>
<![CDATA[
package cn.netkiller.test;
public class Test {
public static void main(String[] args) throws InterruptedException {
System.out.println("我是小云,你是谁小策".replaceAll("小云|小策", ""));
}
}
]]>
</programlisting>
</section>
<section>
<title>替换控制字符</title>
<programlisting>
<![CDATA[
str.replaceAll("\\p{Cntrl}", "");
]]>
</programlisting>
</section>
<section>
<title>正则表达式查找与替换</title>
<para>查找特定字符并替换为找到的内容</para>
<programlisting>
<![CDATA[
package cn.netkiller.type;
public class ragexTest {
public ragexTest() {
// TODO Auto-generated constructor stub
}
public static void main(String[] args) {
// TODO Auto-generated method stub
String str = "<html>Netkiller</html>";
String regex = "<html>|</html>";
//运行结果返回 Netkiller
System.out.println(str.replaceAll(regex, ""));
// 运行结果返回 Neo
System.out.println("CN/NETKILLER/WWW/Neo_Chen".replaceAll(".*/(.+)_Chen", "$1"));
}
}
]]>
</programlisting>
</section>
<section>
<title>利用正则快速转换时间格式</title>
<programlisting>
<![CDATA[
// 20200303 => 2020-03-03
date = date.replace(/(.{4})/, "$1-");
date = date.replace(/(.{7})/, "$1-");
]]>
</programlisting>
<para>用正则处理 Alertmanager 日期 rfc3339 格式
2021-08-23T01:30:30.2206015+08:00,Alertmanager 日期格式中有7位数的毫秒,使用
SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSXXX") 无法正常读取。
</para>
<programlisting>
<![CDATA[
package cn.netkiller.alertmanager;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.TimeZone;
public class Test {
public Test() {
// TODO Auto-generated constructor stub
}
public static void main(String[] args) throws ParseException {
// TODO Auto-generated method stub
String rfc3339 = "2021-08-23T01:30:30.2206015+08:00";
rfc3339 = rfc3339.replaceAll(".[0-9]{7}", "");
System.out.println(rfc3339);
System.out.println(rfc3339.replaceAll("([0-9]{4})-([0-9]{2})-([0-9]{2})T([0-9]{2}):([0-9]{2}):([0-9]{2})+(.*)", "$1年$2月$3日 $4时$5分$6秒"));
System.out.println(rfc3339.replaceAll("([0-9]{4}-[0-9]{2}-[0-9]{2})T([0-9]{2}:[0-9]{2}:[0-9]{2})+(.+)", "日期:$1 时间:$2"));
// SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSXXX");
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssXXX");
simpleDateFormat.setTimeZone(TimeZone.getTimeZone("GMT+8"));
System.out.println(simpleDateFormat.parse(rfc3339).toString());
}
}
]]>
</programlisting>
<screen>
<![CDATA[
2021-08-23T01:30:30+08:00
2021年08月23日 01时30分30秒
日期:2021-08-23 时间:01:30:30
Mon Aug 23 01:30:30 CST 2021
]]>
</screen>
</section>
<section id="string.replaceAll">
<title>replaceAll</title>
<para>替换一组字符串</para>
<programlisting>
<![CDATA[
String str1 = "广东省,福建省,北京市,海淀区,河北省,上海市";
str1 = str1.replaceAll("(省|市|区)", "");
System.out.println("替换多个中文:" + str1);
替换多个中文:广东,福建,北京,海淀,河北,上海
]]>
</programlisting>
<para>替换 Ascii</para>
<programlisting>
<![CDATA[
String string = "佛山市123南海区ABC精密abc机械有限公司,";
String clean = string.replaceAll("\\P{Print}", "");
System.out.println(clean);
]]>
</programlisting>
</section>
<section id="string.unicode">
<title>字符串处理,删除中文以外的字符</title>
<para>Unicode 码表 https://www.ssec.wisc.edu/~tomw/java/unicode.html
</para>
<programlisting>
<![CDATA[
String string = "2017-12-18 netkiller http://www.netkiller.cn - 网站正常";
System.out.println(string.replaceAll("[^\u4e00-\u9fa5]", ""));
]]>
</programlisting>
</section>
<section id="string.regex">
<title>取出字符串中的中文字符</title>
<para>Unicode 码表 https://www.ssec.wisc.edu/~tomw/java/unicode.html
</para>
<programlisting>
<![CDATA[
String string = "2017-12-18 netkiller http://www.netkiller.cn - 网站正常";
String regex = "[\u4e00-\u9fa5]";
System.out.println(string.replaceAll(regex, ""));
]]>
</programlisting>
</section>
<section id="替换行首标点符号">
<title>替换行首的标点符号</title>
<programlisting>
<![CDATA[
public static String cleansing(String text) {
String replacement = "";
Pattern pattern = Pattern.compile("^[\\p{P}]", Pattern.MULTILINE);
Matcher matcher = pattern.matcher(text);
String result = matcher.replaceAll(replacement);
return result;
}
]]>
</programlisting>
</section>
<section>
<title>查找替换末尾最后一个字符</title>
<programlisting>
<![CDATA[
package cn.netkiller.test;
import java.util.List;
public class Test {
public Test() {
}
public static String replaceEnd(String string) {
char c = string.charAt(string.length() - 1);
if (List.of(',', ',', '、').contains(c)) {
string = string.substring(0, string.length() - 1);
}
return string;
}
public static void main(String[] args) throws Exception {
List.of("你好!", "你好,中国,", "Hello,", "Hello world!", "你在干啥么吗?没看到吗?", "这里真好").forEach(s -> {
System.out.println(replaceEnd(s));
});
}
}
]]>
</programlisting>
<screen>
<![CDATA[
你好!
你好,中国
Hello
Hello world!
你在干啥么吗?没看到吗?
这里真好
]]>
</screen>
</section>
<section>
<title>数字开头的字符串前面加回车符</title>
<programlisting>
<![CDATA[
String original = "1: neo 2: netkiller 3: other";
System.out.println(original.replaceAll("\n", "").replaceAll("(?=^\\d:|\\d:)", "\n"));
]]>
</programlisting>
<screen>
<![CDATA[
1: neo
2: netkiller
3: other
]]>
</screen>
</section>
</section>
<section id="substring">
<title>substring</title>
<programlisting>
<![CDATA[
例如:
String str = "helloword!!!";
System.out.println(str.substring(1,4));
System.out.println(str.substring(3,5));
System.out.println(str.substring(0,4));
将得到结果为:
ell
lo
hell
]]>
</programlisting>
</section>
<section id="string2tempstamp">
<title>string to timestamp</title>
<para>Timestamp转化为String:</para>
<programlisting>
SimpleDateFormat df = new
SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); //定义格式,不显示毫秒
Timestamp now =
new Timestamp(System.currentTimeMillis());
//获取系统当前时间
String str =
df.format(now);
</programlisting>
<para>String转化为Timestamp:</para>
<programlisting>
SimpleDateFormat df = new
SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String time = df.format(new
Date());
Timestamp ts = Timestamp.valueOf(time);
</programlisting>
</section>
<section id="String.strip">
<title>String.strip</title>
<para>java11对String类新增了strip,stripLeading以及stripTrailing方法,除了strip相关的方法还新增了isBlank、lines、repeat(int)等方法
</para>
<screen>
<![CDATA[
@Test
public void testStrip(){
String text = " \u2000a b ";
Assert.assertEquals("a b",text.strip());
Assert.assertEquals("\u2000a b",text.trim());
Assert.assertEquals("a b ",text.stripLeading());
Assert.assertEquals(" \u2000a b",text.stripTrailing());
}
]]>
</screen>
</section>
<section id="string.block">
<title>文本块</title>
<programlisting>
<![CDATA[
package cn.netkiller.kubernetes;
public class Test {
public Test() {
}
public static void main(String[] args) {
String json = """
{
"name" : "netkiller",
"home" : "https://www.netkiller.cn/"
}
""";
System.out.println(json);
}
}
]]>
</programlisting>
</section>
<section id="string.split">
<title>分割字符串</title>
<screen>
<![CDATA[
String[] string = topic.split("/");
Log.d("Service", string.length + "");
for (String str: string) {
Log.d("Service", str);
}
String str2 = new String("www.netkiller.cn");
for (String retval: str2.split("\\.", 3)){
System.out.println(retval);
}
System.out.println("");
String str3 = new String("acount=? and uu =? or n=?");
for (String retval: str3.split("and|or")){
System.out.println(retval);
}
]]>
</screen>
</section>
<section id="String.format">
<title>String.format 字符串格式化</title>
<section>
<title>前补零</title>
<programlisting>
<![CDATA[
package cn.netkiller.test;
import lombok.Data;
import java.io.IOException;
@Data
public class Test {
public static void main(String[] args) throws IOException {
System.out.printf("%02d%n", 1);
System.out.printf("%04d%n", 90);
System.out.println(String.format("%02d", 1));
System.out.println(String.format("%04d", 90));
}
}
]]>
</programlisting>
<screen>
<![CDATA[
01
0090
01
0090
]]>
</screen>
</section>
<section>
<title>前补零</title>
<programlisting>
<![CDATA[
package cn.netkiller.test;
import lombok.Data;
import java.io.IOException;
import java.text.DecimalFormat;
@Data
public class Test {
public static void main(String[] args) throws IOException {
int test = 520;
String paddedStr = String.format("%-6s", test).replace(' ', '0');
System.out.println("后补零:" + paddedStr);
}
}
]]>
</programlisting>
<screen>
<![CDATA[
后补零:520000
]]>
</screen>
</section>
</section>
<section id="java.text.DecimalFormat">
<title>DecimalFormat 文本格式化</title>
<programlisting>
<![CDATA[
package cn.netkiller.test;
import lombok.Data;
import java.io.IOException;
import java.text.DecimalFormat;
@Data
public class Test {
public static void main(String[] args) throws IOException {
int test = 520;
System.out.println("================ 前补零 =================");
DecimalFormat decimalFormat = new DecimalFormat("000000");
String before = decimalFormat.format(test);
System.out.println("前补零:" + before);
DecimalFormat decimalFormat1 = new DecimalFormat("0.000");
System.out.println("================ 小数点后补零 =================");
String after = decimalFormat1.format(test);
System.out.println("后补零:" + after);
}
}
]]>
</programlisting>
<screen>
<![CDATA[
================ 前补零 =================
前补零:000520
================ 小数点后补零 =================
后补零:520.000
]]>
</screen>
</section>
<section id="stream().anyMatch()">
<title>通过 stream() 匹配一组字符串</title>
<programlisting>
<![CDATA[
String str1 = "Hello, World!";
List<String> str2 = Arrays.asList("World", "Hello");
boolean contains = str2.stream().anyMatch(str1::contains);
System.out.println("找到字符串:" + contains);
]]>
</programlisting>
</section>
<section>
<title>字符串输出转译字符串</title>
<programlisting>
<![CDATA[
public static String escapeString(String input) {
if (input == null) return null;
return input
.replace("\\", "\\\\") // 转义反斜杠
.replace("\t", "\\t") // 转义制表符
.replace("\b", "\\b") // 转义退格符
.replace("\n", "\\n") // 转义换行符
.replace("\r", "\\r") // 转义回车符
.replace("\f", "\\f") // 转义换页符
.replace("'", "\\'") // 转义单引号
.replace("\"", "\\\""); // 转义双引号
}
]]>
</programlisting>
<programlisting>
<![CDATA[
// 使用示例
String original = "Hello\nWorld\t\"Java\"";
String escaped = escapeString(original);
System.out.println(escaped); // 输出: Hello\nWorld\t\"Java\"
]]>
</programlisting>
</section>
</section>
<section id="long">
<title>类型转换</title>
<section>
<title>Long to String</title>
<programlisting>
<![CDATA[
public class MainClass {
public static void main(String[] arg) {
long b = 12L;
System.out.println(String.valueOf(b));
}
}
]]>
</programlisting>
</section>
</section>
<section id="date">
<title>Date 日期时间</title>
<para>java.util.Date, java.sql.Date, java.sql.Time, java.sql.Timestamp
区别
</para>
<section id="date.new">
<title>String to Date</title>
<programlisting>
<![CDATA[
package cn.netkiller.java.date;
/**
* Hello world!
*
*/
public class App {
public static void main(String[] args) {
System.out.println("Hello World!");
// Get standard date and time
java.util.Date utilDate = new java.util.Date();
long javaTime = utilDate.getTime();
System.out.println("The Java Date is:" + utilDate.toString());
// Get and display SQL DATE
java.sql.Date sqlDate = new java.sql.Date(javaTime);
System.out.println("The SQL DATE is: " + sqlDate.toString());
// Get and display SQL TIME
java.sql.Time sqlTime = new java.sql.Time(javaTime);
System.out.println("The SQL TIME is: " + sqlTime.toString());
// Get and display SQL TIMESTAMP
java.sql.Timestamp sqlTimestamp = new java.sql.Timestamp(javaTime);
System.out.println("The SQL TIMESTAMP is: " + sqlTimestamp.toString());
}
}
]]>
</programlisting>
<screen>
<![CDATA[
The Java Date is:Thu Aug 24 16:51:57 CST 2017
The SQL DATE is: 2017-08-24
The SQL TIME is: 16:51:57
The SQL TIMESTAMP is: 2017-08-24 16:51:57.234
]]>
</screen>
<programlisting>
<![CDATA[
Date currentDate = new Date();
int month = currentDate.getMonth() + 1;
System.out.println("当前月份为:" + month);
]]>
</programlisting>
</section>
<section id="DateTimeFormatter">
<title>DateTimeFormatter 格式化日期和时间</title>
<programlisting>
<![CDATA[
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
public class Main {
public static void main(String[] args) {
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
LocalDateTime dateTime = LocalDateTime.now();
String formattedDateTime = dateTime.format(formatter);
System.out.println(formattedDateTime);
}
}
]]>
</programlisting>
<programlisting>
<![CDATA[
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("B");
System.out.println(dtf.format(LocalTime.of(8, 0)));
System.out.println(dtf.format(LocalTime.of(13, 0)));
System.out.println(dtf.format(LocalTime.of(20, 0)));
System.out.println(dtf.format(LocalTime.of(23, 0)));