Recently shared snippets for language "Java"

Created 1 year ago by anonymous
LocalDateTime time = LocalDateTime.now();
				LocalDateTime nextHour = time.plus(1l, ChronoUnit.HOURS).truncatedTo(ChronoUnit.HOURS)
				                                .plusMinutes(60 * (time.getMinute() / 60));
				long minutes = Duration.between(LocalDateTime.now(), nextHour).toMinutes();
Created 1 year ago by anonymous
import org.apache.commons.lang3.StringUtils;
....

 List<String> filteredAndTrimmed = Arrays.asList("abc ", " cd", "", "gh").stream().map(StringUtils::trim).filter(StringUtils::isNotBlank)
			.collect(Collectors.toList());
		 
filteredAndTrimmed.forEach(System.out::println);

//output is 
abc
cd
gh
Arrays.asList(1,2,3,4).stream().mapToInt(i -> i.intValue()).sum();
Arrays.asList(1,2,3,4).stream().mapToInt(Integer::intValue).sum();
Arrays.asList(1,2,3,4).stream().collect(Collectors.summingInt(Integer::intValue));

// parallel streams
LongAdder a = new LongAdder();
Arrays.asList(1,2,3,4).parallelStream().forEach(a::add);
sum = a.intValue();


int sum = Arrays.asList(1,2,3,4).stream().reduce(0, (x,y) -> x+y)
Created 1 year ago by anonymous
private static String removeBetween(String string, String startChars, String endChars) {
		if(string.contains(startChars)&& string.contains(endChars)) {
			String substringAfter = StringUtils.substringAfterLast(string, startChars);
			String substringBefore = StringUtils.substringBefore(substringAfter, endChars);
			String finalStr = StringUtils.remove(string ,startChars + substringBefore+ endChars);
			string =removeBetween(finalStr, startChars, endChars);
		}
		return string;
	}
// Exception
Exception in thread "main" java.lang.NoClassDefFoundError: org/bouncycastle/jce/provider/BouncyCastleProvider
	at org.apache.pdfbox.pdmodel.PDDocument.protect(PDDocument.java:1318)
---
Caused by: java.lang.ClassNotFoundException: org.bouncycastle.jce.provider.BouncyCastleProvider
	at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
	at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
	at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
	at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
	... 2 more

// Solution
// add dependency in your pom.xml
<dependency>
    <groupId>org.bouncycastle</groupId>
    <artifactId>bcprov-jdk15on</artifactId>
    <version>1.58</version>
</dependency>