如何解析ZoneDateTime不包含的字符串zone和其他字段?
这是Spock中的测试重现:
import spock.lang.Specification
import spock.lang.Unroll
import java.time.ZoneId
import java.time.ZoneOffset
import java.time.ZonedDateTime
import java.time.format.DateTimeFormatter
@Unroll
class ZonedDateTimeParsingSpec extends Specification {
def "DateTimeFormatter.ISO_ZONED_DATE_TIME parsing incomplete date: #value #expected"() {
expect:
ZonedDateTime.parse(value, DateTimeFormatter.ISO_ZONED_DATE_TIME) == expected
where:
value | expected
'2014-04-23T04:30:45.123Z' | ZonedDateTime.of(2014, 4, 23, 4, 30, 45, 123_000_000, ZoneOffset.UTC)
'2014-04-23T04:30:45.123+01:00' | ZonedDateTime.of(2014, 4, 23, 4, 30, 45, 123_000_000, ZoneOffset.ofHours(1))
'2014-04-23T04:30:45.123' | ZonedDateTime.of(2014, 4, 23, 4, 30, 45, 123_000_000, ZoneId.systemDefault())
'2014-04-23T04:30' | ZonedDateTime.of(2014, 4, 23, 4, 30, 0, 0, …Run Code Online (Sandbox Code Playgroud) 请考虑以下代码:
Path directory = Paths.get(/* some directory */);
Files.list(directory).forEach(System.out::println);
Run Code Online (Sandbox Code Playgroud)
终端操作(如forEach)是否关闭已打开的基础文件?
请参阅Files.list的javadoc的相关部分:
返回的流封装了DirectoryStream.如果需要及时处理文件系统资源,则应使用try-with-resources构造来确保在流操作完成后调用流的close方法.
如果它没有调用Stream.close(),那么在生成可维护代码时调用它的最佳选择是什么?
我的问题与 该线程中显式使用LambdaMetafactory密切相关,提供了一些非常好的例子来使用LambdaMetafactory访问类的静态方法; 但是,我想知道访问现有bean实例的非静态字段的等效代码是什么.似乎很难找到一个例子,我所执行的每一次尝试都以非工作代码结束.
这是bean代码:
class SimpleBean {
private Object obj= "myCustomObject";
private static Object STATIC_OBJECT = "myCustomStaticObject";
public Object getObj() {
return obj;
}
public void setObj(final Object obj) {
this.obj = obj;
}
public static Object getStaticObj() {
return STATIC_OBJECT;
}
public static void setStaticObj(final Object obj) {
STATIC_OBJECT = obj;
}
}
Run Code Online (Sandbox Code Playgroud)
这里是一个成功访问静态方法"getStaticObj()"的工作单元测试:
@Test
public void accessStaticMethod() throws Throwable
{
MethodHandles.Lookup caller = MethodHandles.lookup();
Method reflected = SimpleBean.class.getDeclaredMethod("getStaticObj");
MethodHandle methodHandle = caller.unreflect(reflected);
CallSite site = LambdaMetafactory.metafactory(caller, …Run Code Online (Sandbox Code Playgroud) 我怎样才能最好地将其转换java.util.Date为Java 8 java.time.YearMonth?
不幸的是,以下抛出DateTimeException:
YearMonth yearMonth = YearMonth.from(date.toInstant());
Run Code Online (Sandbox Code Playgroud)
结果是:
java.time.DateTimeException: Unable to obtain YearMonth from TemporalAccessor: 2015-01-08T14:28:39.183Z of type java.time.Instant
at java.time.YearMonth.from(YearMonth.java:264)
...
Run Code Online (Sandbox Code Playgroud)
我需要此功能,因为我想YearMonth使用JPA 将值存储在数据库中.目前JPA不支持YearMonth,所以我想出了以下YearMonthConverter(省略了导入):
// TODO (future): delete when next version of JPA (i.e. Java 9?) supports YearMonth. See https://java.net/jira/browse/JPA_SPEC-63
@Converter(autoApply = true)
public class YearMonthConverter implements AttributeConverter<YearMonth, Date> {
@Override
public Date convertToDatabaseColumn(YearMonth attribute) {
// uses default zone since in the end only dates are needed
return attribute …Run Code Online (Sandbox Code Playgroud) 我有两个Instant类的实例,java.time例如:
Instant instant1 = Instant.now();
Instant instant2 = Instant.now().plus(5, ChronoUnit.HOURS);
Run Code Online (Sandbox Code Playgroud)
现在我想检查两个实例Instant是否实际上在同一天(日,月和年匹配).我想,让我们只使用闪亮的新的LocalDate和通用的from静态方法:
LocalDate localdate1 = LocalDate.from(instant1);
LocalDate localdate2 = LocalDate.from(instant2);
if (localdate1.equals(localdate2)) {
// All the awesome
}
Run Code Online (Sandbox Code Playgroud)
除了通用from方法不是那么普遍,Java在运行时抱怨异常:
java.time.DateTimeException: Unable to obtain LocalDate from TemporalAccessor: 2014-11-04T18:18:12Z of type java.time.Instant
Run Code Online (Sandbox Code Playgroud)
这让我回到了1号广场.
如果两个实例Instant具有相同的日期(具有相同的日,月和年),建议/最快的方法是什么?
public interface Table<T> {
@Overrride
default boolean equals(Object other) {
//do something and return true/false
}
}
Run Code Online (Sandbox Code Playgroud)
为什么上面的代码有编译错误"java:默认方法在接口表中等于覆盖java.lang.Object的成员"?我们不能使用接口默认方法覆盖hashCode和equals方法,大概我在同一个接口中有方法来确定实现这个接口的对象是否相等?
为了限制Java 7应用程序消耗的总内存,我可以使用以下公式(取自本文):
最大内存= [-Xmx] + [-XX:MaxPermSize] + number_of_threads*[-Xss]
删除PermGen后,此公式如何为Java 8应用程序更改?
我应该使用选项"-XX:MaxMetaspaceSize"来限制元空间消耗的最大内存吗?
我有一个java.time.Duration,我想以mm:ss的形式输出它.似乎不可能使用DateTimeFormatter,因为它只接受LocalTime,ZonedTIme ectera
所以我这样做了,工作正常90秒给1:30,但66秒给出1:6而我想要1:06
Duration duration = Duration.ofSeconds(track.getLength().longValue());
System.out.println(duration.toMinutes() + ":" + duration.minusMinutes(duration.toMinutes()).getSeconds());
Run Code Online (Sandbox Code Playgroud) 这个关于Stack Overflow的流行答案可以说明函数式编程和面向对象编程之间的区别:
当你对事物有一套固定的操作时,面向对象的语言是很好的 ,随着代码的发展,你主要添加新东西.这可以通过添加实现现有方法的新类来完成,并且现有类保持不变.
当你有一套固定的东西时,函数式语言是很好的,随着代码的发展,你主要在现有的东西上添加新的操作.这可以通过添加使用现有数据类型计算的新函数来实现,并且现有函数是独立的.
说我有一个Animal界面:
public interface Animal {
public void speak();
}
Run Code Online (Sandbox Code Playgroud)
我有一个Dog,Cat,Fish和Bird所有实现的接口.如果我想为Animalnamed 添加一个新方法jump(),我将不得不浏览所有子类并实现jump().
访问者模式可以缓解这个问题,但似乎随着Java 8中引入的新功能特性,我们应该能够以不同的方式解决这个问题.在scala我可以很容易地使用模式匹配,但Java还没有真正拥有它.
Java 8实际上是否更容易在现有事物上添加新操作?
我有一个像这样的静态枚举:
private static enum standardAttributes {
id, gender, firstname, lastname, mail, mobile
}
Run Code Online (Sandbox Code Playgroud)
我需要所有值为String.因此我有一个像这样的方法:
public static List<String> getStandardRecipientsAttributes() {
List<String> standardAttributesList = new ArrayList<String>();
for (standardAttributes s : standardAttributes.values())
standardAttributesList.add(s.toString());
return standardAttributesList;
}
Run Code Online (Sandbox Code Playgroud)
每次调用此方法时都无需创建相同的List.所以我创建了一个静态成员:
static final List<String> standardAttributesList;
static {
standardAttributesList = getStandardRecipientsAttributes();
}
Run Code Online (Sandbox Code Playgroud)
这一切都很好,但我想知道是否有一个奇特的Lambda表达式来替换该方法.像这样的东西:
Arrays.asList(standardAttributes.values()).forEach((attribute) -> standardAttributesList.add(attribute.toString()));
Run Code Online (Sandbox Code Playgroud)
两个问题: