Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Formatting dates inside a Function<T,R>

I am trying to format a date inside a Functional Interface but I don't know if it is possible

SimpleDateFormat dt1 = new SimpleDateFormat("ddmmyyyyy");

List<MenuPrice> menuPrices = findAll(restaurant);

menuPrices.parallelStream()
          .collect(Collectors.groupingBy(dt1.format(MenuPrice::getUpdateDate)));
like image 989
Nunyet de Can Calçada Avatar asked Sep 20 '18 08:09

Nunyet de Can Calçada


People also ask

What is T and Z in datetime?

What is T between date and time? The T is just a literal to separate the date from the time, and the Z means “zero hour offset” also known as “Zulu time” (UTC). If your strings always have a “Z” you can use: SimpleDateFormat format = new SimpleDateFormat( “yyyy-MM-dd'T'HH:mm:ss).

Which function in R for dates?

The most basic function we use while dealing with the dates is as. Date() function. This function allows us to create a date value (without time) in R programming. It allows the various input formats of the date value as well through the format = argument.

How do I manage dates in R?

R provides several options for dealing with date and date/time data. The builtin as. Date function handles dates (without times); the contributed library chron handles dates and times, but does not control for time zones; and the POSIXct and POSIXlt classes allow for dates and times with control for time zones.


2 Answers

It's possible, but not with a method reference:

Map<String,List<MenuPrice>>
    menuPrices.parallelStream()
              .collect(Collectors.groupingBy(m -> dt1.format(m.getUpdateDate())));
like image 57
Eran Avatar answered Oct 10 '22 23:10

Eran


You could create a method for that btw, to make things slightly more readable:

private static String formatUpdatedDate(MenuPrice menu){ 
     return dt1.format(menu.getUpdatedDate());
}

And use it:

 .collect(Collectors.groupingBy(YourClass::formatUpdatedDate)
like image 32
Eugene Avatar answered Oct 10 '22 23:10

Eugene