我想解析这样的日期字符串2011-11-30:
LocalDateTime.parse("2011-11-30", DateTimeFormatter.ISO_LOCAL_DATE)
Run Code Online (Sandbox Code Playgroud)
但我得到以下异常:
java.time.format.DateTimeParseException: Text '2011-11-30' could not be parsed:
Unable to obtain LocalDateTime from TemporalAccessor
Run Code Online (Sandbox Code Playgroud)
如果我传递一个日期和时间字符串,一切都按预期工作:
LocalDateTime.parse("2011-11-30T23:59:59", DateTimeFormatter.ISO_LOCAL_DATE_TIME)
Run Code Online (Sandbox Code Playgroud)
如何将日期解析为2011-11-30LocalDateTime(具有默认时间)?
我有以下格式的日期:1/1/2020 3:4:7 AM我正在尝试使用DateTimeFormatter.
我有以下带有格式化程序的代码来解析它,但它不起作用。
LocalDateTime date = LocalDateTime.parse("1/1/2020 3:4:7 AM", DateTimeFormatter.ofPattern("MM/dd/yyyy hh:mm:ss a"));
Run Code Online (Sandbox Code Playgroud)
我收到以下异常:
java.time.format.DateTimeParseException: Text '1/1/2020 3:4:7 AM' could not be parsed at index 0
谁能帮我?
java java-time localdatetime datetimeformatter java.time.localdatetime
我有一个工作单元列表,我想并行处理它们。每个单元工作时间为 8-15 秒,完全计算时间,无 I/O 阻塞。我想要实现的是ExecutorService:
就像是:
Queue<WorkResult> queue = new ConcurrentLinkedDeque<>();
ExecutorService service = ....
for(WorkUnit unit : list) {
service.submit(() -> {
.. do some work ..
queue.offer(result);
);
}
while(queue.peek() != null) {
... process results while they arrive ...
}
Run Code Online (Sandbox Code Playgroud)
我尝试但没有成功的是:
newCachedThreadPool()创建了太多线程new ThreadPoolExecutor(0, 20, 60L, SECONDS, new SynchronousQueue<>()),但后来我注意到由于同步队列,submit() 被阻塞new LinkedBlockingQueue(),只是为了发现 ThreadPoolExecutor 只生成一个线程我确信有官方的实现来处理这个非常基本的并发用例。有人可以建议吗?
在获取两个分隔符之间的所有字符串时,我遇到了一个小问题。正如我在其他相关问题上看到的那样,在两个分隔符之间获取字符的(之一)方法如下:给定一些字符串,在我的示例中"void foo(int a, int b) {",我想获取括号之间的每个字符。使用:
String parameters = currLine.trim().substring(currLine.indexOf("("),
currLine.indexOf(")")-1);
Run Code Online (Sandbox Code Playgroud)
currLine当然是哪里"void foo(int a, int b) {"。现在一切正常,因为我得到了"int a, int b"字符串。问题是使用如下字符串:
void foo ( int a , String b ) {
Run Code Online (Sandbox Code Playgroud)
我明白了:参数 ="nt a , String b ) "
我不知道如何解决这个问题而不会给第一种情况造成问题。
使用Java 11, 对于此代码:
String[] arrayString = {"foo", "bar"};
Run Code Online (Sandbox Code Playgroud)
SonarLint 说 Declare this local variable with "var" instead.
所以,我试过:
var arrayString = {"foo", "bar"};
// or
var[] arrayString = {"foo", "bar"};
Run Code Online (Sandbox Code Playgroud)
但现在我收到这些错误:
Array initializer needs an explicit target-type'var' is not allowed as an element type of an array如何正确声明数组变量或属性。
我有一个表,我想用 awk 过滤。这是我想要如何做到这一点的示例:
Berlin BG AD=14;CD=0.05
Cairo CE AD=9;CD=0.01
Toronto TC AD=23;CD=0.17
Sydney SA AD=2;CD=0.11
Tokyo TJ AD=19;CD=0.22
Run Code Online (Sandbox Code Playgroud)
我想根据 AD 值过滤字段,并输出该行中 AD 等于或大于 10 的所有字段。
结果应该是这样的:
Berlin BG AD=14;CD=0.05
Toronto TC AD=23;CD=0.17
Tokyo TJ AD=19;CD=0.22
Run Code Online (Sandbox Code Playgroud)
我尝试过这个脚本:
awk '{if (awk '{print $3}' temp.txt | awk -F";" '{print $1}' | awk -F"=" '{print $2}' >= 10) print $0}' temp.txt
Run Code Online (Sandbox Code Playgroud)
但它给了我一个关于意外换行或字符串结尾的语法错误
我的角度应用程序中有以下用于邮政地址验证的正则表达式。
const regx = '\\b([p]*(ost)*\\.*\\s*[o|0]*(ffice)*\\.*\\s*b[o|0]x)\\b'
我只想让这个正则表达式通过
比赛名单:
但它也匹配
不匹配列表:
ETC。,
如何收紧此正则表达式,使其与“不匹配列表”不匹配?另外,我希望我的正则表达式能够针对邮政信箱或邮局箱等进行升级。有任何输入吗?
如何从字符串中提取开头字母?我想在遇到第一个非字母字符之前提取开头出现的字母。
例如,如果输入字符串是abcd045tj56,则输出应该是abcd
同样,如果输入是jkl657890,则输出应该是jkl
可以使用 awk/sed/cut 在 shell 脚本中完成吗?
我试过
echo "XYZ123" | awk 'sub(/[[:alpha:]]*/, "")'
Run Code Online (Sandbox Code Playgroud)
但它给出的是 123 而不是 xyz
然后我尝试了
echo "XYZ123" | awk '{print (/[[:alpha:]]*/)}'
Run Code Online (Sandbox Code Playgroud)
但它给出了 1
我希望答案是 XYZ
以下程序来自 Jeanne Boyarsky 和 Scott Selikoff 的 OCP 学习指南:
import java.util.*;
class WhaleDataCalculator {
public int processRecord(int input) {
try {
Thread.sleep(10);
} catch (InterruptedException e) {
// Handle interrupted exception
}
return input + 1;
}
public void processAllData(List<Integer> data) {
data.stream().map(a -> processRecord(a)).count();
}
public static void main(String[] args) {
WhaleDataCalculator calculator = new WhaleDataCalculator();
// Define the data
List<Integer> data = new ArrayList<Integer>();
for (int i = 0; i < 4000; i++)
data.add(i);
// Process the data …Run Code Online (Sandbox Code Playgroud) 如何使用 awk 打印最后 4 列?
以下解决方案适用于数字:
命令:
echo "1 2 3 4 5" | awk '{print $(NF-3),$(NF-2),$(NF-1),$NF}'
Run Code Online (Sandbox Code Playgroud)
输出:
2 3 4 5
Run Code Online (Sandbox Code Playgroud)
但是,当我使用带有如下特殊字符的文本时,我收到语法错误:
echo "2023-01-03 01:05:06: Table test completed (0 hrs 0 mins 0.009 Secs)" | awk '{print $(NF-3),$(NF-2),$(NF-1),$NF}'
Run Code Online (Sandbox Code Playgroud)
输出:
> " | awk '{print $(NF-2),$(NF-1),$NF}'
mins 0.009 Secs)
awk: The field -2 cannot be less than 0.
The input line number is 2.
The source line number is 1.
Run Code Online (Sandbox Code Playgroud)
我期望打印出上面示例的以下内容:
(0 hrs 0 mins 0.009 Secs)
Run Code Online (Sandbox Code Playgroud)
我怎样才能实现这个目标?