Java8クラス
2016-04-18 21:35
621 查看
</pre><span style="font-size:14px">①StringBuilderクラス</span><p></p><pre name="code" class="java">package com.aegeanworkjp.API; public class StringBuilderClass { public static void main(String[] args) { // TODO Auto-generated method stub StringBuilder builder=new StringBuilder("Hello World!"); //容量数を確認 System.out.println("容量数:"+builder.capacity()); //データを回転させる System.out.println(builder.reverse()); builder.append("What a beautiful day!"); //ストリムを追加する System.out.println("The length of"+builder.toString()+":"+builder.length()); //ASCIIコード番号による文字追加 System.out.println(builder.appendCodePoint(65)); //insert(int offset,Object xxxx) System.out.println(builder.insert(builder.length(),"B")); //replace(int start,int end,String xxx) //0から始まり文字列の長さ−1までに書き換える System.out.println(builder.replace(0,12,"world").toString()+"----->「!dlroW olleH」's length:"+"!dlroW olleH".length()); //文字列削除 //delete(int start,int end) System.out.println(builder.delete(0, 5).toString()); //容量を実際の長さに縮小する builder.trimToSize(); System.out.println(builder.toString()); //指定文字削除 System.out.println(builder.deleteCharAt(builder.length()-1).toString()); //容量数を確認 System.out.println("容量数:"+builder.capacity()); } }Result
容量数:28 !dlroW olleH The length of!dlroW olleHWhat a beautiful day!:33 !dlroW olleHWhat a beautiful day!A !dlroW olleHWhat a beautiful day!AB worldWhat a beautiful day!AB----->「!dlroW olleH」's length:12 What a beautiful day!AB What a beautiful day!AB What a beautiful day!A 容量数:23
②ランダ式
③Predicateインタフェース
④LocalDateクラス、LocalTimeクラス
1、java.time.LocalDate:LocalDate是一个不可变的类,它表示默认格式(yyyy-MM-dd)的日期,我们可以使用now()方法得到当前时间,也可以提供输入年份、月份和日期的输入参数来创建一个LocalDate实例。
Example:
package com.aegeanworkjp.API; import java.time.*; public class LocalDateClass { public static void main(String[] args) { // TODO Auto-generated method stub LocalDate date=LocalDate.now(); System.out.println("Current Date:"+date); //of(int year,int month,int day) LocalDate date1=LocalDate.of(Year.now().getValue(), Month.MARCH, 20); System.out.println("Current Date1:"+date1); LocalDate date2=LocalDate.now(ZoneId.of("Asia/Tokyo")); System.out.println("Current Date by zone:"+date2); LocalDate date3=LocalDate.ofYearDay(Year.now().getValue(), 77); System.out.println("77th day of this year:"+date3); System.out.println("minus 44 day :"+date3.minusDays(44)); } }
Result:
Current Date:2016-04-19 Current Date1:2016-03-20 Current Date by zone:2016-04-19 77th day of this year:2016-03-17 minus 44 day :1972-03-17
2、java.time.LocalTime:LocalTime是一个不可变的类,它的实例代表一个符合人类可读格式的时间,默认格式是hh:mm:ss.zzz。像LocalDate一样,该类也提供了时区支持,同时也可以传入小时、分钟和秒等输入参数创建实例。
Example:
package com.aegeanworkjp.API; import java.time.*; public class LocalDateClass { public static void main(String[] args) { // TODO Auto-generated method stub LocalDate date=LocalDate.now(); System.out.println("Current Date:"+date); //of(int year,int month,int day) LocalDate date1=LocalDate.of(Year.now().getValue(), Month.MARCH, 20); System.out.println("Current Date1:"+date1); LocalDate date2=LocalDate.now(ZoneId.of("Asia/Tokyo")); System.out.println("Current Date by zone:"+date2); LocalDate date3=LocalDate.ofYearDay(Year.now().getValue(), 77); System.out.println("77th day of this year:"+date3); System.out.println("minus 44 day :"+date3.minusDays(44)); } }
Result:
Current Date:2016-04-19 Current Date1:2016-03-20 Current Date by zone:2016-04-19 77th day of this year:2016-03-17 minus 44 day :2016-02-02
3、java.time.LocalDateTime:LocalDateTime是一个不可变的日期-时间对象,它表示一组日期-时间,默认格式是yyyy-MM-dd-HH-mm-ss.zzz。它提供了一个工厂方法,接收LocalDate和LocalTime输入参数,创建LocalDateTime实例。
Example:
package com.aegeanworkjp.API; import java.time.LocalDate; import java.time.LocalDateTime; import java.time.LocalTime; import java.time.Month; import java.time.ZoneId; import java.time.ZoneOffset; public class LocalDateTimeClass { public static void main(String[] args) { // TODO Auto-generated method stub LocalDateTime today = LocalDateTime.now(); System.out.println("Current DateTime="+today); //of(int year,int month,int day,int hour,int minute,int second) today = LocalDateTime.of(LocalDate.now(), LocalTime.now()); System.out.println("Current DateTime="+today); LocalDateTime specificDate = LocalDateTime.of(LocalDate.now().getYear(), Month.JANUARY, 1, 10, 10, 30); System.out.println("Specific Date="+specificDate); //Current date in "Asia/Tokyo", you can get it from Tokyo LocalDateTime todayJapn = LocalDateTime.now(ZoneId.of("Asia/Tokyo")); System.out.println("Current Date in JST="+todayJapn); //Getting date from the base date i.e 01/01/1970 LocalDateTime dateFromBase = LocalDateTime.ofEpochSecond(10000, 0, ZoneOffset.UTC); System.out.println("10000th second time from 01/01/1970= "+dateFromBase); } }Result:
Current DateTime=2016-04-19T23:10:23.317 Current DateTime=2016-04-19T23:10:23.318 Specific Date=2016-01-01T10:10:30 Current Date in JST=2016-04-19T23:10:23.318 10000th second time from 01/01/1970= 1970-01-01T02:46:40
⑤Durationクラス
Example:
package com.aegeanworkjp.API; import java.time.Duration; import java.time.Instant; public class DurationClass { public static void main(String[] args) { Instant start = Instant.now(); Instant end = Instant.now(); Duration durate = Duration.between(start, end); long millis=durate.toMinutes(); System.out.println("Time:"+millis+" ZERO:"+durate.isZero()); System.out.println("Time plus:"+durate.plusMillis(2000).toNanos()); System.out.println(Duration.parse("P2DT3H4M").toMinutes()); } }
Result
Time:0 ZERO:true Time plus:2000000000 3064
⑥Periodクラス
⑦DateTimeFormatterクラス formatメソッド
内容 | 例 | 出力例 |
---|---|---|
パターン指定 | DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm:ss.SSS") | 2014/07/21 22:16:46.348 |
固定パターン | DateTimeFormatter.ISO_ZONED_DATE_TIME | 2014 95b6 -07-21T22:16:46.348+09:00[Asia/Tokyo] |
DateTimeFormatter.ISO_OFFSET_DATE_TIME | 2014-07-21T22:16:46.348+09:00 | |
DateTimeFormatter.ISO_OFFSET_DATE | 2014-07-21+09:00 | |
DateTimeFormatter.ISO_OFFSET_TIME | 22:16:46.348+09:00 | |
DateTimeFormatter.ISO_LOCAL_DATE_TIME | 2014-07-21T22:16:46.348 | |
DateTimeFormatter.ISO_LOCAL_DATE | 2014-07-21 | |
DateTimeFormatter.ISO_LOCAL_TIME | 22:16:46.348 |
package com.aegeanworkjp.API; import java.time.*; import java.time.format.DateTimeFormatter; import java.time.format.FormatStyle; public class DateTimeFormatterInterface { public static void main(String[] args) { //DateTimeFormatter.ofLocalizedDate(FormatStyle dateStyle) DateTimeFormatter format=DateTimeFormatter.ofLocalizedDate(FormatStyle.FULL); DateTimeFormatter format2=DateTimeFormatter.ofLocalizedDate(FormatStyle.LONG); DateTimeFormatter format3=DateTimeFormatter.ofLocalizedDate(FormatStyle.MEDIUM); DateTimeFormatter format4=DateTimeFormatter.ofLocalizedDate(FormatStyle.SHORT); System.out.println("format1:"+format.format(ZonedDateTime.now())); System.out.println("format2:"+format2.format(ZonedDateTime.now())); System.out.println("format3:"+format3.format(ZonedDateTime.now())); System.out.println("format4:"+format4.format(ZonedDateTime.now())); DateTimeFormatter format5 = DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm:ss.SSSxxxxx VV"); System.out.println("format5:"+format5.format(ZonedDateTime.now())); DateTimeFormatter format6 = DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm:ss"); String text = "2016/04/19 23:36:41"; System.out.println("format6:"+format6.parse(text)); } }
Result:
format1:2016年4月19日 format2:2016/04/19 format3:2016/04/19 format4:16/04/19 format5:2016/04/19 23:38:58.442+09:00 Asia/Tokyo format6:{},ISO resolved to 2016-04-19T23:36:41
⑧ArrayListクラス
相关文章推荐
- java对世界各个时区(TimeZone)的通用转换处理方法(转载)
- java-注解annotation
- java-模拟tomcat服务器
- java-用HttpURLConnection发送Http请求.
- java-WEB中的监听器Lisener
- Android IPC进程间通讯机制
- Android Native 绘图方法
- Android java 与 javascript互访(相互调用)的方法例子
- 介绍一款信息管理系统的开源框架---jeecg
- 聚类算法之kmeans算法java版本
- java实现 PageRank算法
- PropertyChangeListener简单理解
- c++11 + SDL2 + ffmpeg +OpenAL + java = Android播放器
- 插入排序
- 冒泡排序
- 堆排序
- 快速排序
- 二叉查找树