java-8

DateTimeFormatter giving wrong format for edge cases [duplicate]

只愿长相守 提交于 2021-02-19 01:11:50
问题 This question already has answers here : Java LocalDate Formatting of 2000-1-2 error [duplicate] (2 answers) Closed 2 years ago . DateTimeFormatter is not giving correct format for Dec 30 and 31 2018 as per following snippet. final String DATE_FORMAT = "YYYYMM"; DateTimeFormatter dateFormat = DateTimeFormatter.ofPattern(DATE_FORMAT); LocalDateTime startDate = LocalDateTime.of(2018,12,29,5,0,0); System.out.println(startDate.format(dateFormat)); //prints 201812 LocalDateTime startDate =

Java 8: First use of stream() or parallelStream() very slow - Usage in practice meaningful?

时光总嘲笑我的痴心妄想 提交于 2021-02-18 23:03:45
问题 In the last few days I made some test with external iteration, streams and parallelStreams in Java 8 and measured the duration of the execution time. I also read about the warm up time which I have to consider. But one question still remains. The first time when I call the method stream() or parallelStream() on a collection the execution time is higher than it is for an external iteration. I already know, that when I call the stream() or parallelStream() more often on the same collection and

Is there a equivalent to ChronoUnit.between that returns fraction instead of integer?

余生颓废 提交于 2021-02-18 22:35:27
问题 Methods such is ChronoUnit.HOURS.between(start, end) returns long so I can't get the fraction from there. Is there an alternative method/approach that would return a fraction? 回答1: The whole point of ChronoUnit.HOURS.between(start, end) is to get the number of HOURS between the two timepoints. For example: it's either 1 or 2 hours between, there is no such thing as 1.5658 hours*. If you need more precicion, use another ChronoUnit, i.e. Minutes or seconds. The problem with decimal fractions is

How to Parse Date Strings with 🎌 Japanese Numbers in Java DateTime API

末鹿安然 提交于 2021-02-18 22:10:28
问题 After asking [How to parse 🎌 Japanese Era Date string values into LocalDate & LocalDateTime], I was curious about the following case; 明治二十三年十一月二十九日 Is there a way to parse Japanese numbers on top of Japanese Calendar characters, essentially a pure Japanese date, into LocalDate ? Using only Java DateTime API. I don't want to modify the input String values, but want just API to handle the recognition. 回答1: For anyone reading along, your example date string holds an era designator, year of era

Java 8 : Lambda Function and Generic Wildcards

左心房为你撑大大i 提交于 2021-02-18 22:10:27
问题 I have the following class class Book implement Borrowable { @Override public String toString(Function<? extends Borrowable , String> format) { return format.apply(this); } } This gives me an error that i cannot use "apply" on this(Book object). My current formatter is Function<Book, String> REGULAR_FORMAT = book -> "name='" + book.name + '\'' + ", author='" + book.author + '\'' + ", year=" + book.year; I don't want to make the lambda function of the type Function<Borrowable, String> as I

How to Parse Date Strings with 🎌 Japanese Numbers in Java DateTime API

笑着哭i 提交于 2021-02-18 22:09:13
问题 After asking [How to parse 🎌 Japanese Era Date string values into LocalDate & LocalDateTime], I was curious about the following case; 明治二十三年十一月二十九日 Is there a way to parse Japanese numbers on top of Japanese Calendar characters, essentially a pure Japanese date, into LocalDate ? Using only Java DateTime API. I don't want to modify the input String values, but want just API to handle the recognition. 回答1: For anyone reading along, your example date string holds an era designator, year of era

Java 8 DateTimeFormatter parsing optional sections

£可爱£侵袭症+ 提交于 2021-02-18 22:08:00
问题 I need to parse date-times as strings coming as two different formats: 19861221235959Z 1986-12-21T23:59:59Z The following dateTimeFormatter pattern properly parses the first kind of date strings DateTimeFormatter.ofPattern ("uuuuMMddHHmmss[,S][.S]X") but fails on the second one as dashes, colons and T are not expected. My attempt was to use optional sections as follows: DateTimeFormatter.ofPattern ("uuuu[-]MM[-]dd['T']HH[:]mm[:]ss[,S][.S]X") Unexpectedly, this parses the second kind of date

Java 8 DateTimeFormatter parsing optional sections

倾然丶 夕夏残阳落幕 提交于 2021-02-18 22:04:46
问题 I need to parse date-times as strings coming as two different formats: 19861221235959Z 1986-12-21T23:59:59Z The following dateTimeFormatter pattern properly parses the first kind of date strings DateTimeFormatter.ofPattern ("uuuuMMddHHmmss[,S][.S]X") but fails on the second one as dashes, colons and T are not expected. My attempt was to use optional sections as follows: DateTimeFormatter.ofPattern ("uuuu[-]MM[-]dd['T']HH[:]mm[:]ss[,S][.S]X") Unexpectedly, this parses the second kind of date

Is the skip() method a short circuiting-operation?

可紊 提交于 2021-02-18 21:12:40
问题 I am reading about Java streams' short-circuiting operations and found in some articles that skip() is a short-circuiting operation. In another article they didn't mention skip() as a short-circuiting operation. Now I am confused; is skip() a short-circuiting operation or not? 回答1: From the java doc under the "Stream operations and pipelines" section : An intermediate operation is short-circuiting if, when presented with infinite input, it may produce a finite stream as a result . A terminal

“cannot find symbol method metafactory” using Lambda

喜夏-厌秋 提交于 2021-02-18 20:44:39
问题 I'm using java 8 and lambda expressions. When I use lambda expressions with OnClickListeners everything is fine, but when I use that in this animate method: public void configureFragmentToolbar(String title, boolean displayHomeAsUpEnabled) { //.. this.toolbar.animate().translationY(-50).setDuration(300).withEndAction(() -> { //ERROR toolbar.animate().translationY(0).setDuration(300); }); } I´m getting this error: "error: cannot find symbol method metafactory(Lookup,String,MethodType