java-8

How to convert UTC DateTime to another Time Zone using Java 8 library?

六眼飞鱼酱① 提交于 2020-02-05 09:23:19
问题 final Timestamp rawDateTime = Timestamp.valueOf("2031-04-25 18:30:00"); final ZoneId zoneId = ZoneId.of("Asia/Calcutta"); final ZonedDateTime zonedDateTime = ZonedDateTime.ofInstant( Instant.ofEpochMilli(rawDateTime.getTime()), zoneId); // here we are getting output as 2031-04-25T18:30+05:30[Asia/Calcutta] final ZonedDateTime zonedDateTime1 = ZonedDateTime.of(rawDateTime.toLocalDateTime(), zoneId); // here we are getting output as 2031-04-25T18:30+05:30[Asia/Calcutta] But I want to get the

How to add a *.P12 keystore (with one entry)?

a 夏天 提交于 2020-02-04 02:39:49
问题 Apologies for my lack of understanding of certificates in general. I have a .p12 file (with a non-expired certificate) and a valid password so I can list the contents using: keytool -list -keystore file.p12 -storepass password -storetype PKCS12 -v In my Java code I am attempting an HTTPS post but keep getting this error: javax.net.ssl.SSLHandshakeException: java.security.cert.CertificateException: No name matching [host.path.com] found This same post works against one of my other environments

How to create a long time in Milliseconds from String in Java 8 with LocalDateTime?

一笑奈何 提交于 2020-02-02 04:59:45
问题 I have an input date in format yyyy-MM-dd_HH:mm:ss.SSS and convert it to long this way: SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd_HH:mm:ss.SSS"); try { Date date = simpleDateFormat.parse(lapTime); time = date.getTime(); } catch (ParseException e) { e.printStackTrace(); } And, after some manipulation, get mm:ss.SSS back from long: SimpleDateFormat simpleDateFormat = new SimpleDateFormat("mm:ss.SSS"); return simpleDateFormat.format(new Date(time)); How to change my

CompletableFuture chaining results

假如想象 提交于 2020-02-02 02:36:26
问题 I am trying to chain the calls/results of the methods to the next call. I get compile time error methodE because if am not able to get the reference of objB from the previous call. How can I pass the result of the previous call to the next chain? Have I completely misunderstood the process? Object objC = CompletableFuture.supplyAsync(() -> service.methodA(obj, width, height)) .thenApply(objA -> { try { return service.methodB(objA); } catch (Exception e) { throw new CompletionException(e); } }

How to use non final variable in Java 8 Lambdas

安稳与你 提交于 2020-02-01 12:20:26
问题 How can I use non-final variable in Java 8 lambda. It throws compilation error saying 'Local variable date defined in an enclosing scope must be final or effectively final' I actually want to achieve the following public Integer getTotal(Date date1, Date date2) { if(date2 == null || a few more conditions) { date2 = someOtherDate; } return someList.stream().filter(filter based on date1 and date2).map(Mapping Function).reduce(Addition); } How do I achieve this? It throws comilation error for

How to use non final variable in Java 8 Lambdas

柔情痞子 提交于 2020-02-01 12:19:06
问题 How can I use non-final variable in Java 8 lambda. It throws compilation error saying 'Local variable date defined in an enclosing scope must be final or effectively final' I actually want to achieve the following public Integer getTotal(Date date1, Date date2) { if(date2 == null || a few more conditions) { date2 = someOtherDate; } return someList.stream().filter(filter based on date1 and date2).map(Mapping Function).reduce(Addition); } How do I achieve this? It throws comilation error for

method reference vs lambda expression

冷暖自知 提交于 2020-01-31 18:13:19
问题 I want to replace lambda expression by method reference in the below example : public class Example { public static void main(String[] args) { List<String> words = Arrays.asList("toto.", "titi.", "other"); //lambda expression in the filter (predicate) words.stream().filter(s -> s.endsWith(".")).forEach(System.out::println); } } I want to write a something like this : words.stream().filter(s::endsWith(".")).forEach(System.out::println); is it possible to transform any lambda expression to

method reference vs lambda expression

好久不见. 提交于 2020-01-31 18:11:01
问题 I want to replace lambda expression by method reference in the below example : public class Example { public static void main(String[] args) { List<String> words = Arrays.asList("toto.", "titi.", "other"); //lambda expression in the filter (predicate) words.stream().filter(s -> s.endsWith(".")).forEach(System.out::println); } } I want to write a something like this : words.stream().filter(s::endsWith(".")).forEach(System.out::println); is it possible to transform any lambda expression to

which Point2D should I use

那年仲夏 提交于 2020-01-31 18:02:51
问题 The jdk8 contains 3 different Point2D classes. java.awt.geom.Point2D in the rt.jar javafx.geometry.Point2D in the jfxrt.jar com.sun.javafx.geom.Point2D in the jfxrt.jar Which Point2D class should I use ? Use/application: I want to perform geometric calculations to determine if a point intersects with a line. (e.g. Line2D.contains(Point2D) ) Given the fact that I'm also using other javafx features (i.e. also in the javafx.* package). My first guess, would be to use the javafx.geometry.Point2D

Java 8 modify stream elements

对着背影说爱祢 提交于 2020-01-30 20:00:10
问题 I wanted to write pure function with Java 8 that would take a collection as an argument, apply some change to every object of that collection and return a new collection after the update. I want to follow FP principles so I dont want to update/modify the collection that was passed as an argument. Is there any way of doing that with Stream API without creating a copy of the original collection first (and then using forEach or 'normal' for loop)? Sample object below and lets assume that I want