[英]Java convert DateTime from "yyyy-MM-dd HH:mm:ss.SSSSSSS" to "yyyy-MM-dd HH:mm:ss.S" or from "yyyy-MM-dd HH:mm:ss.SSSSSSS" to "yyyy-MM-dd HH:mm:ss.SSS"
Java 11 Java 11
I want to convert the date in "yyyy-MM-dd HH:mm:ss.SSSSSSS" to either "yyyy-MM-dd HH:mm:ss.S" or "yyyy-MM-dd HH:mm:ss.SSS" based on milliseconds value.我想将“yyyy-MM-dd HH:mm:ss.SSSSSSS”中的日期转换为“yyyy-MM-dd HH:mm:ss.S”或“yyyy-MM-dd HH:mm:ss”。 SSS”基于毫秒值。 If milliseconds are all zeros, then I just want single zero but if it is non-zero value then I want just value omitting trailing zeros.如果毫秒全为零,那么我只想要单个零,但如果它是非零值,那么我只想要省略尾随零的值。
Example:例子:
So, I started out with something like...所以,我开始了类似...
String text = "2021-03-10 16:37:02.4230000";
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSSSSSS");
LocalDateTime ldt1 = LocalDateTime.parse("2021-03-10 16:37:02.4230000", formatter);
DateTimeFormatter shortFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.S");
System.out.println(shortFormatter.format(ldt1));
LocalDateTime ldt2 = LocalDateTime.parse("2021-03-10 16:39:51.0000000", formatter);
System.out.println(shortFormatter.format(ldt2));
Which prints out...哪个打印出来...
2021-03-10 16:37:02.4
2021-03-10 16:39:51.0
Hmmm, not quite what we're looking for.嗯,不是我们要找的东西。
Lucky for us, there's the DateTimeFormatterBuilder
class. So next, I tried something like...幸运的是,有DateTimeFormatterBuilder
class。接下来,我尝试了类似...
DateTimeFormatter toFormatter = new DateTimeFormatterBuilder()
.appendPattern("yyyy-MM-dd HH:mm:ss")
.appendFraction(ChronoField.MILLI_OF_SECOND, 1, 9, true)
.toFormatter();
System.out.println(toFormatter.format(ldt1));
System.out.println(toFormatter.format(ldt2));
Which prints out...哪个打印出来...
2021-03-10 16:37:02.423
2021-03-10 16:39:51.0
Success成功
Now, please note, I've not really used DateTimeFormatterBuilder
before, so there might be some other, really super awesome cool way to achieve the same or better result, but hay, it's a nice start现在,请注意,我之前并没有真正使用过DateTimeFormatterBuilder
,所以可能还有其他一些非常棒的酷方法可以达到相同或更好的结果,但是,这是一个不错的开始
This can done using replaceAll
method of String as well.这也可以使用 String 的replaceAll
方法来完成。 you can remove the trailing zero's in the date string if there is no milisecond left after removing all trailing zero's, then append .0
如果删除所有尾随零后没有毫秒,则可以删除日期字符串中的尾随零,然后 append .0
private static String getMiliSecond(String input) {
// removing trailing zeros
String miliSecondTrimmed = input.replaceAll("[0.]*$", "");
// if there is there is no mili second left after removing zeros, append .0
return (!miliSecondTrimmed.contains("."))?(miliSecondTrimmed+(".0").toString()):miliSecondTrimmed;
}
This code called from main method此代码从 main 方法调用
public static void main(String[] args) {
String input1 ="2021-03-10 16:39:51.0000000";
String input2 = "2021-03-10 16:37:02.4230000";
String miliSecond1 = getMiliSecond(input1);
String miliSecond2 = getMiliSecond(input2);
System.out.println("input1 mili:"+miliSecond1);
System.out.println("input2 mili:"+miliSecond2);
}
Output: Output:
input1 mili:2021-03-10 16:39:51.0
input2 mili:2021-03-10 16:37:02.423
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.