现在我真的很困惑,为什么以下代码片段导致DateTimeParseException。
public static void main(String[] args) {
java.time.format.DateTimeFormatter dtf = java.time.format.DateTimeFormatter.ofPattern("EEE, dd MMM yyyy HH:mm:ss zzz");
String date = "Mon, 10 Sep 2018 23:57:09 UTC";
System.out.println(dtf.parse(date));
}
Run Code Online (Sandbox Code Playgroud)
引发以下异常:
Exception in thread "main" java.time.format.DateTimeParseException: Text 'Mon, 10 Sep 2018 23:57:09 UTC' could not be parsed at index 2
at java.time.format.DateTimeFormatter.parseResolved0(DateTimeFormatter.java:1949)
at java.time.format.DateTimeFormatter.parse(DateTimeFormatter.java:1777)
at com.sample.binding.bitronvideo.driver.BitronVideoRecordingDriver.main(BitronVideoRecordingDriver.java:448)
Run Code Online (Sandbox Code Playgroud)
我将非常感谢您的进一步帮助。
谢谢阿米特
我有一种计算器:
sum = value1 + value2;
multiplication = value1 * value2;
division = (value1 / value2);
average = (sum / 2);
if (operation.compareToIgnoreCase("add") == 0)
result = sum;
else if (operation.compareToIgnoreCase("mult") == 0)
result = multiplication;
else if (operation.compareToIgnoreCase("div") == 0)
result = division;
else if (operation.compareToIgnoreCase("avg") == 0)
result = average;
Run Code Online (Sandbox Code Playgroud)
我必须将其转换为lambda表达式:
result = (request.getValue1(), request.getValue2()) -> {
if (request.getOperation().compareToIgnoreCase("add") == 0) return request.getValue1() + request.getValue2();
else if (request.getOperation().compareToIgnoreCase("mult") == 0) return request.getValue1() * request.getValue2();
else if (request.getOperation().compareToIgnoreCase("div") == …Run Code Online (Sandbox Code Playgroud) 在解决代码战中的kata时,我遇到了将二进制数(以列表形式)转换为整数的问题的单行解决方案.我无法理解人们使用java stream api reduce函数的解决方案.请帮我理解.
例如:[0,0,0,1]被视为0001,它是1的二进制表示.
import java.util.List;
public class BinaryArrayToNumber {
public static int ConvertBinaryArrayToInt(List<Integer> binary) {
return binary.stream().reduce((x, y) -> x * 2 + y).get();
}
}
Run Code Online (Sandbox Code Playgroud) 我需要使用lambda表达式迭代HashMap的HashMap并过滤掉一些不需要的条目.我尝试了一些方法,但似乎没有用.以下是地图的结构.
Map<String, Map<Date, String>> sensor_tags = new HashMap<String, Map<Date,String>>();
Run Code Online (Sandbox Code Playgroud)
从这张地图中,我需要删除传感器数据早于特定日期的条目(日期是内部地图的关键).以下是地图sensor_tags-的示例数据
String tagName = "zoneSensor";
Map<Date, String> values= new HashMap<Date, String>();
// (1st entry for tag 1) --> date is day before yesterday
Calendar datekey1 = Calendar.getInstance();
datekey1.set(2018, 12, 01, 12, 30, 45);
values.put(datekey1.getTime(), "ON");
// (2nd entry for tag 1) --> date is yesterdys date
Calendar datekey = Calendar.getInstance();
datekey.set(2018, 12, 02, 12, 30, 45);
values.put(datekey.getTime(), "OFF");
// (3rd entry for tag 1) --> date is today
Calendar instance = …Run Code Online (Sandbox Code Playgroud) 我在Windows Server 2016上使用Java 1.8,我的代码包含如下测试条件:
String path = "\\myserver\folder";
File file = new File(path);
if(file.isDirectory())
System.out.println("is a dir");
else
System.out.println("is not a dir");
Run Code Online (Sandbox Code Playgroud)
但总是返回值为false.我尝试了映射,创建了一个符号链接.我也尝试了java nio但没有改变.有谁知道为什么?它是Windows或Java的错误吗?
谢谢
我有一个用例,我想根据我在元素上执行的网络调用过滤出列表中的一些元素.为了实现这一点,我使用流,过滤器和Completable Future.目标是执行异步执行,以便操作变得高效.下面提到伪代码.
public List<Integer> afterFilteringList(List<Integer> initialList){
List<Integer> afterFilteringList =initialList.stream().filter(element -> {
boolean valid = true;
try{
valid = makeNetworkCallAndCheck().get();
} catch (Exception e) {
}
return valid;
}).collect(Collectors.toList());
return afterFilteringList;
}
public CompletableFuture<Boolean> makeNetworkCallAndCheck(Integer value){
return CompletableFuture.completedFuture(resultOfNetWorkCall(value);
}
Run Code Online (Sandbox Code Playgroud)
我在这里遇到的问题是,我是否以异步方式进行此操作?(因为我在过滤器中使用'get'函数会阻止执行并使其仅为顺序)或者是否有更好的方法使用Java 8中的Completable Future和Filters以异步方式执行此操作.
final Timestamp rawDateTime = Timestamp.valueOf("2031-04-25 18:30:00");
final ZoneId zoneId = ZoneId.of("Asia/Calcutta");
final ZonedDateTime zonedDateTime = ZonedDateTime.ofInstant(
Instant.ofEpochMilli(rawDateTime.getTime()), zoneId);
// here we are getting output as 2031-04-25T18:30+05:30[Asia/Calcutta]
final ZonedDateTime zonedDateTime1 =
ZonedDateTime.of(rawDateTime.toLocalDateTime(), zoneId);
// here we are getting output as 2031-04-25T18:30+05:30[Asia/Calcutta]
Run Code Online (Sandbox Code Playgroud)
但我希望将转换日期时间设置为2031-04-26 00:00:00 + 5:30,因为我的时间戳值在UTC时区中.
请帮忙.
如何在Java 8中转换Stream<Character>成A String?Collectors.joining()期望CharSequence因此它给编译错误。
我试图在Java中导入SecureRamdom,但是
import java.security.SecureRandom;
Run Code Online (Sandbox Code Playgroud)
不工作。我在Eclipse中使用Java SE 8。有人知道如何导入吗?
我想将转换List<String>为IntStream。假设我的清单是["abc", "de", "fghi"]。然后IntStream,我要像1,1,1,2,2,3,3,3,3。(一些出现的次数i在IntStream取决于长度i个串中的给定的列表)
为此,我编写了以下方法(它将不会编译):
private static IntStream getSingleIntStream(List<String> list) {
final AtomicInteger atomicInteger = new AtomicInteger();
return list.stream()
.map(str -> {
atomicInteger.incrementAndGet();
return IntStream.range(0, str.length())
.map(i -> atomicInteger.get());
}); // Now I don't understand how can I convert this Stream<IntStream> to a single IntStream
}
Run Code Online (Sandbox Code Playgroud)
但是我不明白如何将a转换Stream<IntStream>为单个IntStream。(我的猜测是我们可以flatMap以某种方式使用,但我不完全了解如何使用它。)
我使用IntStream而不是Stream<Integer>避免自动装箱,并使我的整个系统更高效。