默认情况下,使用DateTimeFormatter.ISO_INSTANT格式化程序的toString方法.如果格式化程序碰巧为0,则不会打印秒数的数字.Instant
java时间示例:
2015-10-08T17:13:07.589Z
2015-10-08T17:13:07Z
Run Code Online (Sandbox Code Playgroud)
Joda-Time示例(以及我对java.time的期望):
2015-10-08T17:13:07.589Z
2015-10-08T17:13:07.000Z
Run Code Online (Sandbox Code Playgroud)
在一些系统中解析这真的很令人沮丧.Elasticsearch是我遇到的第一个问题,没有预定义的格式支持可选的millis,但我可以使用自定义格式解决这个问题.默认似乎是错误的.
看来你无法为Instants构建自己的格式字符串.是实现我自己的java.time.format.DateTimeFormatterBuilder.InstantPrinterParser的唯一选择吗?
为什么下面的片段在第二次传递时打印为真?它应该不是一个新的实例吗?
import java.util.function.Supplier;
public class Foo {
public static void main(String[] args) throws Exception {
Supplier<Long> old = () -> System.nanoTime();
for (int i = 0; i < 3; i++) {
/* false true true
Supplier<Long> foo = System::nanoTime;*/
Supplier<Long> foo = () -> System.nanoTime();
/* false false false
Supplier<Long> foo = new Supplier<Long>() {
@Override
public Long get() {
return System.nanoTime();
}
};
//*/
System.out.printf("%s %s %s%n", foo == old, foo, old);
old = foo;
}
}
}
Run Code Online (Sandbox Code Playgroud)
false …Run Code Online (Sandbox Code Playgroud) 我正在通过服务器jre,当我提取serverjre tar.gz时,它给了我jdk文件夹.为什么serverJRE给JDK这样的文件夹.serverJRE里面还有JRE文件夹.那么使用哪一个,整个serverJRE或者只是在serverJRE里面的JRE文件夹.ServerJRE中的JRE内容与JDK中的JRE相同.
我不明白这种差异.
我有一个商品对象,它有两个属性:firstCategoryId和secondCategoryId.我有一个商品清单,我想获得所有类别ID(包括firstCategoryId和secondCategoryId).
我目前的解决方案是:
List<Integer> categoryIdList = goodsList.stream().map(g->g.getFirstCategoryId()).collect(toList());
categoryIdList.addAll(goodsList.stream().map(g->g.getSecondCategoryId()).collect(toList()));
Run Code Online (Sandbox Code Playgroud)
有没有更方便的方式我可以在一个语句中获得所有categoryIds?
在Java 8中,如果我有两个具有不同(但兼容)返回类型的接口,则反射告诉我两个方法中的一个是默认方法,即使我实际上没有将该方法声明为默认方法或提供方法体.
例如,请使用以下代码段:
package com.company;
import java.lang.reflect.Method;
interface BarInterface {}
class Bar implements BarInterface {}
interface FooInterface {
public BarInterface getBar();
}
interface FooInterface2 extends FooInterface {
public Bar getBar();
}
class Foo implements FooInterface2 {
public Bar getBar(){
throw new UnsupportedOperationException();
}
}
public class Main {
public static void main(String[] args) {
for(Method m : FooInterface2.class.getMethods()){
System.out.println(m);
}
}
}
Run Code Online (Sandbox Code Playgroud)
Java 1.8生成以下输出:
public abstract com.company.Bar com.company.FooInterface2.getBar()
public default com.company.BarInterface com.company.FooInterface2.getBar()
Run Code Online (Sandbox Code Playgroud)
这看起来很奇怪,不仅因为两种方法都存在,而且因为其中一种方法突然而且莫名其妙地变成了默认方法.
尽管两种方法具有相同的签名,但在Java 7中运行相同的代码会产生一些不太意外的情况,尽管仍然令人困惑:
public …Run Code Online (Sandbox Code Playgroud) 我在使用jre1.8.0_66的代码运行中遇到这个奇怪的异常:
Exception in thread "main" java.lang.BootstrapMethodError: call site initialization exception
at java.lang.invoke.CallSite.makeSite(CallSite.java:341)
at java.lang.invoke.MethodHandleNatives.linkCallSiteImpl(MethodHandleNatives.java:307)
at java.lang.invoke.MethodHandleNatives.linkCallSite(MethodHandleNatives.java:297)
at main
Caused by: java.lang.invoke.LambdaConversionException: Invalid receiver type class java.lang.Object; not a subtype of implementation type interface Fruit
at java.lang.invoke.AbstractValidatingLambdaMetafactory.validateMetafactoryArgs(AbstractValidatingLambdaMetafactory.java:233)
at java.lang.invoke.LambdaMetafactory.metafactory(LambdaMetafactory.java:303)
at java.lang.invoke.CallSite.makeSite(CallSite.java:302)
... 3 more
Run Code Online (Sandbox Code Playgroud)
这是什么意思?代码如下:
public static interface Fruit {
int getPickingMonth();
}
public static class Apple implements Fruit, Serializable {
@Override
public int getPickingMonth() {
return 11;
}
}
public static class Orange implements Fruit, Serializable {
@Override
public int …Run Code Online (Sandbox Code Playgroud) 当我看到代码片段之类的时候
interface A {
void a();
void b() default { System.out.println("b"); };
void c() final { System.out.println("c"); };
}
Run Code Online (Sandbox Code Playgroud)
我有一个问题.我们还没有得到足够的Java?为什么有人需要这个呢?
运行下面的代码会导致错误消息Bad type on operand stack.
public static void main(String args[]) {
TransformService transformService = (inputs) -> {
return new ArrayList<String>(3) {{
add("one");
add("two");
add("three");
}};
};
Collection<Integer> inputs = new HashSet<Integer>(2) {{
add(5);
add(7);
}};
Collection<String> results = transformService.transform(inputs);
System.out.println(results.size());
}
public interface TransformService {
Collection<String> transform(Collection<Integer> inputs);
}
Run Code Online (Sandbox Code Playgroud)
但是,删除lamda中的双括号初始化(匿名内部类)允许代码按预期运行,为什么?以下作品:
public class SecondLambda {
public static void main(String args[]) {
TransformService transformService = (inputs) -> {
Collection<String> results = new ArrayList<String>(3);
results.add("one");
results.add("two");
results.add("three");
return results;
};
Collection<Integer> …Run Code Online (Sandbox Code Playgroud) Java 8接口默认方法与抽象类中的非抽象方法 - 两者之间是否存在任何差异(除了iface的类别,可见性等)
不是Java中的默认方法,这意味着它违背了Java多年来所宣传的本质?!
java ×10
java-8 ×10
lambda ×4
interface ×2
delegates ×1
java-stream ×1
java-time ×1
jdk1.6 ×1
jodatime ×1
return-type ×1