Skip to content

Instantly share code, notes, and snippets.

@gecko655
Last active December 16, 2015 05:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gecko655/14f6f334ae9e814622da to your computer and use it in GitHub Desktop.
Save gecko655/14f6f334ae9e814622da to your computer and use it in GitHub Desktop.
import java.time.Instant;
import java.time.format.DateTimeFormatter;
import java.time.temporal.TemporalAccessor;
import java.util.Date;
import java.util.List;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
public class DateTimeFormatterTest{
private final static String pattern = "yyyy-MM-dd HH:mm:ss.SSS z";
private final static DateTimeFormatter dtFormatter = DateTimeFormatter.ofPattern(pattern);
public static Date stringToDate(String dateString) {
//これはダメ
//return dtFormatter.parse(dateString,((Function<TemporalAccessor,Instant>)Instant::from).andThen(Date::from))
//これはOK
return dtFormatter.parse(dateString,DateTimeFormatterTest::taToDate);
}
private static Date taToDate(TemporalAccessor ta){
Function<TemporalAccessor,Date> fun = ((Function<TemporalAccessor,Instant>)Instant::from).andThen(Date::from);
return fun.apply(ta);
}
public static void main(String[] args){
List<Date> ds = IntStream.range(00,59).parallel()
.mapToObj(sec -> stringToDate("2015-12-16 14:"+String.format("%02d",sec)+":00.655 JST"))
.collect(Collectors.toList());
System.out.println(ds);// -> [Wed Dec 16 14:00:00 JST 2015, Wed Dec 16 14:01:00 JST 2015, Wed Dec 16 14:02:00 JST 2015, ..
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment