IDEA2023.1.3破解,IDEA破解,IDEA 2023.1破解,最新IDEA激活码

Java8中 LocalDate使用,Java8 实例介绍LocalDate,LocalDate 教程

IDEA2023.1.3破解,IDEA破解,IDEA 2023.1破解,最新IDEA激活码

1、计算时间间隔

Java8之前,我们想要确定一个方法的运行时间长度,可以这样:

long start = System.currentTimeMillis();
doSomething();
long end = System.currentTimeMillis();
System.out.println(end-start);

Java8中,可以这样

Instant start = Instant.now();
doSomething();
Instant end = Instant.now();
Duration time = Duration.between(start, end);
long seconds = time.getSeconds();//秒表示 
long millis = time.toMillis();//毫秒表示
System.out.println(seconds);
System.out.println(millis);

可以轻松选择用纳秒、毫秒、秒、分钟、小时或者天来表示时间间隔的单位。

可以这样来比较第一个方法是不是比第二个执行得更快:

Instant start = Instant.now();
doSomething();
Instant end = Instant.now();
Duration time = Duration.between(start, end);

Instant start2 = Instant.now();
doSomething2();
Instant end2 = Instant.now();
Duration time2 = Duration.between(start2, end2);
boolean fast = time.minus(time2).isNegative();
System.out.println(fast);

2、本地日期和时间

#

LocalDate now = LocalDate.now();
LocalDate date = LocalDate.of(2005, 5, 10);
LocalDate date2 = LocalDate.of(2003, Month.FEBRUARY, 5);
System.out.println(now);
System.out.println(date);
System.out.println(date2);

LocalTime now2 = LocalTime.now();
LocalTime time = LocalTime.of(22, 50, 56);
System.out.println(now2);
System.out.println(time);

3、日期格式化

#

//按照内置的不同方式格式化
String format = DateTimeFormatter.ISO_LOCAL_DATE.format(LocalDate.now());
String format2 = DateTimeFormatter.ISO_LOCAL_TIME.format(LocalTime.now());
String format3 = DateTimeFormatter.ISO_DATE.format(LocalDateTime.now());
String format4 = DateTimeFormatter.ISO_INSTANT.format(Instant.now());
System.out.println(format);
System.out.println(format2);
System.out.println(format3);
System.out.println(format4);

//按照标准格式格式化
DateTimeFormatter formatter = DateTimeFormatter.ofLocalizedDate(FormatStyle.FULL);
String format5 = formatter.format(LocalDateTime.now());
System.out.println(format5);

//按照指定方式格式化
DateTimeFormatter pattern = DateTimeFormatter.ofPattern("yyyy-MM-dd E HH:mm:ss");
String format6 = pattern.format(LocalDateTime.now());
System.out.println(format6);

文章永久链接:https://tech.souyunku.com/?p=13147


Warning: A non-numeric value encountered in /data/wangzhan/tech.souyunku.com.wp/wp-content/themes/dux/functions-theme.php on line 1154
赞(95) 打赏



未经允许不得转载:搜云库技术团队 » Java8中 LocalDate使用,Java8 实例介绍LocalDate,LocalDate 教程

IDEA2023.1.3破解,IDEA破解,IDEA 2023.1破解,最新IDEA激活码
IDEA2023.1.3破解,IDEA破解,IDEA 2023.1破解,最新IDEA激活码

评论 抢沙发

大前端WP主题 更专业 更方便

联系我们联系我们

觉得文章有用就打赏一下文章作者

微信扫一扫打赏

微信扫一扫打赏


Fatal error: Uncaught Exception: Cache directory not writable. Comet Cache needs this directory please: `/data/wangzhan/tech.souyunku.com.wp/wp-content/cache/comet-cache/cache/https/tech-souyunku-com/index.q`. Set permissions to `755` or higher; `777` might be needed in some cases. in /data/wangzhan/tech.souyunku.com.wp/wp-content/plugins/comet-cache/src/includes/traits/Ac/ObUtils.php:367 Stack trace: #0 [internal function]: WebSharks\CometCache\Classes\AdvancedCache->outputBufferCallbackHandler() #1 /data/wangzhan/tech.souyunku.com.wp/wp-includes/functions.php(5109): ob_end_flush() #2 /data/wangzhan/tech.souyunku.com.wp/wp-includes/class-wp-hook.php(303): wp_ob_end_flush_all() #3 /data/wangzhan/tech.souyunku.com.wp/wp-includes/class-wp-hook.php(327): WP_Hook->apply_filters() #4 /data/wangzhan/tech.souyunku.com.wp/wp-includes/plugin.php(470): WP_Hook->do_action() #5 /data/wangzhan/tech.souyunku.com.wp/wp-includes/load.php(1097): do_action() #6 [internal function]: shutdown_action_hook() #7 {main} thrown in /data/wangzhan/tech.souyunku.com.wp/wp-content/plugins/comet-cache/src/includes/traits/Ac/ObUtils.php on line 367