java-8

Where are the Java 8 DateTimeFormatters constants defined?

拜拜、爱过 提交于 2020-01-12 08:56:14
问题 I am looking at the DateTimeFormatter class and I was wondering where are the constants like "M" , "EEE" and "YY" etc are defined. More specifically there should be code like this somewhere given a ChronoField and TextStyle, it should return the DateTimeFormatter string snippet e.g. (ChronoField.MONTH_OF_YEAR, TextStyle.SHORT) should map to the string "MMM" 回答1: After some source digging, I found out that these are pre-defined in a private static final Map , called FIELD_MAP , which is a

Where are the Java 8 DateTimeFormatters constants defined?

我与影子孤独终老i 提交于 2020-01-12 08:56:03
问题 I am looking at the DateTimeFormatter class and I was wondering where are the constants like "M" , "EEE" and "YY" etc are defined. More specifically there should be code like this somewhere given a ChronoField and TextStyle, it should return the DateTimeFormatter string snippet e.g. (ChronoField.MONTH_OF_YEAR, TextStyle.SHORT) should map to the string "MMM" 回答1: After some source digging, I found out that these are pre-defined in a private static final Map , called FIELD_MAP , which is a

Catching exceptions out of 'stream()' or 'parallelStream()' loses correct values

会有一股神秘感。 提交于 2020-01-12 07:41:18
问题 In the following code, when catching NumberFormatException out of for iteration, the strings in appropriate form appearing in strList before the first bad one (i.e., "illegal_3" ) have been parsed successfully (i.e., "1" and "2" have been parsed as integers 1 and 2 ). public void testCaughtRuntimeExceptionOutOfIteration() { List<String> strList = Stream.of("1", "2", "illegal_3", "4", "illegal_5", "6").collect(Collectors.toList()); List<Integer> intList = new ArrayList<>(); try{ for (String

closures in groovy vs closures in java 8 (lambda expressions)?

点点圈 提交于 2020-01-12 06:56:07
问题 Given doSomething(Function foo) { println foo(2) } Groovy: doSomething( { it*it } as Function ) Java: doSomething( (x) -> x*x ) Is there any difference between the two? 回答1: In Groovy, closures are first class citizens of type groovy.lang.Closure , whereas lambdas in Java 8 are actual instances of the default method interface they represent. This means you need to use the as keyword in Groovy (as you've shown), but alternatively, in Java you need to specify an interface, so in Groovy, you

Convert ZonedDateTime to LocalDateTime at time zone

爱⌒轻易说出口 提交于 2020-01-12 06:44:26
问题 I have an object of ZonedDateTime that is constructed like this ZonedDateTime z = ZonedDateTime.of(LocalDate.now().atTime(11, 30), ZoneOffset.UTC); How can I convert it to LocalDateTime at time zone of Switzerland? Expected result should be 16 april 2018 13:30 . 回答1: How can I convert it to LocalDateTime at time zone of Switzerland? You can convert the UTC ZonedDateTime into a ZonedDateTime with the time zone of Switzerland, but maintaining the same instant in time, and then get the

Convert ZonedDateTime to LocalDateTime at time zone

狂风中的少年 提交于 2020-01-12 06:44:10
问题 I have an object of ZonedDateTime that is constructed like this ZonedDateTime z = ZonedDateTime.of(LocalDate.now().atTime(11, 30), ZoneOffset.UTC); How can I convert it to LocalDateTime at time zone of Switzerland? Expected result should be 16 april 2018 13:30 . 回答1: How can I convert it to LocalDateTime at time zone of Switzerland? You can convert the UTC ZonedDateTime into a ZonedDateTime with the time zone of Switzerland, but maintaining the same instant in time, and then get the

Stack using the Java 8 collection streaming API

元气小坏坏 提交于 2020-01-12 06:36:08
问题 I have a method which generates an object each time I execute it, and I need to reverse the order with which I am getting them. So I thought the natural way to do it would be a Stack, since it is LIFO. However, the Java Stack does not seem to play well with the new Java 8 streaming API. If I do this: Stack<String> stack = new Stack<String>(); stack.push("A"); stack.push("B"); stack.push("C"); List<String> list = stack.stream().collect(Collectors.toList()); System.out.println("Collected: " +

Eclipse: JRE System Library in Java Build Path reset

女生的网名这么多〃 提交于 2020-01-12 04:31:51
问题 For developing a JavaFX application I'm using a 4.3.1 snapshot of eclipse together with JDK 8 build b116. In my workspace projects the JRE library inclusion in the build path get resetted back to Java 1.4 all the time: Unfortunately, this can only be fixed temporary (until the next eclipse restart): In the build section of my pom files I have: <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.1</version> <configuration> <source>1.8<

How can a Java lambda-expression reference itself?

泄露秘密 提交于 2020-01-12 02:24:17
问题 I found this article to be very informative in comparison of old-style functions to new Java-8 lambda-functions and parallel processing. One thing I couldn't quite understand was one restriction on the lambda-functions: From page 4: 3.3 Preconditions Although lambda expressions are intended as a more con- cise alternative to AIC , they are not a complete replacement. There are several preconditions that LambdaFicator checks before refactoring an AIC into a lambda expression. These

Why Instant does not support operations with ChronoUnit.YEARS?

假装没事ソ 提交于 2020-01-11 19:55:47
问题 This was unexpected to me: > Clock clock = Clock.systemUTC(); > Instant.now(clock).minus(3, ChronoUnit.DAYS); java.time.Instant res4 = 2016-10-04T00:57:20.840Z > Instant.now(clock).minus(3, ChronoUnit.YEARS); java.time.temporal.UnsupportedTemporalTypeException: Unsupported unit: Years As a workaround I have to do this: > Instant.now(clock).atOffset(ZoneOffset.UTC).minus(3, ChronoUnit.YEARS).toInstant(); java.time.Instant res11 = 2013-10-07T01:02:56.361Z I am curios why Instant does not