zoukankan      html  css  js  c++  java
  • 常用类

    Java中常用类:

    String类、Random类、Date类、还包括:Scanner类、Math类、等

    一、 String类

     1 package org.wanglei.hello;
     2 
     3 //import com.sun.org.apache.xpath.internal.operations.Equals;
     4 
     5 public class StringExDemo {
     6 
     7     /**
     8      * 打印出以hello开头的文件的后缀名
     9      * @param args
    10      */
    11 
    12     public static void main(String[] args) {
    13         test1();
    14         test2();
    15         test3();
    16     }
    17 
    18     // 获取以hello开头的文件的后缀名
    19     private static void test1() {
    20         String fileName = new String(
    21                 "hi.java,hello.java,hello.txt,hehe.txt,hello.jar");
    22         System.out.println(fileName);
    23         // 以;分割
    24         String[] names = fileName.split(",");
    25         // System.out.println(names.toString());
    26         // 迭代出数组里面的以hello开头的文件名
    27         for (String eleName : names) {
    28             if (eleName.startsWith("hello")) {
    29                 System.out.println(eleName);
    30                 // 获取最后一个.的索引
    31                 int index = eleName.lastIndexOf(".");
    32                 System.out.println(index);
    33                 // 从索引开始,截取后面的字符串
    34                 System.out.println(eleName.substring(index));
    35 
    36             }
    37         }
    38     }
    39 
    40     private static void test2() {
    41         String str = "leizi";
    42         //拿出首字母并大写
    43         String head = str.substring(0, 1).toUpperCase();
    44         //拿出首字母后面的字母,字符串形式的
    45         String rear = str.substring(1);
    46         System.out.println(head + rear);
    47 
    48     }
    49     private static void test3(){
    50         String str1 = "";
    51         //String str2 = " ";
    52         //String str3 = "hello";
    53         //String str4 = null;
    54         if (str1 != null && !"".equals(str1.trim())) {
    55             System.out.println("非空");
    56         }
    57         else {
    58             System.out.println("空!");
    59         }
    60         System.out.println("--------------------------------------");
    61         String str2 = "hello";
    62         System.out.println(StringUtil.hasLength(str2));  //直接调用StringUtil方法判断
    63         
    64     }
    65 
    66 }
    View Code

    -----------------------------

    因为在java1.5之前,是不存在StringBuilder的,StringBuffer之前就有,但是很少有应用场景。

    所以在字符串拼接时,基本上都用StringBuilder。

    注意:

     1 package org.wang.hello;
     2 
     3 //StringBuilder的练习操作,包括调用的一些方法
     4 public class StringBuilderDemo {
     5 
     6     
     7     public static void main(String[] args) {
     8         StringBuilder stbd = new StringBuilder(16);//等价于StringBuilder stbd = new StringBuilder();
     9         
    10         //链式编程
    11         stbd.append("A").append("B");
    12         System.out.println(stbd);
    13         
    14         //删除最后一个字符
    15         String str = "ABCDE";
    16         String str1 = new StringBuffer(str).deleteCharAt(str.length() - 1).toString();
    17         System.out.println(str1);
    18 
    19         //反转一个字符串
    20         String str2 = new StringBuffer(str).reverse().toString();
    21         System.out.println(str2);
    22     }
    23 
    24 }
    View Code

    二、随机数Random

       ------》

     1 package org.wang.random;
     2 
     3 import java.util.Random;
     4 
     5 public class RandomDemo {
     6 
     7     /**
     8      * 生成伪随机数  通过相同的种子产生随机数是相同的
     9      * @param args
    10      */
    11     public static void main(String[] args) {
    12         Random r1 = new Random();
    13         System.out.println(r1.nextInt(100));
    14         System.out.println(r1.nextDouble());
    15         System.out.println(r1.nextBoolean());
    16         System.out.println("-------------------------");
    17         Random r2 = new Random(10);
    18         System.out.println(r2.nextInt(100));
    19         
    20         //生产50-150之间的随机数;
    21         Random r3 = new Random(10);
    22         System.out.println(r3.nextInt(100) + 50 );
    23 
    24     }
    25 
    26 }

     1 package org.wang.random;
     2 
     3 import java.util.UUID;
     4 import java.util.concurrent.ThreadLocalRandom;
     5 
     6 public class ThreadLocalRandomDemo {
     7 
     8     /**介绍ThreadLocalRandom的用法
     9      * @param args
    10      */
    11     public static void main(String[] args) {
    12         ThreadLocalRandom r1 = ThreadLocalRandom.current();
    13         System.out.println(r1.nextInt(5));
    14         System.out.println(r1.nextInt(50, 100)); //生成[50,100)之间的随机数
    15         
    16         //UUID的用法,通用唯一识别。生成策略,用于数据库组件    
    17         String uuid = UUID.randomUUID().toString();
    18         System.out.println(uuid);  //d9fa6975-a92b-40c3-b67d-8d3787b115c3
    19 
    20 
    21     }
    22 
    23 }

    4  
    63  
    c2d9e0b4-0634-489d-b216-33b7b405492c

    ---------------------------------------------------------------

     1 package org.wanglei.random;
     2 
     3 import java.util.Random;
     4 import java.util.UUID;
     5 
     6 public class RandomCodeDemo {
     7 
     8     /**
     9      * @param args
    10      */
    11     public static void main(String[] args) {
    12         // 生成一个5位数的验证码
    13         String code = UUID.randomUUID().toString().substring(0, 5);
    14         System.out.println(code);
    15         System.out.println("-------------------------");
    16         // 生成一个5位数的验证码
    17         String str = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    18         str += str.toLowerCase();
    19         str += "1234567890";
    20         System.out.println(str);
    21         StringBuilder stbd = new StringBuilder(10);
    22         for (int i = 0; i < 5; i++) {
    23             stbd.append(str.charAt(new Random().nextInt(str.length())));
    24         }
    25         System.out.println(stbd);
    26 
    27     }
    28 
    29 }

    50151     (随机)
    -------------------------
    ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890
    VTzX5    (随机)

    --------------------------------

     三、Date类

     Calendar类使用

     1 package org.wang.date;
     2 
     3 import java.util.Calendar;
     4 import java.util.Date;
     5 
     6 public class CalendarDemo {
     7 
     8     /**
     9      * @param args
    10      */
    11     public static void main(String[] args) {
    12         // 创建日历对象
    13         Calendar c = Calendar.getInstance();
    14         
    15         System.out.println(c.get(Calendar.YEAR));
    16         System.out.println(c.get(Calendar.MONTH) + 1);// 月是从0开始算的
    17         System.out.println(c.get(Calendar.DAY_OF_MONTH));
    18         System.out.println(c.get(Calendar.HOUR_OF_DAY));
    19         System.out.println(c.get(Calendar.MINUTE));
    20         System.out.println(c.get(Calendar.SECOND));
    21 
    22         // Calendar转换为Date类型
    23         Date d = c.getTime();
    24     
    25         System.out.println(d.toLocaleString());
    26     }
    27 
    28 }
    View Code

    2017
    8
    3
    15
    1
    33
    2017-8-3 15:01:33

    --------------------------------------------

    Date current = new Date();
    System.out.println(current.toLocaleString());

    ------------------------------------------------------------

    四、其他常用类

    (用的机会很少)

    ------------------------------------------------------------------------------------

  • 相关阅读:
    Vtune 学习笔记 1 Finding Hotspots
    成都 宏碁 售后地址 官方
    Vtune 学习笔记 分析锁 Tutorial: Analyzing Locks and Waits
    (技术贴)铁道部12306网站瘫痪:网络购票技术之罪
    【转】移植之乱谈 深入理解Android 卷I作者谈
    FastReport集粹(一)
    FastReport集粹(二)
    在TDBGrid控件中为作修改过的记录进行标识!
    从此开始我的OGRE开发3D游戏之旅行。
    DBGrid 应用全书
  • 原文地址:https://www.cnblogs.com/wang--lei/p/7279471.html
Copyright © 2011-2022 走看看