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);
	}











}
posted @ 2022-01-27 18:36  zhangdaopin  阅读(59)  评论(0编辑  收藏  举报