小编Arv*_*ash的帖子

如何修改浮点数以显示至少 n 位数字

在 bash 中,如何修改 afloat以便点之前的部分至少有两位数?

我想让A 列中的数字显示为B 列

A (Current)   B (Desired)
-----         ------
8.456         08.456
4.19          04.19
3.5           03.5
Run Code Online (Sandbox Code Playgroud)

我做了很多搜索,但我发现的大部分内容都是关于如何显示带有N 位小数的数字(例如17.7647 to 17.76),这没有帮助。

bash digits

2
推荐指数
1
解决办法
50
查看次数

将 Javascript Date 对象转换为 Java LocalDateTime

我正在尝试从 Javascript 前端获取 UTC 时间到 Java 后端。我想通过使用Date.toISOString()该对象并将其发送到 Java 后端来完成此任务。然而,我注意到的一个问题是,toISOString()以格式返回时间戳YYYY-MM-DDTHH:mm:ss.sssZ,并且该格式与任何 Java 8 预定义的LocalDateTime格式化程序都不匹配。

我的问题是,是否有最佳实践来完成我想做的事情?我知道我可以编写一个自定义 Java 格式化程序来匹配 Javascript 输出。但是,我想知道是否有一种标准方法可以完成此任务,因为这似乎是一个非常常见的情况。

javascript java datetime datetime-format typescript

1
推荐指数
1
解决办法
7934
查看次数

如何将“2020-12-20T00:00:00.000Z”转换为java.util.Date?

我试着用

DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'", Locale.ENGLISH);
LocalDate date = LocalDate.parse(d.toString(), formatter);
Run Code Online (Sandbox Code Playgroud)

但它会引发错误。

有没有办法转换JSON默认时间戳?

java date datetime-parsing localdate datetimeformatter

1
推荐指数
1
解决办法
9022
查看次数

在 Java 中获取 UTC 时间戳

一个老堆栈溢出发帖建议得到Java中的UTC时间戳的方式如下:

Instant.now()   // Capture the current moment in UTC.
Run Code Online (Sandbox Code Playgroud)

不幸的是,这对我不起作用。我有一个非常简单的程序(转载如下),它展示了不同的行为。

在 Windows 上:时间是本地时间,并标有 GMT 的偏移量

在 Linux 上:时间再次是本地时间,并且为本地时区正确标记了时间


问题:我们如何在 Java 程序中显示 UTC 时间戳?


我的示例源代码如下:

import java.time.Instant;
import java.util.Date;

public class UTCTimeDisplayer {
    public static void main(String[] args) {
        System.out.println(System.getProperty("os.name"));
        Date currentUtcTime = Date.from(Instant.now());
        System.out.println("Current UTC time is " + currentUtcTime);
    }
}  
Run Code Online (Sandbox Code Playgroud)

窗口输出:

C:\tmp>java UTCTimeDisplayer
Windows 10
Current UTC time is Fri Jan 22 14:28:59 GMT-06:00 2021
Run Code Online (Sandbox Code Playgroud)

Linux输出:

/tmp> java UTCTimeDisplayer
Linux
Current UTC time …
Run Code Online (Sandbox Code Playgroud)

java timezone datetime date utc

1
推荐指数
2
解决办法
3645
查看次数

Exception in thread "main" java.util.regex.PatternSyntaxException: Illegal character range near index 9

I want to match alphanumeric words separated by the operators, +, -, *, /, <, > and ending with a semicolon. There can be whitespace characters in between e.g. the following strings should return true:

first + second;
first - second;
first * second;
first / second;
first;
first + second < third;
third < second * first;
Run Code Online (Sandbox Code Playgroud)

This is what I have tried:

first + second;
first - second;
first * second;
first / second;
first;
first + …
Run Code Online (Sandbox Code Playgroud)

java regex string

1
推荐指数
1
解决办法
122
查看次数

我如何将 String.split 转换为 int

    String nDate;
    String dateTemp;
    int i;

    nDate = kb.nextLine();
    String[] temp = nDate.split("-");
    int numDate = Integer.parseInt(String.valueOf(temp));
Run Code Online (Sandbox Code Playgroud)

我在最后一行的 (temp) 部分有问题。例如,如果我的输入是“06-21-2020”,我想要发生的是它变成“06212020”

java string date java-time localdate

1
推荐指数
1
解决办法
87
查看次数

我们可以从 java 中删除不需要的分隔符吗

我希望程序接受输入并从该字符串中删除分隔符,然后将其添加回来,然后我可以稍后将其解析为对象LocalDate,但我无法执行所需的操作。

Scanner darshit = new Scanner(System.in);
String oo = "";
System.out.println("Enter your DOB: ");
String dob = darshit.next();
String[] words = dob.split("\\D");
for (int i = 0; i > words.length; i++) {
    oo = oo + words[i];
}
System.out.println(oo);
Run Code Online (Sandbox Code Playgroud)

例如,输入 DOB 25-06-2008 后,输出应该是 25062008 或 2662008,但我得到的不是这个,而是一个空行!

java date

1
推荐指数
1
解决办法
434
查看次数

如何查找字符串上的索引并替换?

我们如何通过索引查找并用字符串替换它?(参见下面的预期产出)

我尝试了下面的代码,但它也替换了下一个字符串。(来自这些堆栈)

String.prototype.replaceAt = function(index, replacement) {
    return this.substr(0, index) + replacement + this.substr(index + replacement.length);
}

var hello = "hello world";
console.log(hello.replaceAt(2, "!!")); // He!!o World
Run Code Online (Sandbox Code Playgroud)

预期输出:

var toReplace = "!!";

1. "Hello World" ==> .replaceAt(5, toReplace) ===> "Hello!!World"
2. "Hello World" ==> .replaceAt(2, toReplace) ===> "He!!lo World"
Run Code Online (Sandbox Code Playgroud)

注意: toReplace变量可以是动态的。

javascript

1
推荐指数
1
解决办法
1589
查看次数

如何将这个字符串转换为数字数组?

我正在编写这段代码,其中用户在输入框中输入一个数组,因此该框几乎包含一个看起来有点像这样的字符串:[2, 2, 2, 2, 2, 9, 9, 9, 2, 2, 2], [2, 1, 1, 1, 1, 9, 9, 9, 1, 1, 2], [2, 1, 1,14, 1, 9, 9, 9, 1, 1, 2]等等。

我想将其转换为数字数组的数组,以便我的代码可以用它做一些事情,但我不确定如何做。

简单地获取输入框的值会输出如下所示的内容:'[2, 2, 2, 2, 2, 9, 9, 9, 2, 2, 2], \n[2, 1, 1, 1, 1, 9, 9, 9, 1, 1, 2], \n[2, 1, 1,14, 1, 9, 9, 9, 1, 1, 2]',当我尝试拆分它/正则表达式替换某些内容时,要么我什么也得不到,要么得到如下所示的数组:["2, 2, 2, 2, 2, 9, 9, 9, 2, 2, 2"] …

javascript arrays string

1
推荐指数
1
解决办法
57
查看次数

java中如何从当前月份获取上个月?

我有以下场景。

String currentMonth = SEP/2021
Run Code Online (Sandbox Code Playgroud)

在这里我想要String previous month = AUG/2021.

我怎样才能在java中实现这一点?

java calendar date java-time yearmonth

1
推荐指数
1
解决办法
927
查看次数