Java II:6.1、时间线的使用

6.1、时间线的使用

package xyz.jangle.javabase.n6.b1;

import java.time.Duration;
import java.time.Instant;
import java.util.Collections;
import java.util.List;
import java.util.Random;
//import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

/**
 * 6.1、时间线的使用
 * @author jangle
 * @email [email protected]
 * @time 2021年2月14日 下午12:49:33
 * 
 */
public class M1 {

	public static void main(String[] args) throws InterruptedException {
		
		// 获取当前时间
		Instant now = Instant.now();
//		TimeUnit.SECONDS.sleep(2);
		runAlgorithm();
		Instant now2 = Instant.now();
		System.out.println(now);
		System.out.println(now2);
		Instant now3 = Instant.now();
		runAlgorithm2();
		Instant now4 = Instant.now();
		System.out.println(now3);
		System.out.println(now4);
		// 获取两个时间之间的差值
		Duration between = Duration.between(now, now2);
		System.out.println(between.toNanos());
		System.out.println(between.toMillis());
		System.out.println(between.getSeconds());
		System.out.println(between.toDays());
		System.out.println(between.toHours());
		// 增加时间
		System.out.println(now.plus(between));
		// 减少时间
		System.out.println(now.minus(between));

	}
	
	public static void runAlgorithm() {
		int size = 10;
		List list = new Random().ints().map(i -> i%100).limit(size)
				.boxed().collect(Collectors.toList());
		Collections.sort(list);
		System.out.println(list);
	}
	
	public static void runAlgorithm2() {
		int size = 10;
		List list = new Random().ints().map(i -> i%100).limit(size).boxed().collect(Collectors.toList());
		System.out.println(list);
		while(!IntStream.range(1, list.size()).allMatch(i -> list.get(i-1).compareTo(list.get(i))<=0)) {
			Collections.shuffle(list);
		}
		System.out.println(list);
	}

}

 

你可能感兴趣的:(bakcode,Java相关,#,JavaBase,java)