我正在使用ZonedDateTime和Java 8的DateTimeFormatter.当我尝试解析自己的模式时,它无法识别并抛出异常.
String oraceDt = "1970-01-01 00:00:00.0";
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.S");
ZonedDateTime dt = ZonedDateTime.parse(oraceDt, formatter);
Exception in thread "main" java.time.format.DateTimeParseException: Text '1970-01-01 00:00:00.0' could not be parsed: Unable to obtain ZonedDateTime from TemporalAccessor: {},ISO resolved to 1970-01-01T00:00 of type java.time.format.Parsed
at java.time.format.DateTimeFormatter.createError(DateTimeFormatter.java:1918)
at java.time.format.DateTimeFormatter.parse(DateTimeFormatter.java:1853)
at java.time.ZonedDateTime.parse(ZonedDateTime.java:597)
at com.timezone.Java8DsteTimes.testZonedDateTime(Java8DsteTimes.java:31)
at com.timezone.Java8DsteTimes.main(Java8DsteTimes.java:11)
Run Code Online (Sandbox Code Playgroud)
引起:java.time.DateTimeException:无法从TemporalAccessor获取ZonedDateTime:{},ISO解析为1970-01-01T00:00,类型为java.time.format.Parsed
运行代码后如
public static void main(String... args) throws Exception {
getUnsafe().getByte(0);
}
private static Unsafe getUnsafe() throws NoSuchFieldException, IllegalAccessException {
Field theUnsafe = Unsafe.class.getDeclaredField("theUnsafe");
theUnsafe.setAccessible(true);
return (Unsafe) theUnsafe.get(null);
}
Run Code Online (Sandbox Code Playgroud)
这会导致JVM崩溃,然后查看记录的输出,在Internal exceptions部分下显示一些奇怪的路径:
thrown at [C:\re\workspace\8-2-build-windows-amd64-cygwin\jdk8u60\4407\hotspot\srÚÞ ©Ö_®?
thrown at [C:\re\workspace\8-2-build-windows-amd64-cygwin\jdk8u60\4407\hotspot\src\share\vm\prims\j
Run Code Online (Sandbox Code Playgroud)
我绝对没有在我的本地机器上使用这些路径,并且一些谷歌搜索显示它们经常在这些故障转储中结束.我假设它们来自最初编译JDK的时候.
我的问题是 - 这是正确的吗?为什么这些信息会被整合到JDK中?纯粹是为了稍后调试?
Java 8 Stream API中的collect操作被定义为可以安全地并行执行的可变缩减,即使结果Collection不是线程安全的.
我们可以对Stream.toArray()方法说同样的话吗?
这个方法是一个可变的减少,即使Stream是并行流并且结果数组不是线程安全的,也是线程安全的吗?
我有一个unix时间戳列表,例如:
[1111111 1200000 1200060 1200120 1200180 1300000 1400000 140060]
Run Code Online (Sandbox Code Playgroud)
我希望按照彼此60秒内的簇对它们进行分组,其中键是第一个时间戳,例如:
{1111111=[1111111], 1200000=[1200000,120060, 1200120], 1300000=[1300000], 1400060=[1400000, 1400060]}
Run Code Online (Sandbox Code Playgroud)
我通过使用for循环实现了这一点,我希望有一种更好的方法,最好使用Java 8流.
(我对Java不太好,所以如果没有办法使用流,那么构造for循环是否更好?)
List <Integer> timestamps = new ArrayList<Integer>();
timestamps.add(1111111);
timestamps.add(1200000);
timestamps.add(1200060);
timestamps.add(1200120);
timestamps.add(1200180);
timestamps.add(1300000);
timestamps.add(1400000);
timestamps.add(1400060);
HashMap <Integer, List <Integer>> grouped = new HashMap<Integer, List <Integer>>();
List <Integer> subList = new ArrayList<Integer>();
for (int i = 0; i < timestamps.size(); i++) {
if(i > 0 && (timestamps.get(i - 1) + 60 < timestamps.get(i))) {
grouped.put(subList.get(0), new ArrayList <Integer>(subList));
subList.removeAll(subList);
}
subList.add(timestamps.get(i));
}
grouped.put(subList.get(0), …Run Code Online (Sandbox Code Playgroud) 要明确我没有任何问题,并且真的不需要帮助,但我还是想问:
假设我们有一个String数组
String[] sarr = new String[]{"POTATO", "TOMATO"};
Run Code Online (Sandbox Code Playgroud)
我们有一个枚举
public enum Food{POTATO, TOMATO, PIZZA}
Run Code Online (Sandbox Code Playgroud)
如果我想检查是否所有的字符串sarr中存在Food,我会做到以下几点:
ArrayList<String> foodstrings = new ArrayList<>();
Arrays.asList(Food.values()).forEach((in) -> foodstrings.add(in.toString()));
if (!foodstrings.containsAll(Arrays.asList(sarr))) doStuff();
Run Code Online (Sandbox Code Playgroud)
有没有办法在更少的代码行中做到这一点?或者只是一个更优雅的方式?
我有一个远在过去的约会.
我发现了这个日期和现在之间的持续时间.
现在我想知道 - 多年来这是多少?
我使用Java8 API提出了这个解决方案.
这是一个可怕的解决方案,因为我必须首先手动将持续时间转换为Days,因为UnsupportedTemporalTypeException否则会有- 否则LocalDate.plus(SECONDS)不支持任何原因.
即使编译器允许此调用.
转换Duration成年份的可能性是否较低?
LocalDate dateOne = LocalDate.of(1415, Month.JULY, 6);
Duration durationSinceGuss1 = Duration.between(LocalDateTime.of(dateOne, LocalTime.MIDNIGHT),LocalDateTime.now());
long yearsSinceGuss = ChronoUnit.YEARS.between(LocalDate.now(),
LocalDate.now().plus(
TimeUnit.SECONDS.toDays(
durationSinceGuss1.getSeconds()),
ChronoUnit.DAYS) );
/*
* ERROR -
* LocalDate.now().plus(durationSinceGuss1) causes an Exception.
* Seconds are not Supported for LocalDate.plus()!!!
* WHY OR WHY CAN'T JAVA DO WHAT COMPILER ALLOWS ME TO DO?
*/
//long yearsSinceGuss = ChronoUnit.YEARS.between(LocalDate.now(), LocalDate.now().plus(durationSinceGuss) );
/*
* ERROR -
* Still an …Run Code Online (Sandbox Code Playgroud) 我有一个Strings日期的数组,例如:
现在我想在此列表中找到最近的日期.为了做到这一点,我尝试将这些字符串反序列化为java.util.Date对象,然后比较它们.
java.util.Date对象生成的代码示例:
strDate = "Tue, 09 Feb 2016 14:07:00 GMT";
DateFormat format = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss z");
Date date;
try {
date = format.parse(strDate);
//Result: Tue Feb 09 16:07:00 IST 2016
System.out.println("Result: " + date.toString());
} catch(ParseException e) {
e.printStackTrace();
}
Run Code Online (Sandbox Code Playgroud)
我的问题:
EEE, dd MMM格式化的,SimpleDateFormat模式也是这种格式,因此,结果是EEE, …是否有任何Java 8 API静态方法Function在非null输入上运行,但nullValue在null输入上返回?
我自己可以很容易地编写这个方法,但如果它存在,我宁愿使用标准方法.
public static <T, R> R transform(final T t, final Function<T, R> rFromT, final R nullValue) {
return
t == null
? nullValue
: rFromT.apply(t)
;
}
// which can be called like:
final Number x = getNumberThatCouldBeNull();
final long y = transform(x, Number::longValue, 0L);
Run Code Online (Sandbox Code Playgroud) 我正在研究程序的一部分(关于语音识别和遥控车),其中代码transmit(XXXXX); disableAutoMode();重复多次.为了好奇,我想将其转换成一个类似的lambda函数var f = p -> transmit(p); disableAutoMode();(原谅var,我不知道这个表达式的类型是什么),然后把它在一个类似的方式:f("s");,f("a");和f("f");或类似的东西到f.call("s");,f.call("a");和f.call("f");.
在Java中使用简单的lambda函数的正确语法是什么,类似于我上面描述的?(我应该放下什么类型而不是说var?)
如果你很好奇,这是代码块:
@Override
public void onResult(Hypothesis hypothesis) {
if (hypothesis != null) {
String text = hypothesis.getHypstr();
Log.i("onresult",text);
ToastMaster(text);
switch (text) {
case "forward":
case "go forward":
transmit("f");
disableAutoMode();
break;
case "go back":
case "go backward":
case "back":
case "backward":
case "reverse":
transmit("b");
disableAutoMode();
break;
case "skid left":
case "go left":
transmit("l"); …Run Code Online (Sandbox Code Playgroud) 我有一个HashMap<Integer, Integer>,唯一键可以有重复的值.有没有办法将HashMap转换为Set<Integer>包含键和值的唯一整数的HashMap .
通过迭代keySet()和.values(),这绝对可以在两个循环中完成.我想知道这是否可以在java 8流中使用.