1. 程式人生 > >Java - JDK8新特性,程式碼demo示例;

Java - JDK8新特性,程式碼demo示例;

哈哈哈哈大魔都下雪啦,敲段程式碼暖和暖和,嘿嘿

public class jdk8Test {
	
	@Test
	public void LambdaNew() {
		//before jdk8
		List<String> names = Arrays.asList("Tom", "Sun", "Lily", "Amanda");
		for (String name : names) {
			System.out.println(name);
		}
		//after jdk8
		names.forEach(n -> System.out.println(n));
	}

	@Test
	public void testStream() {
		List<Integer> nums = Lists.newArrayList(1,1,null,2,3,4,null,5,6,7,8,9,10);
		System.out.println("求和:"+nums
				.stream()//轉成Stream
				.filter(team -> team!=null)//過濾
				.distinct()//去重
				.mapToInt(num->num*2)//map操作
				.skip(2)//跳過前2個元素
				.limit(4)//限制取前4個元素
				.peek(System.out::println)//流式處理物件函式
				.sum());
	}

	public interface JDK8Interface{
		//1、介面中可以定義靜態方法
		static void staticMethod() {
			System.out.println("介面中的靜態方法");
		}
		//2、使用default之後就可以定義普通方法的方法體了
		default void DefaultMethod() {
			System.out.println("介面中的預設方法");
		}
	}
	
	@Test
	public void base64Test() {
		final String test = "就是要測試加解密sd! !asdfas#^((&^*";
		String encoded = Base64.getEncoder().encodeToString(test.getBytes(StandardCharsets.UTF_8));
		System.out.println("加密後="+  encoded);
		
		final String decoded = new String( Base64.getDecoder().decode(encoded), StandardCharsets.UTF_8);
		System.out.println("解密後="+ decoded);
	}

	@Test
	public void TimeTest() {
		//1、Clock
		final Clock clock = Clock.systemUTC();
		System.out.println("clock = "+ clock.getZone());
		
		//2、
		final LocalDate date = LocalDate.now();
		System.out.println("date = " + date);
		final LocalDate dateFromClock = LocalDate.now(clock);
		System.out.println("dateFromClock = "+ dateFromClock);
		
		//3、
		final LocalTime time = LocalTime.now();
		System.out.println("time = " + time);
		final LocalTime timeFromClock = LocalTime.now(clock);
		System.out.println("timeFromClock = "+ timeFromClock);
	
	    //4、
		final LocalDateTime datetime = LocalDateTime.now();
		System.out.println("datetime = " + datetime);
		final LocalDateTime datetimeFromClock = LocalDateTime.now(clock);
		System.out.println("datetimeFromClock = "+ datetimeFromClock);
		
		//5、
		final ZonedDateTime zonedDateTime = ZonedDateTime.now();
		System.out.println("zonedDateTime = "+zonedDateTime);
		final ZonedDateTime zonedDateTimef = ZonedDateTime.now(clock);
		System.out.println("zonedDateTimef = "+zonedDateTimef);
		final ZonedDateTime zonedDateTimec =ZonedDateTime.now(ZoneId.of("America/New_York"));
		System.out.println("zonedDateTimec = "+zonedDateTimec);
		
	}
	
	@Test
	public void paeseDateTime() {
		//解析日期
		String dateStr= "2018年12月08日";
		DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy年MM月dd日");
		LocalDate date= LocalDate.parse(dateStr, formatter);
		System.out.println(date);
		//日期轉換為字串
		LocalDateTime now = LocalDateTime.now();
		//DateTimeFormatter format = DateTimeFormatter.ofPattern("yyyy年MM月dd日 hh:mm a");
		DateTimeFormatter format = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss,SSS");
		String nowStr = now .format(format);
		System.out.println(nowStr);
	}











}