简体   繁体   中英

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

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. 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:

  1. Input: 2021-03-10 16:37:02.4230000 => Desired Output: 2021-03-10 16:37:02.423
  2. Input: 2021-03-10 16:39:51.0000000 => Desired output: 2021-03-10 16:39:51.0
  3. Input: 2021-04-22 23:03:52.0234000 => Desired output: 2021-04-22 23:03:52.0234

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...

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

This can done using replaceAll method of String as well. 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

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

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:

input1 mili:2021-03-10 16:39:51.0
input2 mili:2021-03-10 16:37:02.423

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM