【06-与运行环境交互】

若有不正之处,请多多谅解并欢迎批评指正,不甚感激。
请尊重作者劳动成果:

本文原创作者:pipi-changing
本文原创出处:http://www.cnblogs.com/pipi-changing/

本文版权归作者和博客园共有,未经作者同意必须保留此段声明,
且在文章页面明显位置给出原文连接 ,否则保留追究法律责任的权利。

 

 

Java程序的参数


 

 

  •如果运行Java程序时在类名后紧跟一个或多个字符串(多个字符串之间以空格隔开),JVM就会把
 
这些字符串依次赋给args数组元素。
 
 
  •如果某参数本身包含了空格,则应该将该参数用双引号(")括起来,否则JVM会把这个空格当成参
 
数分隔符,而不是当成参数本身。 

 
使用Scanner获取键盘输入 

 

 

    •使用Scanner类可以很方面地获取用户的键盘输入,Scanner是一个基于正则表达式的文本扫描器,它可以从

文件、输入流、字符串中解析出基本类型值和字符串值。Scanner类提供了多个构造器,不同的构造器可接受文件、

输入流、字符串作为数据源,用于从文件、输入流、字符串中解析数据。

   •Scanner主要提供了两个方法来扫描输入:

    –hasNextXxx():是否还有下一个输入项,其中Xxx可以是Int、Long等代表基本数据类型的字符串。如果

需要判断是否包含下一个字符串,则可以省略Xxx。

    –nextXxx():获取下一个输入项。Xxx的含义与前一个方法中Xxx相同。

 


 

使用BufferedReader读取键盘

 

  •BufferedReader是Java IO流中的一个字符、包装流,它必须建立在另一个字符流的基础之上。但标准输入:

System.in是字节流 ,程序需要使用转换流InputStreamReader将其包装字符流。

  •使用BufferedReader可以逐行读取用户的键盘输入,每次用户的键盘输入都被BufferedReader当成String

对象。与Scanner不同的是,BufferedReader不能读取基本类型输入项,它总是读取String对象。

 


 

 

 System类

 

  •System类代表当前Java程序的运行平台,程序不能创建System类的对象,所以它提供了一些类属性和类方

法,允许直接通过System类名来调用这些属性和方法。

  •System类提供了代表标准输入、标准输出和错误输出的类属性;并提供了一些静态方法用于访问环境变量、系

统属性的方法;还提供了加载文件和动态链接库的方法。下面程序通过System类来访问操作的环境变量和系统属性。 

 


 

 

Runtime类

 

 

  •Runtime类代表Java程序的运行时环境,每个Java程序都有一个与之对应的Runtime实例,应用程序通过该对

象与其运行时环境相连。

  •应用程序不能创建自己的Runtime实例,但可以通过getRuntime()方法获取与之关联的Runtime对象。

  •Runtime类代表Java程序的运行时环境,可以访问JVM的相关信息,如处理器数量,内存信息等。

  •除此之外,Runtime还有一个功能:它可以直接单独启动一条进程来运行操作系统的命令。

 


 

 

Object类

 

 

  •Object类是所有类、数组、枚举类的父类,也就是说,Java允许把所有任何类型的对象赋给Object类型的变

量。当定义一个类时没有使用extends关键字为它显式指定父类,则该类默认继承Object父类。

 


 

对象“克隆”

 

  •Object还提供了一个protected修饰的clone()方法,程序员可重写该方法来实现“浅克隆”。

  •自定义类实现“浅克隆”的步骤:

  •(1)自定义类实现Cloneable接口。

  •(2)自定义类实现clone()方法。

    (3)在clone()方法中通过super.clone()调用Object的clone()方法来实现“浅克隆”。

 


 

 

Java 7新增的Objects类

 

 

  •Objects提供了一些工具方法来操作对象。比如:

  –hashCode():返回指定对象的hashCode值。

  –toString:返回指定对象的“描述性”字符串。

  –requiredNonNull:检查对象是否为null。

 


 

 

String、StringBuffer和StringBuilder

 

  

   •字符串就是一连串的字符序列,Java提供了String和StringBuffer两个类来封装对字符串,并提供了系列方法

来操作字符串对象。

  •String类是不可变类的,

  •StringBuffer对象则代表一个字符序列可变的字符串,当一个StringBuffer被创建以后,通过StringBuffer提

供的append、insert、reverse、setCharAt、setLength等方法可以改变这个字符串对象的字符序列。一旦通过

StringBuffer生成了最终想要的字符串,就可以调用它的toString方法将其转换为一个String对象。

  •JDK1.5又新增了一个StringBuilder类,它也代表了字符串对象。StringBuilder是线程不安全的。

 


 

 

无处不在的字符串

 

 

  •使用String对象存储字符串

    String s = "有志者事竟成";

    String s = new String("有志者事竟成");

  •String类位于java.lang包中,具有丰富的方法

    –计算字符串的长度

    –连接字符串

    –比较字符串

       提取字符串


 

 

字符串比较

 

 

  •equals()方法比较原理

    检查组成字符串内容的字符是否完全一致

  •输入的“Java”和“java”代表同一课程,如何解决?
 
    –使用equalsIgnoreCase()方法
 
    –结合toUpperCase()或toLowerCase()
 

使用equalsIgnoreCase()方法解决

if(favCourse1.equalsIgnoreCase(favCourse2)){
            System.out.println("最喜欢的课程相同");
 }else{
            System.out.println("最喜欢的课程不相同");
 }

 

比较时忽略大小写形式

 

结合toUpperCase()方法

 

if(favCourse1.toUpperCase().equals(favCourse2.toUpperCase())){
            System.out.println("最喜欢的课程相同");
}else{
           System.out.println("最喜欢的课程不相同");
}

 

 

 


 

 

字符串连接

 

    •方法1:使用“+”

 

     •方法2:使用String类的concat()方法

 

 


 

 

 字符串提取方法

 

 

 

  •常用提取方法举例

 

方法

说明

public int indexOf(int ch)

搜索第一个出现的字符ch(或字符串

value)

public int indexOf(String value)

public int lastIndexOf(int ch)

搜索最后一个出现的字符ch(或字符串

value)

public int lastIndexOf(String value)

 

方法

说明

public String substring(int index)

提取从位置索引开始的字符串部分

public String substring(int beginindex, int

endindex)

提取beginindex和endindex之间的字符串部分

public String trim()

返回一个前后不含任何空格的调用字符串的副本

 

 注意:

beginindex: 字符串的位置从0开始算;endindex: 字符串的位置从1开始算 

 


 

 

StringBuffer

 

 

  •StringBuffer:String增强版
 
  •StringBuffer声明

 

  •StringBuffer的使用

 

 


 

 

StringBuffer的用武之地

 

 

从控制台接收课程信息,不断累加直到输入“#”键结束,并输出全部课程信息

 

        //声明课程信息字符串
        StringBuffer course = new StringBuffer();//定义StringBuffer的实例,存储课程字符串
        System.out.println("请输入课程信息: ");
        Scanner sc = new Scanner(System.in);
        
        //循环从键盘接收字符串
        String input;
        while(!(input = sc.next()).equals("#")){
                course.append(input);
                course.append("\n");//循环追加字符,使用StringBuffer,效率高!
        }
        System.out.println("课程信息是:" + course);

 


 

 

 Math类 

 

 

  •Java提供了基本的+、-、*、/、%等基本算术运算的运算符,但对于更复杂的数学运算,例如三角函数、对数运算、指数

运算等则无能无力。Java提供了Math工具类来完成这些复杂的运算。

  •Math类是一个工具类,它的构造器被定义成private的,因此无法创建Math类的对象;Math类中所有方法都是类方法,可

以直接通过类名来调用它们。 

 


 

 

Random与ThreadLocalRandom

 

 

  •Random类专门用于生成一个伪随机数,它有两个构造器:一个构造器使用默认的种子,另一个构造器需要程序员显式传入

一个long型整数的种子。

  •相对于Math的random()方法而言,Random类的提供了更多方法来生成各种伪随机数,它不仅可以生成浮点类型的伪随

机数,也可以生成整数类型的伪随机数,还可以指定生成随机数的范围。

  •ThreadLocalRandom是Java 7新增的,它可以在多线程环境下代替Random减少多线程资源竞争,从而提供更好的线程安全。

 


 

 

BigDecimal类

   

 

  •float、double两种基本浮点类型的浮点数容易引起精度丢失。

  •如果程序中需要对double浮点数进行加、减、乘、除基本运算,则需要先将double类型数值包装成BigDecimal对象,调用

BigDecimal对象的方法执行运算后再将结果转换成double型变量。

 


 

本文原创作者:pipi-changing

本文原创出处:http://www.cnblogs.com/pipi-changing/

 


 

 

Date类 

 

 

  •Java提供了Date类来处理日期、时间。

  •Date类从JDK1.0起就开始存在了。但正因为它历史悠久,所以它的大部分构造器、方法都已经过时,不再推荐使用了。 

 


 

 

Calendar类 

 

 

  •因为Date类的设计上存在一些缺陷,Java提供了Calendar类来更好地处理日期和时间。Calendar是一个抽象类,它用于

表示日历。

  •Calendar本身是一个抽象类,它是所有日历类的模板,并提供了一些所有日历通用的方法,但它本身不能直接实例化。程序

只能创建Calendar子类的实例,Java 本身提供了一个GregorianCalendar类,一个代表Gregorian Calendar的子类,它代表

了我们通常所说的公历。

开发者可以开发自己的Calendar子类。

 


 

Java 8新增的日期、时间包 

 

 

  •Java 8专门新增了一个java.time包,该包下包含了如下常用的类:

    –Clock:该类用于获取指定时区的当前日期、时间。该类可取代System类的currentTimeMillis()方法,而且该类提供

了更多方法来获取当前日期、时间的方法。该类提供了大量静态方法来获取Clock对象。

    –Duration:该类代表持续时间。该类可以非常方便地获取一段时间

    –Instant:代表一个具体的时刻,它可以精确到纳秒。该类提供了静态的now()方法来获取当前时刻,也提供了静态的

now(Clock clock)获取clock对应的时刻。除此之外,它还提供了系列minusXxx()方法在当前时刻基础上减去一段时间,也提供

了plusXxx()方法在当前时刻基础上加上一段时间。

    –LocalDate:该类代表不带时区的日期。例如2007-12-03。该类提供了静态的now()方法来获取当前日期,也提供了

静态的now(Clock clock)获取clock对应的日期。除此之外,它还提供了minusXxx()方法在当前年份基础上减去几年、几月、几

周或几日等,也提供了plusXxx()方法在当前年份基础上加上几年、几月、几周或几日等。

    –LocalTime:该类代表不带时区的时间,例如10:15:30。该类提供了静态的now()方法来获取当前时间,也提供了静

态的now(Clock clock)获取clock对应的时间。除此之外,它还提供了minusXxx()方法在当前年份基础上减去几小时、几分、几

秒等,也提供了plusXxx()方法在当前年份基础上加上几小时、几分、几秒等。

    –LocalDateTime:该类代表不带时区的日期、时间,例如2007-12-03T10:15:30。该类提供了静态的now()方法来

获取当前日期、时间,也提供了静态的now(Clock clock)获取clock对应的日期、时间。除此之外,它还提供了minusXxx()方法

在当前年份基础上减去几年、几月、几日、几小时、几分、几秒等,也提供了plusXxx()方法在当前年份基础上加上几年、几月、

几日、几小时、几分、几秒等。

    –MonthDay:该类仅代表月日。例如--04-12。该类提供了静态的now()方法来获取当前月日,也提供了静态的

now(Clock clock)获取clock对应的月日。

    –Year:该类仅代表年。例如2014。该类提供了静态的now()方法来获取当前年份,也提供了静态的now(Clock

clock)获取clock对应的年份。除此之外,它还提供了minusYears()方法在当前年份基础上减去几年,也提供了plusYears()方

法在当前年份基础上加上几年。

    –YearMonth:该类仅代表年月。例如2014-04。该类提供了静态的now()方法来获取当前年月,也提供了静态的

now(Clock clock)获取clock对应的年月。除此之外,它还提供了minusXxx()方法在当前年月基础上减去几年、几月,也提供了

plusXxx()方法在当前年月基础上加上几年、几月。

    –DayOfWeek:这一个枚举类,定义了周日到周六的枚举值。

    –Month:这也是一个枚举值,定义了一月到十二月的枚举值 

 


 

 

String类的正则表达式支持

 

 

  •boolean matches(String regex):判断该字符串是否匹配指定正则表达式。

  •String replaceAll(String regex, String replacement):返回该字符串中所有匹配正则表达式的子串替换成

replacement后的新字符串

  •String replaceFirst(String regex, String replacement):返回该字符串中第一个匹配正则表达式的子串替换成

replacement后的新字符串

  •String[] split(String regex):根据给定正则表达式的拆分该字符串后得到的字符串数组。

 


 

 

 创建正则表达式 

 

 

  •表达式就是一个字符串模板,可以匹配一批字符串,所以创建正则表达式就是创建一个特殊的字符串。 

 


 

 

正则表达式所支持的合法字符 

 

 

  •  x  字符 :x(x可代表任何合法的字符)\0mnn:8进制数0mnn所表示的字符

  •  \xhh:16进制值0xhh所表示的字符

  •  \uhhhh:16进制值0xhhhh所表示的UNICODE字符

  •  \t :制表符('\u0009')

  •  \n :新行(换行)符('\u000A')

  •  \r :回车符('\u000D')

  •  \f :换页符('\u000C')

  •  \a :报警(bell)符('\u0007')

  •  \e : Escape符('\u001B')

  •  \cx:对应的的控制符。例如,\cM :匹配Ctrl-M。x值必须为A-Z或a-z之一。

 


 

 

 正则表达式中的特殊字符 

 

 

•  $:匹配一行的结尾。要匹配 $ 字符本身,请使用 \$。

•  ^:匹配一行的开头。要匹配 ^ 字符本身,请使用 \^。

•  ( ):标记子表达式的开始和结束位置。要匹配这些字符,请使用 \( 和 \)。

•  [ ]:用于确定中括号表达式的开始和结束位置,要匹配这些字符,请使用 \[ 和 \]。

•  { }:用于标记前面子表达式的出现频度,要匹配这些字符,请使用 \{ 和 \}。

•  *:指定前面子表达式可以出现零次或多次。要匹配 * 字符,请使用 \*。

•  +:指定前面子表达式可以出现一次或多次。要匹配 + 字符,请使用 \+。

•  ?:指定前面子表达式可以出现零次或一次。要匹配 ? 字符,请使用 \?。

•  .:匹配除换行符 \n之外的任何单字符。要匹配 .,请使用 \.

•  \用于转义下一个字符,或指定八进制、十六进制字符。如果需匹配 \,请用\\

•  |:指定两项之间任选一项,如果要匹配 | ,请使用 \|。

 


 

 

预定义字符 

 

 

•  .:可以匹配任何字符

•  \d:匹配0~9的所有数字

•  \D:匹配非数字。

•  \s:匹配所有空白字符,包括空格、制表符、回车符、换页符、换行符等。

•  \S:匹配所有非空白字符。

•  \w:匹配所有单词字符,包括0~9所有数字,26个英文字母和下划线(_)。

•  \W:匹配所有非单词字符。

 


 

 

 使用正则表达式 

 

 

  •一旦在程序中定义了正则表达式之后,就可以使用PatternMatcher来使用正则表达式。

  •Pattern对象是正则表达式的编译后在内存中的表示形式,因此,正则表达式字符串必须先被编译为Pattern对象,然后再利

用该Pattern对象创建对应的Matcher对象。执行匹配所涉及的状态保留在Matcher对象中,多个Matcher对象可共享同一个

Pattern对象。 

 


 

 

 Java国际化的思路 

 

 

  •Java程序的国际化的思路是将程序中的标签,提示等信息放在资源文件中,程序需要支持哪些国家、语言环境,就需要提供

相应的资源文件。资源文件是key-value对,每个资源文件中的key是不变的,但value则随不同国家、语言改变。 

 


 

 

 国际化支持

 

 

  •Java程序的国际化主要通过如下三个类完成:

    –java.util.ResourceBundle:用于加载一个国家、语言资源包。

    –java.util.Locale:用于封装一个特定的国家/区域、语言环境。

    –java.text.MessageFormat:用于格式化带占位符的字符串。

 


 

 

 使用类文件代替资源文件

 

 

  •使用Java文件来代替资源文件的Java文件必须满足如下条件:

    –类的名字必须为baseName_language_country,这与属性文件的命名相似

    –该类必须继承ListResourceBundle,并重写getContents方法,该方法返回Object数组,该数组的每一个项都是

key-value 对。

 


 

 

 处理带占位符的消息

 

 

  •此时需要使用MessageFormat类,该类包含一个有用的静态方法:

    –format(String pattern , Object... values):返回后面的多个参数值填充前面的pattern字符串,其中pattern字符

串不是正则表达式,而是一个带占位符的字符串。 

 


 

 

 使用NumberFormat格式化

 

 

  •NumberFormat和DateFormat都包含了format和parse方法,其中format用于将数值、日期格式化成字符串,parse

用于将字符串解析成数值、日期。

  •NumberFormat也是一个抽象基类,所以无法通过它的构造器来创建NumberFormat对象,它提供了如下几个工厂方法来

得到NumberFormat对象:

    –getCurrencyInstance:返回默认Locale的货币格式器。也可以在调用该方法传入指定Locale,则获取指定Locale的

货币格式器。

    –getIntegerInstance:返回默认Locale的整数格式器。也可以在调用该方法传入指定Locale,则获取指定Locale的

整数格式器。

    –getNumberInstance:返回默认Locale的通用数值格式器。也可以在调用该方法传入指定Locale,则获取指定

Locale的通用数值格式器。

    –getPercentInstance:返回默认Locale的百分数格式器。也可以在调用该方法传入指定Locale,则获取指定Locale

的百分数格式器。

 


 

 

 使用DateFormat

 

 

  •与NumberFormat相似的是,DateFormat也是一个抽象类,它也提供了几个工厂方法用于获取DateFormat对象:

    –getDateInstance:返回一个日期格式器,它格式化后的字符串只有日期,没有时间。该方法可以传入多个参数,用于

指定日期样式和Locale等参数。如果不指定这些参数,则使用默认参数。

    –getTimeInstance:返回一个时间格式器,它格式化后的字符串只有时间,没有日期。该方法可以传入多个参数,用于

指定时间样式和Locale等参数。如果不指定这些参数,则使用默认参数。

    –getDateTimeInstance:返回一个日期、时间格式器,它格式化后的字符串既有日期,也有时间。该方法可以传入多

个参数,用于指定日期样式、时间样式和Locale等参数。如果不指定这些参数,则使用默认参数。

 


 

 

 使用SimpleDateFormat

 

 

  •SimpleDateFormat是DateFormat的子类,正如它的名字所暗示的,它是“简单”的日期格式器。很多读者对“简单”的日期

格式器不屑一顾,实际上SimpleDateFormat比DateFormat更简单、功能更强大。

  •SimpleDateFormat可以非常灵活地输格式化Date,也可以非常解析各种格式的日期字符串。创建SimpleDateFormat对

象时需要传入一个pattern字符串,这个pattern不是正则表达式,而是一个日期模板字符串。 

 


 

 

 Java 8新增的日期时间格式器

 

 

  •java.time.format包下提供了一个DateTimeFormatter格式器类。

  •为了使用DateTimeFormatter将日期、时间(LocalDate、LocalDateTime、LocalTime等实例)格式化为字符串,可

通过如下两种方式:

    –调用DateTimeFormatter的format(TemporalAccessor temporal)方法执行格式化,其中LocalDate、

LocalDateTime、LocalTime等类都是TemporalAccessor接口的实现类。

    –调用LocalDate、LocalDateTime、LocalTime等日期、时间对象的format(DateTimeFormatter formatter)方

法执行格式化。

  •为了使用DateTimeFormatter将指定格式的字符串解析成日期、时间对象(LocalDate、LocalDateTime、LocalTime

等实例),可通过如下日期、时间对象提供的parse(CharSequence text, DateTimeFormatter formatter)方法进行解析。 

 


 

 

 下面贴出知识点的相关代码:

 

public class ArgsTest {
    public static void main(String[] args) {
        // 输出args数组的长度
        System.out.println(args.length);
        // 遍历args数组的每个元素
        for (String arg : args) {
            System.out.println(arg);
        }
    }
}

 

 

 

 

 

public class KeyboardInTest {
    public static void main(String[] args) throws Exception {
        // 以System.in节点流为基础,创建一个BufferedReader对象
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String line = null;
        // 逐行读取键盘输入
        while ((line = br.readLine()) != null) {
            System.out.println("用户键盘输入是:" + line);
        }
    }
}

 

 

 

public class ScannerFileTest {
    public static void main(String[] args) throws Exception {
        // 将一个File对象作为Scanner的构造器参数,Scanner读取文件内容
        Scanner sc = new Scanner(new File("ScannerFileTest.java"));
        System.out.println("ScannerFileTest.java文件内容如下:");
        // 判断是否还有下一行
        while (sc.hasNextLine()) {
            // 输出文件中的下一行
            System.out.println(sc.nextLine());
        }
    }
}

 

 

 

public class ScannerKeyBoardTest {
    public static void main(String[] args) {
        // System.in代表标准输入,就是键盘输入
        Scanner sc = new Scanner(System.in);
        // 增加下面一行将只把回车作为分隔符
        // sc.useDelimiter("\n");
        // 判断是否还有下一个输入项
        while (sc.hasNext()) {
            // 输出输入项
            System.out.println("键盘输入的内容是:" + sc.next());
        }
    }
}

 

 

 

public class ScannerLongTest {
    public static void main(String[] args) {
        // System.in代表标准输入,就是键盘输入
        Scanner sc = new Scanner(System.in);
        // 判断是否还有下一个long型整数
        while (sc.hasNextLong()) {
            // 输出输入项
            System.out.println("键盘输入的内容是:" + sc.nextLong());
        }
    }
}

 


 

 

 

public class ExecTest {
    public static void main(String[] args) throws Exception {
        Runtime rt = Runtime.getRuntime();
        // 运行记事本程序
        rt.exec("notepad.exe");
    }
}

 

 

 

public class IdentityHashCodeTest {
    public static void main(String[] args) {
        // 下面程序中s1和s2是两个不同对象
        String s1 = new String("Hello");
        String s2 = new String("Hello");
        // String重写了hashCode()方法——改为根据字符序列计算hashCode值,
        // 因为s1和s2的字符序列相同,所以它们的hashCode方法返回值相同
        System.out.println(s1.hashCode() + "----" + s2.hashCode());
        // s1和s2是不同的字符串对象,所以它们的identityHashCode值不同
        System.out.println(System.identityHashCode(s1) + "----"
                + System.identityHashCode(s2));
        String s3 = "Java";
        String s4 = "Java";
        // s3和s4是相同的字符串对象,所以它们的identityHashCode值相同
        System.out.println(System.identityHashCode(s3) + "----"
                + System.identityHashCode(s4));
    }
}

 

 

 

 

public class RuntimeTest {
    public static void main(String[] args) {
        // 获取Java程序关联的运行时对象
        Runtime rt = Runtime.getRuntime();
        System.out.println("处理器数量:" + rt.availableProcessors());
        System.out.println("空闲内存数:" + rt.freeMemory());
        System.out.println("总内存数:" + rt.totalMemory());
        System.out.println("可用最大内存数:" + rt.maxMemory());
    }
}

 

 

 

 

public class SystemTest {
    public static void main(String[] args) throws Exception {
        // 获取系统所有的环境变量
        Map<String, String> env = System.getenv();
        for (String name : env.keySet()) {
            System.out.println(name + " ---> " + env.get(name));
        }
        // 获取指定环境变量的值
        System.out.println(System.getenv("JAVA_HOME"));
        // 获取所有的系统属性
        Properties props = System.getProperties();
        // 将所有系统属性保存到props.txt文件中
        props.store(new FileOutputStream("props.txt"), "System Properties");
        // 输出特定的系统属性
        System.out.println(System.getProperty("os.name"));
    }
}

 

 

 


 

 

public class Arith {
    // 默认除法运算精度
    private static final int DEF_DIV_SCALE = 10;

    // 构造器私有,让这个类不能实例化
    private Arith() {
    }

    // 提供精确的加法运算。
    public static double add(double v1, double v2) {
        BigDecimal b1 = BigDecimal.valueOf(v1);
        BigDecimal b2 = BigDecimal.valueOf(v2);
        return b1.add(b2).doubleValue();
    }

    // 提供精确的减法运算。
    public static double sub(double v1, double v2) {
        BigDecimal b1 = BigDecimal.valueOf(v1);
        BigDecimal b2 = BigDecimal.valueOf(v2);
        return b1.subtract(b2).doubleValue();
    }

    // 提供精确的乘法运算。
    public static double mul(double v1, double v2) {
        BigDecimal b1 = BigDecimal.valueOf(v1);
        BigDecimal b2 = BigDecimal.valueOf(v2);
        return b1.multiply(b2).doubleValue();
    }

    // 提供(相对)精确的除法运算,当发生除不尽的情况时.
    // 精确到小数点以后10位的数字四舍五入。
    public static double div(double v1, double v2) {
        BigDecimal b1 = BigDecimal.valueOf(v1);
        BigDecimal b2 = BigDecimal.valueOf(v2);
        return b1.divide(b2, DEF_DIV_SCALE, BigDecimal.ROUND_HALF_UP)
                .doubleValue();
    }

    public static void main(String[] args) {
        System.out.println("0.05 + 0.01 = " + Arith.add(0.05, 0.01));
        System.out.println("1.0 - 0.42 = " + Arith.sub(1.0, 0.42));
        System.out.println("4.015 * 100 = " + Arith.mul(4.015, 100));
        System.out.println("123.3 / 100 = " + Arith.div(123.3, 100));
    }
}

 

 

 

 1 public class BigDecimalTest {
 2     public static void main(String[] args) {
 3         BigDecimal f1 = new BigDecimal("0.05");
 4         BigDecimal f2 = BigDecimal.valueOf(0.01);
 5         BigDecimal f3 = new BigDecimal(0.05);
 6         System.out.println("使用String作为BigDecimal构造器参数:");
 7         System.out.println("0.05 + 0.01 = " + f1.add(f2));
 8         System.out.println("0.05 - 0.01 = " + f1.subtract(f2));
 9         System.out.println("0.05 * 0.01 = " + f1.multiply(f2));
10         System.out.println("0.05 / 0.01 = " + f1.divide(f2));
11         System.out.println("使用double作为BigDecimal构造器参数:");
12         System.out.println("0.05 + 0.01 = " + f3.add(f2));
13         System.out.println("0.05 - 0.01 = " + f3.subtract(f2));
14         System.out.println("0.05 * 0.01 = " + f3.multiply(f2));
15         System.out.println("0.05 / 0.01 = " + f3.divide(f2));
16     }
17 /**
18 * 使用String作为BigDecimal构造器参数:
19 0.05 + 0.01 = 0.06
20 0.05 - 0.01 = 0.04
21 0.05 * 0.01 = 0.0005
22 0.05 / 0.01 = 5
23 使用double作为BigDecimal构造器参数:
24 0.05 + 0.01 = 0.06000000000000000277555756156289135105907917022705078125
25 0.05 - 0.01 = 0.04000000000000000277555756156289135105907917022705078125
26 0.05 * 0.01 = 0.0005000000000000000277555756156289135105907917022705078125
27 0.05 / 0.01 = 5.000000000000000277555756156289135105907917022705078125
28 
29      */
30 }
BigDecimalTest
 1 class Address {
 2     String detail;
 3 
 4     public Address(String detail) {
 5         this.detail = detail;
 6     }
 7 }
 8 
 9 // 实现Cloneable接口
10 class User implements Cloneable {
11     int age;
12     Address address;
13 
14     public User(int age) {
15         this.age = age;
16         address = new Address("广州天河");
17     }
18 
19     // 通过调用super.clone()来实现clone()方法
20     public User clone() throws CloneNotSupportedException {
21         return (User) super.clone();
22     }
23 }
24 
25 public class CloneTest {
26     public static void main(String[] args) throws CloneNotSupportedException {
27         User u1 = new User(29);
28         // clone得到u1对象的副本。
29         User u2 = u1.clone();
30         // 判断u1、u2是否相同
31         System.out.println(u1 == u2); //32         // 判断u1、u2的address是否相同
33         System.out.println(u1.address == u2.address); //
34     }
35     /*
36      * 结果为: false true
37      */
38 }
CloneTest
public class DoubleTest {
    public static void main(String args[]) {
        System.out.println("0.05 + 0.01 = " + (0.05 + 0.01));
        System.out.println("1.0 - 0.42 = " + (1.0 - 0.42));
        System.out.println("4.015 * 100 = " + (4.015 * 100));
        System.out.println("123.3 / 100 = " + (123.3 / 100));
    }
    /*
     * 0.05 + 0.01 = 0.060000000000000005 
     * 1.0 - 0.42 = 0.5800000000000001 
     * 4.015 * 100 = 401.49999999999994
     *  123.3 / 100 = 1.2329999999999999
     */
}
DoubleTest
public class MathTest {
    public static void main(String[] args) {
        /*---------下面是三角运算---------*/
        // 将弧度转换角度
        System.out.println("Math.toDegrees(1.57):" + Math.toDegrees(1.57));
        // 将角度转换为弧度
        System.out.println("Math.toRadians(90):" + Math.toRadians(90));
        // 计算反余弦,返回的角度范围在 0.0 到 pi 之间。
        System.out.println("Math.acos(1.2):" + Math.acos(1.2));
        // 计算反正弦;返回的角度范围在 -pi/2 到 pi/2 之间。
        System.out.println("Math.asin(0.8):" + Math.asin(0.8));
        // 计算反正切;返回的角度范围在 -pi/2 到 pi/2 之间。
        System.out.println("Math.atan(2.3):" + Math.atan(2.3));
        // 计算三角余弦。
        System.out.println("Math.cos(1.57):" + Math.cos(1.57));
        // 计算值的双曲余弦。
        System.out.println("Math.cosh(1.2 ):" + Math.cosh(1.2));
        // 计算正弦
        System.out.println("Math.sin(1.57 ):" + Math.sin(1.57));
        // 计算双曲正弦
        System.out.println("Math.sinh(1.2 ):" + Math.sinh(1.2));
        // 计算三角正切
        System.out.println("Math.tan(0.8 ):" + Math.tan(0.8));
        // 计算双曲正切
        System.out.println("Math.tanh(2.1 ):" + Math.tanh(2.1));
        // 将矩形坐标 (x, y) 转换成极坐标 (r, thet));
        System.out.println("Math.atan2(0.1, 0.2):" + Math.atan2(0.1, 0.2));
        /*---------下面是取整运算---------*/
        // 取整,返回小于目标数的最大整数。
        System.out.println("Math.floor(-1.2 ):" + Math.floor(-1.2));
        // 取整,返回大于目标数的最小整数。
        System.out.println("Math.ceil(1.2):" + Math.ceil(1.2));
        // 四舍五入取整
        System.out.println("Math.round(2.3 ):" + Math.round(2.3));
        /*---------下面是乘方、开方、指数运算---------*/
        // 计算平方根。
        System.out.println("Math.sqrt(2.3 ):" + Math.sqrt(2.3));
        // 计算立方根。
        System.out.println("Math.cbrt(9):" + Math.cbrt(9));
        // 返回欧拉数 e 的n次幂。
        System.out.println("Math.exp(2):" + Math.exp(2));
        // 返回 sqrt(x2 +y2)
        System.out.println("Math.hypot(4 , 4):" + Math.hypot(4, 4));
        // 按照 IEEE 754 标准的规定,对两个参数进行余数运算。
        System.out.println("Math.IEEEremainder(5 , 2):"
                + Math.IEEEremainder(5, 2));
        // 计算乘方
        System.out.println("Math.pow(3, 2):" + Math.pow(3, 2));
        // 计算自然对数
        System.out.println("Math.log(12):" + Math.log(12));
        // 计算底数为 10 的对数。
        System.out.println("Math.log10(9):" + Math.log10(9));
        // 返回参数与 1 之和的自然对数。
        System.out.println("Math.log1p(9):" + Math.log1p(9));
        /*---------下面是符号相关的运算---------*/
        // 计算绝对值。
        System.out.println("Math.abs(-4.5):" + Math.abs(-4.5));
        // 符号赋值,返回带有第二个浮点数符号的第一个浮点参数。
        System.out.println("Math.copySign(1.2, -1.0):"
                + Math.copySign(1.2, -1.0));
        // 符号函数;如果参数为 0,则返回 0;如果参数大于 0,
        // 则返回 1.0;如果参数小于 0,则返回 -1.0。
        System.out.println("Math.signum(2.3):" + Math.signum(2.3));
        /*---------下面是大小相关的运算---------*/
        // 找出最大值
        System.out.println("Math.max(2.3 , 4.5):" + Math.max(2.3, 4.5));
        // 计算最小值
        System.out.println("Math.min(1.2 , 3.4):" + Math.min(1.2, 3.4));
        // 返回第一个参数和第二个参数之间与第一个参数相邻的浮点数。
        System.out.println("Math.nextAfter(1.2, 1.0):"
                + Math.nextAfter(1.2, 1.0));
        // 返回比目标数略大的浮点数
        System.out.println("Math.nextUp(1.2 ):" + Math.nextUp(1.2));
        // 返回一个伪随机数,该值大于等于 0.0 且小于 1.0。
        System.out.println("Math.random():" + Math.random());
    }
    /*
     * Math.toDegrees(1.57):89.95437383553926
Math.toRadians(90):1.5707963267948966
Math.acos(1.2):NaN
Math.asin(0.8):0.9272952180016123
Math.atan(2.3):1.1606689862534056
Math.cos(1.57):7.963267107332633E-4
Math.cosh(1.2 ):1.8106555673243747
Math.sin(1.57 ):0.9999996829318346
Math.sinh(1.2 ):1.5094613554121725
Math.tan(0.8 ):1.0296385570503641
Math.tanh(2.1 ):0.9704519366134539
Math.atan2(0.1, 0.2):0.4636476090008061
Math.floor(-1.2 ):-2.0
Math.ceil(1.2):2.0
Math.round(2.3 ):2
Math.sqrt(2.3 ):1.51657508881031
Math.cbrt(9):2.080083823051904
Math.exp(2):7.38905609893065
Math.hypot(4 , 4):5.656854249492381
Math.IEEEremainder(5 , 2):1.0
Math.pow(3, 2):9.0
Math.log(12):2.4849066497880004
Math.log10(9):0.9542425094393249
Math.log1p(9):2.302585092994046
Math.abs(-4.5):4.5
Math.copySign(1.2, -1.0):-1.2
Math.signum(2.3):1.0
Math.max(2.3 , 4.5):4.5
Math.min(1.2 , 3.4):1.2
Math.nextAfter(1.2, 1.0):1.1999999999999997
Math.nextUp(1.2 ):1.2000000000000002
Math.random():0.11190960130735261

     */
}
MathTest
public class ObjectsTest {
    // 定义一个obj变量,它的默认值是null
    static ObjectsTest obj;

    public static void main(String[] args) {
        // 输出一个null对象的hashCode值,输出0
        System.out.println(Objects.hashCode(obj));
        // 输出一个null对象的toString,输出null
        System.out.println(Objects.toString(obj));
        // 要求obj不能为null,如果obj为null则引发异常
        System.out.println(Objects.requireNonNull(obj, "obj参数不能是null!"));
    }
    /*
     * 0
    null
Exception in thread "main" java.lang.NullPointerException: obj参数不能是null!
    at java.util.Objects.requireNonNull(Objects.java:226)
    at ObjectsTest.main(ObjectsTest.java:24)

     */
}
ObjectsTest
public class RandomTest {
    public static void main(String[] args) {
        Random rand = new Random();
        System.out.println("rand.nextBoolean():" + rand.nextBoolean());
        byte[] buffer = new byte[16];
        rand.nextBytes(buffer);
        System.out.println(Arrays.toString(buffer));
        // 生成0.0~1.0之间的伪随机double数
        System.out.println("rand.nextDouble():" + rand.nextDouble());
        // 生成0.0~1.0之间的伪随机float数
        System.out.println("rand.nextFloat():" + rand.nextFloat());
        // 生成平均值是 0.0,标准差是 1.0的伪高斯数
        System.out.println("rand.nextGaussian():" + rand.nextGaussian());
        // 生成一个处于int整数取值范围的伪随机整数
        System.out.println("rand.nextInt():" + rand.nextInt());
        // 生成0~26之间的伪随机整数
        System.out.println("rand.nextInt(26):" + rand.nextInt(26));
        // 生成一个处于long整数取值范围的伪随机整数
        System.out.println("rand.nextLong():" + rand.nextLong());
    }
    /*
     * rand.nextBoolean():true
[-36, 39, 30, -22, -78, -15, -48, -41, -91, -88, -127, -113, 47, 66, -47, 38]
rand.nextDouble():0.40815483428193944
rand.nextFloat():0.4410478
rand.nextGaussian():0.013423670838707597
rand.nextInt():1237723304
rand.nextInt(26):23
rand.nextLong():-4409740969692108703

     */
}
RandomTest
public class SeedTest {
    public static void main(String[] args) {
        Random r1 = new Random(50);
        System.out.println("第一个种子为50的Random对象");
        System.out.println("r1.nextBoolean():\t" + r1.nextBoolean());
        System.out.println("r1.nextInt():\t\t" + r1.nextInt());
        System.out.println("r1.nextDouble():\t" + r1.nextDouble());
        System.out.println("r1.nextGaussian():\t" + r1.nextGaussian());
        System.out.println("---------------------------");
        Random r2 = new Random(50);
        System.out.println("第二个种子为50的Random对象");
        System.out.println("r2.nextBoolean():\t" + r2.nextBoolean());
        System.out.println("r2.nextInt():\t\t" + r2.nextInt());
        System.out.println("r2.nextDouble():\t" + r2.nextDouble());
        System.out.println("r2.nextGaussian():\t" + r2.nextGaussian());
        System.out.println("---------------------------");
        Random r3 = new Random(100);
        System.out.println("种子为100的Random对象");
        System.out.println("r3.nextBoolean():\t" + r3.nextBoolean());
        System.out.println("r3.nextInt():\t\t" + r3.nextInt());
        System.out.println("r3.nextDouble():\t" + r3.nextDouble());
        System.out.println("r3.nextGaussian():\t" + r3.nextGaussian());
    }
    /*
     * 第一个种子为50的Random对象
r1.nextBoolean():    true
r1.nextInt():        -1727040520
r1.nextDouble():    0.6141579720626675
r1.nextGaussian():    2.377650302287946
---------------------------
第二个种子为50的Random对象
r2.nextBoolean():    true
r2.nextInt():        -1727040520
r2.nextDouble():    0.6141579720626675
r2.nextGaussian():    2.377650302287946
---------------------------
种子为100的Random对象
r3.nextBoolean():    true
r3.nextInt():        -1139614796
r3.nextDouble():    0.19497605734770518
r3.nextGaussian():    0.6762208162903859

     */
}
SeedTest
public class StringBuilderTest {
    public static void main(String[] args) {
        StringBuilder sb = new StringBuilder();
        // 追加字符串
        sb.append("java");// sb = "java"
        // 插入
        sb.insert(0, "hello "); // sb="hello java"
        // 替换
        sb.replace(5, 6, ","); // sb="hello, java"
        // 删除
        sb.delete(5, 6); // sb="hellojava"
        System.out.println(sb);
        // 反转
        sb.reverse(); // sb="avajolleh"
        System.out.println(sb);
        System.out.println(sb.length()); // 输出9
        System.out.println(sb.capacity()); // 输出16
        // 改变StringBuilder的长度,将只保留前面部分
        sb.setLength(5); // sb="avajo"
        System.out.println(sb);
    }
/*
 * hellojava
avajolleh
9
16
avajo
    
 */
}
StringBuilderTest

 


 

 

public class CalendarTest {
    public static void main(String[] args) {
        Calendar c = Calendar.getInstance();
        // 取出年
        System.out.println(c.get(YEAR));
        // 取出月份
        System.out.println(c.get(MONTH));
        // 取出日
        System.out.println(c.get(DATE));
        // 分别设置年、月、日、小时、分钟、秒
        c.set(2003, 10, 23, 12, 32, 23); // 2003-11-23 12:32:23
        System.out.println(c.getTime());
        // 将Calendar的年前推1年
        c.add(YEAR, -1); // 2002-11-23 12:32:23
        System.out.println(c.getTime());
        // 将Calendar的月前推8个月
        c.roll(MONTH, -8); // 2002-03-23 12:32:23
        System.out.println(c.getTime());

        Calendar cal1 = Calendar.getInstance();
        cal1.set(2003, 7, 23, 0, 0, 0); // 2003-8-23
        cal1.add(MONTH, 6); // 2003-8-23 => 2004-2-23
        System.out.println(cal1.getTime());

        Calendar cal2 = Calendar.getInstance();
        cal2.set(2003, 7, 31, 0, 0, 0); // 2003-8-31
        // 因为进位到后月份改为2月,2月没有31日,自动变成29日
        cal2.add(MONTH, 6); // 2003-8-31 => 2004-2-29
        System.out.println(cal2.getTime());

        Calendar cal3 = Calendar.getInstance();
        cal3.set(2003, 7, 23, 0, 0, 0); // 2003-8-23
        // MONTH字段“进位”,但YEAR字段并不增加
        cal3.roll(MONTH, 6); // 2003-8-23 => 2003-2-23
        System.out.println(cal3.getTime());

        Calendar cal4 = Calendar.getInstance();
        cal4.set(2003, 7, 31, 0, 0, 0); // 2003-8-31
        // MONTH字段“进位”后变成2,2月没有31日,
        // YEAR字段不会改变,2003年2月只有28天
        cal4.roll(MONTH, 6); // 2003-8-31 => 2003-2-28
        System.out.println(cal4.getTime());
    }
    /*
     * 2016
3
15
Sun Nov 23 12:32:23 CST 2003
Sat Nov 23 12:32:23 CST 2002
Sat Mar 23 12:32:23 CST 2002
Mon Feb 23 00:00:00 CST 2004
Sun Feb 29 00:00:00 CST 2004
Sun Feb 23 00:00:00 CST 2003
Fri Feb 28 00:00:00 CST 2003

     */
}
CalendarTest
public class public class DateTest {
    public static void main(String[] args) {
        Date d1 = new Date();
        // 获取当前时间之后100ms的时间
        Date d2 = new Date(System.currentTimeMillis() + 100);
        System.out.println(d2);
        System.out.println(d1.compareTo(d2));
        System.out.println(d1.before(d2));
    }
    /*
     * Fri Apr 15 00:36:09 CST 2016
-1
true

     */
}
{
    public static void main(String[] args) {
        Date d1 = new Date();
        // 获取当前时间之后100ms的时间
        Date d2 = new Date(System.currentTimeMillis() + 100);
        System.out.println(d2);
        System.out.println(d1.compareTo(d2));
        System.out.println(d1.before(d2));
    }
    /*
     * Fri Apr 15 00:36:09 CST 2016
-1
true

     */
}
DateTest
public class LazyTest {
    public static void main(String[] args) {
        Calendar cal = Calendar.getInstance();
        cal.set(2003, 7, 31); // 2003-8-31
        // 将月份设为9,但9月31日不存在。
        // 如果立即修改,系统将会把cal自动调整到10月1日。
        cal.set(MONTH, 8);
        // 下面代码输出10月1日
        // System.out.println(cal.getTime()); //// 设置DATE字段为5
        cal.set(DATE, 5); //
        System.out.println(cal.getTime()); //
    }
    /*
     * Fri Sep 05 00:37:31 CST 2003

     */
}
LazyTest
public class LenientTest {
    public static void main(String[] args) {
        Calendar cal = Calendar.getInstance();
        // 结果是YEAR字段加1,MONTH字段为1(二月)
        cal.set(MONTH, 13); //
        System.out.println(cal.getTime());
        // 关闭容错性
        cal.setLenient(false);
        // 导致运行时异常
        cal.set(MONTH, 13); //
        System.out.println(cal.getTime());
    }
    /*
     * Wed Feb 15 00:38:49 CST 2017
Exception in thread "main" java.lang.IllegalArgumentException: MONTH
    at java.util.GregorianCalendar.computeTime(GregorianCalendar.java:2583)
    at java.util.Calendar.updateTime(Calendar.java:2606)
    at java.util.Calendar.getTimeInMillis(Calendar.java:1118)
    at java.util.Calendar.getTime(Calendar.java:1091)
    at LenientTest.main(LenientTest.java:25)

     */
}
LenientTest
public class NewDatePackageTest {
    public static void main(String[] args) {
        // -----下面是关于Clock的用法-----
        // 获取当前Clock
        Clock clock = Clock.systemUTC();
        // 通过Clock获取当前时刻
        System.out.println("当前时刻为:" + clock.instant());
        // 获取clock对应的毫秒数,与System.currentTimeMillis()输出相同
        System.out.println(clock.millis());
        System.out.println(System.currentTimeMillis());
        // -----下面是关于Duration的用法-----
        Duration d = Duration.ofSeconds(6000);
        System.out.println("6000秒相当于" + d.toMinutes() + "分");
        System.out.println("6000秒相当于" + d.toHours() + "小时");
        System.out.println("6000秒相当于" + d.toDays() + "天");
        // 在clock基础上增加6000秒,返回新的Clock
        Clock clock2 = Clock.offset(clock, d);
        // 可看到clock2与clock1相差1小时40分
        System.out.println("当前时刻加6000秒为:" + clock2.instant());
        // -----下面是关于Instant的用法-----
        // 获取当前时间
        Instant instant = Instant.now();
        System.out.println(instant);
        // instant添加6000秒(即100分钟),返回新的Instant
        Instant instant2 = instant.plusSeconds(6000);
        System.out.println(instant2);
        // 根据字符串中解析Instant对象
        Instant instant3 = Instant.parse("2014-02-23T10:12:35.342Z");
        System.out.println(instant3);
        // 在instant3的基础上添加5小时4分钟
        Instant instant4 = instant3.plus(Duration.ofHours(5).plusMinutes(4));
        System.out.println(instant4);
        // 获取instant4的5天以前的时刻
        Instant instant5 = instant4.minus(Duration.ofDays(5));
        System.out.println(instant5);
        // -----下面是关于LocalDate的用法-----
        LocalDate localDate = LocalDate.now();
        System.out.println(localDate);
        // 获得2014年的第146天
        localDate = LocalDate.ofYearDay(2014, 146);
        System.out.println(localDate); // 2014-05-26
        // 设置为2014年5月21日
        localDate = LocalDate.of(2014, Month.MAY, 21);
        System.out.println(localDate); // 2014-05-21
        // -----下面是关于LocalTime的用法-----
        // 获取当前时间
        LocalTime localTime = LocalTime.now();
        // 设置为22点33分
        localTime = LocalTime.of(22, 33);
        System.out.println(localTime); // 22:33
        // 返回一天中的第5503秒
        localTime = LocalTime.ofSecondOfDay(5503);
        System.out.println(localTime); // 01:31:43
        // -----下面是关于localDateTime的用法-----
        // 获取当前日期、时间
        LocalDateTime localDateTime = LocalDateTime.now();
        // 当前日期、时间加上25小时3分钟
        LocalDateTime future = localDateTime.plusHours(25).plusMinutes(3);
        System.out.println("当前日期、时间的25小时3分之后:" + future);
        // 下面是关于Year、YearMonth、MonthDay的用法示例-----
        Year year = Year.now(); // 获取当前的年份
        System.out.println("当前年份:" + year); // 输出当前年份
        year = year.plusYears(5); // 当前年份再加5年
        System.out.println("当前年份再过5年:" + year);
        // 根据指定月份获取YearMonth
        YearMonth ym = year.atMonth(10);
        System.out.println("year年10月:" + ym); // 输出XXXX-10,XXXX代表当前年份
        // 当前年月再加5年,减3个月
        ym = ym.plusYears(5).minusMonths(3);
        System.out.println("year年10月再加5年、减3个月:" + ym);
        MonthDay md = MonthDay.now();
        System.out.println("当前月日:" + md); // 输出--XX-XX,代表几月几日
        // 设置为5月23日
        MonthDay md2 = md.with(Month.MAY).withDayOfMonth(23);
        System.out.println("5月23日为:" + md2); // 输出--05-23
    }
}
NewDatePackageTest
public class TimeZoneTest {
    public static void main(String[] args) {
        // 取得Java所支持的所有时区ID
        String[] ids = TimeZone.getAvailableIDs();
        System.out.println(Arrays.toString(ids));
        TimeZone my = TimeZone.getDefault();
        // 获取系统默认时区的ID:Asia/Shanghai
        System.out.println(my.getID());
        // 获取系统默认时区的名称:中国标准时间
        System.out.println(my.getDisplayName());
        // 获取指定ID的时区的名称:纽芬兰标准时间
        System.out.println(TimeZone.getTimeZone("CNT").getDisplayName());
    }
    /*
     * [Etc/GMT+12, Etc/GMT+11, Pacific/Midway, Pacific/Niue, Pacific/Pago_Pago, Pacific/Samoa, US/Samoa, America/Adak, America/Atka, Etc/GMT+10, HST, Pacific/Honolulu, Pacific/Johnston, Pacific/Rarotonga, Pacific/Tahiti, SystemV/HST10, US/Aleutian, US/Hawaii, Pacific/Marquesas, AST, America/Anchorage, America/Juneau, America/Nome, America/Sitka, America/Yakutat, Etc/GMT+9, Pacific/Gambier, SystemV/YST9, SystemV/YST9YDT, US/Alaska, America/Dawson, America/Ensenada, America/Los_Angeles, America/Metlakatla, America/Santa_Isabel, America/Tijuana, America/Vancouver, America/Whitehorse, Canada/Pacific, Canada/Yukon, Etc/GMT+8, Mexico/BajaNorte, PST, PST8PDT, Pacific/Pitcairn, SystemV/PST8, SystemV/PST8PDT, US/Pacific, US/Pacific-New, America/Boise, America/Cambridge_Bay, America/Chihuahua, America/Creston, America/Dawson_Creek, America/Denver, America/Edmonton, America/Hermosillo, America/Inuvik, America/Mazatlan, America/Ojinaga, America/Phoenix, America/Shiprock, America/Yellowknife, Canada/Mountain, Etc/GMT+7, MST, MST7MDT, Mexico/BajaSur, Navajo, PNT, SystemV/MST7, SystemV/MST7MDT, US/Arizona, US/Mountain, America/Bahia_Banderas, America/Belize, America/Cancun, America/Chicago, America/Costa_Rica, America/El_Salvador, America/Guatemala, America/Indiana/Knox, America/Indiana/Tell_City, America/Knox_IN, America/Managua, America/Matamoros, America/Menominee, America/Merida, America/Mexico_City, America/Monterrey, America/North_Dakota/Beulah, America/North_Dakota/Center, America/North_Dakota/New_Salem, America/Rainy_River, America/Rankin_Inlet, America/Regina, America/Resolute, America/Swift_Current, America/Tegucigalpa, America/Winnipeg, CST, CST6CDT, Canada/Central, Canada/East-Saskatchewan, Canada/Saskatchewan, Chile/EasterIsland, Etc/GMT+6, Mexico/General, Pacific/Easter, Pacific/Galapagos, SystemV/CST6, SystemV/CST6CDT, US/Central, US/Indiana-Starke, America/Atikokan, America/Bogota, America/Cayman, America/Coral_Harbour, America/Detroit, America/Eirunepe, America/Fort_Wayne, America/Guayaquil, America/Havana, America/Indiana/Indianapolis, America/Indiana/Marengo, America/Indiana/Petersburg, America/Indiana/Vevay, America/Indiana/Vincennes, America/Indiana/Winamac, America/Indianapolis, America/Iqaluit, America/Jamaica, America/Kentucky/Louisville, America/Kentucky/Monticello, America/Lima, America/Louisville, America/Montreal, America/Nassau, America/New_York, America/Nipigon, America/Panama, America/Pangnirtung, America/Port-au-Prince, America/Porto_Acre, America/Rio_Branco, America/Thunder_Bay, America/Toronto, Brazil/Acre, Canada/Eastern, Cuba, EST, EST5EDT, Etc/GMT+5, IET, Jamaica, SystemV/EST5, SystemV/EST5EDT, US/East-Indiana, US/Eastern, US/Michigan, America/Caracas, America/Anguilla, America/Antigua, America/Aruba, America/Asuncion, America/Barbados, America/Blanc-Sablon, America/Boa_Vista, America/Campo_Grande, America/Cuiaba, America/Curacao, America/Dominica, America/Glace_Bay, America/Goose_Bay, America/Grenada, America/Guadeloupe, America/Guyana, America/Halifax, America/Kralendijk, America/La_Paz, America/Lower_Princes, America/Manaus, America/Marigot, America/Martinique, America/Moncton, America/Montserrat, America/Port_of_Spain, America/Porto_Velho, America/Puerto_Rico, America/Santiago, America/Santo_Domingo, America/St_Barthelemy, America/St_Kitts, America/St_Lucia, America/St_Thomas, America/St_Vincent, America/Thule, America/Tortola, America/Virgin, Antarctica/Palmer, Atlantic/Bermuda, Brazil/West, Canada/Atlantic, Chile/Continental, Etc/GMT+4, PRT, SystemV/AST4, SystemV/AST4ADT, America/St_Johns, CNT, Canada/Newfoundland, AGT, America/Araguaina, America/Argentina/Buenos_Aires, America/Argentina/Catamarca, America/Argentina/ComodRivadavia, America/Argentina/Cordoba, America/Argentina/Jujuy, America/Argentina/La_Rioja, America/Argentina/Mendoza, America/Argentina/Rio_Gallegos, America/Argentina/Salta, America/Argentina/San_Juan, America/Argentina/San_Luis, America/Argentina/Tucuman, America/Argentina/Ushuaia, America/Bahia, America/Belem, America/Buenos_Aires, America/Catamarca, America/Cayenne, America/Cordoba, America/Fortaleza, America/Godthab, America/Jujuy, America/Maceio, America/Mendoza, America/Miquelon, America/Montevideo, America/Paramaribo, America/Recife, America/Rosario, America/Santarem, America/Sao_Paulo, Antarctica/Rothera, Atlantic/Stanley, BET, Brazil/East, Etc/GMT+3, America/Noronha, Atlantic/South_Georgia, Brazil/DeNoronha, Etc/GMT+2, America/Scoresbysund, Atlantic/Azores, Atlantic/Cape_Verde, Etc/GMT+1, Africa/Abidjan, Africa/Accra, Africa/Bamako, Africa/Banjul, Africa/Bissau, Africa/Casablanca, Africa/Conakry, Africa/Dakar, Africa/El_Aaiun, Africa/Freetown, Africa/Lome, Africa/Monrovia, Africa/Nouakchott, Africa/Ouagadougou, Africa/Sao_Tome, Africa/Timbuktu, America/Danmarkshavn, Antarctica/Troll, Atlantic/Canary, Atlantic/Faeroe, Atlantic/Faroe, Atlantic/Madeira, Atlantic/Reykjavik, Atlantic/St_Helena, Eire, Etc/GMT, Etc/GMT+0, Etc/GMT-0, Etc/GMT0, Etc/Greenwich, Etc/UCT, Etc/UTC, Etc/Universal, Etc/Zulu, Europe/Belfast, Europe/Dublin, Europe/Guernsey, Europe/Isle_of_Man, Europe/Jersey, Europe/Lisbon, Europe/London, GB, GB-Eire, GMT, GMT0, Greenwich, Iceland, Portugal, UCT, UTC, Universal, WET, Zulu, Africa/Algiers, Africa/Bangui, Africa/Brazzaville, Africa/Ceuta, Africa/Douala, Africa/Kinshasa, Africa/Lagos, Africa/Libreville, Africa/Luanda, Africa/Malabo, Africa/Ndjamena, Africa/Niamey, Africa/Porto-Novo, Africa/Tunis, Africa/Windhoek, Arctic/Longyearbyen, Atlantic/Jan_Mayen, CET, ECT, Etc/GMT-1, Europe/Amsterdam, Europe/Andorra, Europe/Belgrade, Europe/Berlin, Europe/Bratislava, Europe/Brussels, Europe/Budapest, Europe/Busingen, Europe/Copenhagen, Europe/Gibraltar, Europe/Ljubljana, Europe/Luxembourg, Europe/Madrid, Europe/Malta, Europe/Monaco, Europe/Oslo, Europe/Paris, Europe/Podgorica, Europe/Prague, Europe/Rome, Europe/San_Marino, Europe/Sarajevo, Europe/Skopje, Europe/Stockholm, Europe/Tirane, Europe/Vaduz, Europe/Vatican, Europe/Vienna, Europe/Warsaw, Europe/Zagreb, Europe/Zurich, MET, Poland, ART, Africa/Blantyre, Africa/Bujumbura, Africa/Cairo, Africa/Gaborone, Africa/Harare, Africa/Johannesburg, Africa/Kigali, Africa/Lubumbashi, Africa/Lusaka, Africa/Maputo, Africa/Maseru, Africa/Mbabane, Africa/Tripoli, Asia/Amman, Asia/Beirut, Asia/Damascus, Asia/Gaza, Asia/Hebron, Asia/Istanbul, Asia/Jerusalem, Asia/Nicosia, Asia/Tel_Aviv, CAT, EET, Egypt, Etc/GMT-2, Europe/Athens, Europe/Bucharest, Europe/Chisinau, Europe/Helsinki, Europe/Istanbul, Europe/Kaliningrad, Europe/Kiev, Europe/Mariehamn, Europe/Nicosia, Europe/Riga, Europe/Sofia, Europe/Tallinn, Europe/Tiraspol, Europe/Uzhgorod, Europe/Vilnius, Europe/Zaporozhye, Israel, Libya, Turkey, Africa/Addis_Ababa, Africa/Asmara, Africa/Asmera, Africa/Dar_es_Salaam, Africa/Djibouti, Africa/Juba, Africa/Kampala, Africa/Khartoum, Africa/Mogadishu, Africa/Nairobi, Antarctica/Syowa, Asia/Aden, Asia/Baghdad, Asia/Bahrain, Asia/Kuwait, Asia/Qatar, Asia/Riyadh, EAT, Etc/GMT-3, Europe/Minsk, Europe/Moscow, Europe/Simferopol, Europe/Volgograd, Indian/Antananarivo, Indian/Comoro, Indian/Mayotte, W-SU, Asia/Riyadh87, Asia/Riyadh88, Asia/Riyadh89, Mideast/Riyadh87, Mideast/Riyadh88, Mideast/Riyadh89, Asia/Tehran, Iran, Asia/Baku, Asia/Dubai, Asia/Muscat, Asia/Tbilisi, Asia/Yerevan, Etc/GMT-4, Europe/Samara, Indian/Mahe, Indian/Mauritius, Indian/Reunion, NET, Asia/Kabul, Antarctica/Mawson, Asia/Aqtau, Asia/Aqtobe, Asia/Ashgabat, Asia/Ashkhabad, Asia/Dushanbe, Asia/Karachi, Asia/Oral, Asia/Samarkand, Asia/Tashkent, Asia/Yekaterinburg, Etc/GMT-5, Indian/Kerguelen, Indian/Maldives, PLT, Asia/Calcutta, Asia/Colombo, Asia/Kolkata, IST, Asia/Kathmandu, Asia/Katmandu, Antarctica/Vostok, Asia/Almaty, Asia/Bishkek, Asia/Dacca, Asia/Dhaka, Asia/Kashgar, Asia/Novosibirsk, Asia/Omsk, Asia/Qyzylorda, Asia/Thimbu, Asia/Thimphu, Asia/Urumqi, BST, Etc/GMT-6, Indian/Chagos, Asia/Rangoon, Indian/Cocos, Antarctica/Davis, Asia/Bangkok, Asia/Ho_Chi_Minh, Asia/Hovd, Asia/Jakarta, Asia/Krasnoyarsk, Asia/Novokuznetsk, Asia/Phnom_Penh, Asia/Pontianak, Asia/Saigon, Asia/Vientiane, Etc/GMT-7, Indian/Christmas, VST, Antarctica/Casey, Asia/Brunei, Asia/Chita, Asia/Choibalsan, Asia/Chongqing, Asia/Chungking, Asia/Harbin, Asia/Hong_Kong, Asia/Irkutsk, Asia/Kuala_Lumpur, Asia/Kuching, Asia/Macao, Asia/Macau, Asia/Makassar, Asia/Manila, Asia/Shanghai, Asia/Singapore, Asia/Taipei, Asia/Ujung_Pandang, Asia/Ulaanbaatar, Asia/Ulan_Bator, Australia/Perth, Australia/West, CTT, Etc/GMT-8, Hongkong, PRC, Singapore, Australia/Eucla, Asia/Dili, Asia/Jayapura, Asia/Khandyga, Asia/Pyongyang, Asia/Seoul, Asia/Tokyo, Asia/Yakutsk, Etc/GMT-9, JST, Japan, Pacific/Palau, ROK, ACT, Australia/Adelaide, Australia/Broken_Hill, Australia/Darwin, Australia/North, Australia/South, Australia/Yancowinna, AET, Antarctica/DumontDUrville, Asia/Magadan, Asia/Sakhalin, Asia/Ust-Nera, Asia/Vladivostok, Australia/ACT, Australia/Brisbane, Australia/Canberra, Australia/Currie, Australia/Hobart, Australia/Lindeman, Australia/Melbourne, Australia/NSW, Australia/Queensland, Australia/Sydney, Australia/Tasmania, Australia/Victoria, Etc/GMT-10, Pacific/Chuuk, Pacific/Guam, Pacific/Port_Moresby, Pacific/Saipan, Pacific/Truk, Pacific/Yap, Australia/LHI, Australia/Lord_Howe, Antarctica/Macquarie, Asia/Srednekolymsk, Etc/GMT-11, Pacific/Efate, Pacific/Guadalcanal, Pacific/Kosrae, Pacific/Noumea, Pacific/Pohnpei, Pacific/Ponape, SST, Pacific/Norfolk, Antarctica/McMurdo, Antarctica/South_Pole, Asia/Anadyr, Asia/Kamchatka, Etc/GMT-12, Kwajalein, NST, NZ, Pacific/Auckland, Pacific/Fiji, Pacific/Funafuti, Pacific/Kwajalein, Pacific/Majuro, Pacific/Nauru, Pacific/Tarawa, Pacific/Wake, Pacific/Wallis, NZ-CHAT, Pacific/Chatham, Etc/GMT-13, MIT, Pacific/Apia, Pacific/Enderbury, Pacific/Fakaofo, Pacific/Tongatapu, Etc/GMT-14, Pacific/Kiritimati, Pacific/Bougainville, America/Grand_Turk]
Asia/Shanghai
中国标准时间
纽芬兰标准时间

     */
}
TimeZoneTest

 

 

public class FindGroup {
    public static void main(String[] args) {
        // 使用字符串模拟从网络上得到的网页源码
        String str = "我想求购一本《疯狂Java讲义》,尽快联系我13500006666"
                + "交朋友,电话号码是13611125565" + "出售二手电脑,联系方式15899903312";
        // 创建一个Pattern对象,并用它建立一个Matcher对象
        // 该正则表达式只抓取13X和15X段的手机号,
        // 实际要抓取哪些电话号码,只要修改正则表达式即可。
        Matcher m = Pattern.compile("((13\\d)|(15\\d))\\d{8}").matcher(str);
        // 将所有符合正则表达式的子串(电话号码)全部输出
        while (m.find()) {
            System.out.println(m.group());
        }
    }
}
FindGroup
public class MatchesTest {
    public static void main(String[] args) {
        String[] mails = { "kongyeeku@163.com", "kongyeeku@gmail.com",
                "ligang@crazyit.org", "wawa@abc.xx" };
        String mailRegEx = "\\w{3,20}@\\w+\\.(com|org|cn|net|gov)";
        Pattern mailPattern = Pattern.compile(mailRegEx);
        Matcher matcher = null;
        for (String mail : mails) {
            if (matcher == null) {
                matcher = mailPattern.matcher(mail);
            } else {
                matcher.reset(mail);
            }
            String result = mail + (matcher.matches() ? "是" : "不是")
                    + "一个有效的邮件地址!";
            System.out.println(result);
        }
    }
}
MatchesTest
public class ReplaceTest {
    public static void main(String[] args) {
        String[] msgs = { "Java has regular expressions in 1.4",
                "regular expressions now expressing in Java",
                "Java represses oracular expressions" };
        Pattern p = Pattern.compile(" re\\w*");
        Matcher matcher = null;
        for (int i = 0; i < msgs.length; i++) {
            if (matcher == null) {
                matcher = p.matcher(msgs[i]);
            } else {
                matcher.reset(msgs[i]);
            }
            System.out.println(matcher.replaceAll("哈哈:)"));
        }
    }
}
ReplaceTest
public class StartEnd {
    public static void main(String[] args) {
        // 创建一个Pattern对象,并用它建立一个Matcher对象
        String regStr = "Java is very easy!";
        System.out.println("目标字符串是:" + regStr);
        Matcher m = Pattern.compile("\\w+").matcher(regStr);
        while (m.find()) {
            System.out.println(m.group() + "子串的起始位置:" + m.start() + ",其结束位置:"
                    + m.end());
        }
    }
}
StartEnd
public class StringReg {
    public static void main(String[] args) {
        String[] msgs = { "Java has regular expressions in 1.4",
                "regular expressions now expressing in Java",
                "Java represses oracular expressions" };
        for (String msg : msgs) {
            System.out.println(msg.replaceFirst("re\\w*", "哈哈:)"));
            System.out.println(Arrays.toString(msg.split(" ")));
        }
    }
}
StringReg

 

 

 


 

 

public class DateFormatTest {
    public static void main(String[] args) throws ParseException {
        // 需要被格式化的时间
        Date dt = new Date();
        // 创建两个Locale,分别代表中国、美国
        Locale[] locales = { Locale.CHINA, Locale.US };
        DateFormat[] df = new DateFormat[16];
        // 为上面两个Locale创建16个DateFormat对象
        for (int i = 0; i < locales.length; i++) {
            df[i * 8] = DateFormat.getDateInstance(SHORT, locales[i]);
            df[i * 8 + 1] = DateFormat.getDateInstance(MEDIUM, locales[i]);
            df[i * 8 + 2] = DateFormat.getDateInstance(LONG, locales[i]);
            df[i * 8 + 3] = DateFormat.getDateInstance(FULL, locales[i]);
            df[i * 8 + 4] = DateFormat.getTimeInstance(SHORT, locales[i]);
            df[i * 8 + 5] = DateFormat.getTimeInstance(MEDIUM, locales[i]);
            df[i * 8 + 6] = DateFormat.getTimeInstance(LONG, locales[i]);
            df[i * 8 + 7] = DateFormat.getTimeInstance(FULL, locales[i]);
        }
        for (int i = 0; i < locales.length; i++) {
            String tip = i == 0 ? "----中国日期格式----" : "----美国日期格式----";
            System.out.println(tip);
            System.out.println("SHORT格式的日期格式:" + df[i * 8].format(dt));
            System.out.println("MEDIUM格式的日期格式:" + df[i * 8 + 1].format(dt));
            System.out.println("LONG格式的日期格式:" + df[i * 8 + 2].format(dt));
            System.out.println("FULL格式的日期格式:" + df[i * 8 + 3].format(dt));
            System.out.println("SHORT格式的时间格式:" + df[i * 8 + 4].format(dt));
            System.out.println("MEDIUM格式的时间格式:" + df[i * 8 + 5].format(dt));
            System.out.println("LONG格式的时间格式:" + df[i * 8 + 6].format(dt));
            System.out.println("FULL格式的时间格式:" + df[i * 8 + 7].format(dt));
        }

        String str1 = "2014-12-12";
        String str2 = "2014年12月10日";
        // 下面输出 Fri Dec 12 00:00:00 CST 2014
        System.out.println(DateFormat.getDateInstance().parse(str1));
        // 下面输出 Wed Dec 10 00:00:00 CST 2014
        System.out.println(DateFormat.getDateInstance(LONG).parse(str2));
        // 下面抛出 ParseException异常
        // System.out.println(DateFormat.getDateInstance().parse(str2));
    }
}
DateFormatTest
public class Hello {
    public static void main(String[] args) {
        // 取得系统默认的国家/语言环境
        Locale myLocale = Locale.getDefault(Locale.Category.FORMAT);
        // 根据指定国家/语言环境加载资源文件
        ResourceBundle bundle = ResourceBundle.getBundle("mess", myLocale);
        // 打印从资源文件中取得的消息
        System.out.println(bundle.getString("hello"));
    }
}
Hello
public class HelloArg {
    public static void main(String[] args) {
        // 定义一个Locale变量
        Locale currentLocale = null;
        // 如果运行程序的指定了两个参数
        if (args.length == 2) {
            // 使用运行程序的两个参数构造Locale实例
            currentLocale = new Locale(args[0], args[1]);
        } else {
            // 否则直接使用系统默认的Locale
            currentLocale = Locale.getDefault(Locale.Category.FORMAT);
        }
        // 根据Locale加载语言资源
        ResourceBundle bundle = ResourceBundle.getBundle("myMess",
                currentLocale);
        // 取得已加载的语言资源文件中msg对应消息
        String msg = bundle.getString("msg");
        // 使用MessageFormat为带占位符的字符串传入参数
        System.out.println(MessageFormat.format(msg, "yeeku", new Date()));
    }
}
HelloArg
public class LocaleList {
    public static void main(String[] args) {
        // 返回Java所支持的全部国家和语言的数组
        Locale[] localeList = Locale.getAvailableLocales();
        // 遍历数组的每个元素,依次获取所支持的国家和语言
        for (int i = 0; i < localeList.length; i++) {
            // 输出出所支持的国家和语言
            System.out.println(localeList[i].getDisplayCountry() + "="
                    + localeList[i].getCountry() + " "
                    + localeList[i].getDisplayLanguage() + "="
                    + localeList[i].getLanguage());
        }
    }
}
LocaleList
public class myMess_zh_CN extends ListResourceBundle {
    // 定义资源
    private final Object myData[][] = { { "msg", "{0},你好!今天的日期是{1}" } };

    // 重写方法getContents()
    public Object[][] getContents() {
        // 该方法返回资源的key-value对
        return myData;
    }
}
myMess_zh_CN
public class NumberFormatTest {
    public static void main(String[] args) {
        // 需要被格式化的数字
        double db = 1234000.567;
        // 创建四个Locale,分别代表中国、日本、德国、美国
        Locale[] locales = { Locale.CHINA, Locale.JAPAN, Locale.GERMAN,
                Locale.US };
        NumberFormat[] nf = new NumberFormat[12];
        // 为上面四个Locale创建12个NumberFormat对象
        // 每个Locale分别有通用数值格式器、百分比格式器、货币格式器
        for (int i = 0; i < locales.length; i++) {
            nf[i * 3] = NumberFormat.getNumberInstance(locales[i]);
            nf[i * 3 + 1] = NumberFormat.getPercentInstance(locales[i]);
            nf[i * 3 + 2] = NumberFormat.getCurrencyInstance(locales[i]);
        }
        for (int i = 0; i < locales.length; i++) {
            String tip = i == 0 ? "----中国的格式----" : i == 1 ? "----日本的格式----"
                    : i == 2 ? "----德国的格式----" : "----美国的格式----";
            System.out.println(tip);
            System.out.println("通用数值格式:" + nf[i * 3].format(db));
            System.out.println("百分比数值格式:" + nf[i * 3 + 1].format(db));
            System.out.println("货币数值格式:" + nf[i * 3 + 2].format(db));
        }
    }
}
NumberFormatTest
public class RawHello {
    public static void main(String[] args) {
        System.out.println("Hello World");
    }
}
RawHello
public class SimpleDateFormatTest {
    public static void main(String[] args) throws ParseException {
        Date d = new Date();
        // 创建一个SimpleDateFormat对象
        SimpleDateFormat sdf1 = new SimpleDateFormat("Gyyyy年中第D天");
        // 将d格式化成日期,输出:公元2014年中第101天
        String dateStr = sdf1.format(d);
        System.out.println(dateStr);
        // 一个非常特殊的日期字符串
        String str = "14###三月##21";
        SimpleDateFormat sdf2 = new SimpleDateFormat("y###MMM##d");
        // 将日期字符串解析成日期,输出:Fri Mar 21 00:00:00 CST 2014
        System.out.println(sdf2.parse(str));
    }
}
SimpleDateFormatTest

 

 

public class NewFormatterParse {
    public static void main(String[] args) {
        // 定义一个任意格式的日期时间字符串
        String str1 = "2014==04==12 01时06分09秒";
        // 根据需要解析的日期、时间字符串定义解析所用的格式器
        DateTimeFormatter fomatter1 = DateTimeFormatter
                .ofPattern("yyyy==MM==dd HH时mm分ss秒");
        // 执行解析
        LocalDateTime dt1 = LocalDateTime.parse(str1, fomatter1);
        System.out.println(dt1); // 输出 2014-04-12T01:06:09
        // ---下面代码再次解析另一个字符串---
        String str2 = "2014$$$四月$$$13 20小时";
        DateTimeFormatter fomatter2 = DateTimeFormatter
                .ofPattern("yyy$$$MMM$$$dd HH小时");
        LocalDateTime dt2 = LocalDateTime.parse(str2, fomatter2);
        System.out.println(dt2); // 输出 2014-04-13T20:00
    }
}
NewFormatterParse
public class NewFormatterTest {
    public static void main(String[] args) {
        DateTimeFormatter[] formatters = new DateTimeFormatter[] {
                // 直接使用常量创建DateTimeFormatter格式器
                DateTimeFormatter.ISO_LOCAL_DATE,
                DateTimeFormatter.ISO_LOCAL_TIME,
                DateTimeFormatter.ISO_LOCAL_DATE_TIME,
                // 使用本地化的不同风格来创建DateTimeFormatter格式器
                DateTimeFormatter.ofLocalizedDateTime(FormatStyle.FULL,
                        FormatStyle.MEDIUM),
                DateTimeFormatter.ofLocalizedTime(FormatStyle.LONG),
                // 根据模式字符串来创建DateTimeFormatter格式器
                DateTimeFormatter.ofPattern("Gyyyy%%MMM%%dd HH:mm:ss") };
        LocalDateTime date = LocalDateTime.now();
        // 依次使用不同的格式器对LocalDateTime进行格式化
        for (int i = 0; i < formatters.length; i++) {
            // 下面两行代码的作用相同
            System.out.println(date.format(formatters[i]));
            System.out.println(formatters[i].format(date));
        }
    }
}
NewFormatterTest

 

 

posted @ 2016-04-19 01:15  pipi-changing  阅读(288)  评论(0)    收藏  举报