0

Consider a string representation timestamp as "2017-06-05T19:27:10.917360". But if there are continous trailing zeros to above timestamp, Upstream system truncates zeros for second and milliseconds in above example like "2017-06 05T19:27:00.000000"
Is there any formatter available in java to add trailing zero in specific format including separator as : between hours, minutes and seconds.

For example: "2017-06-05" should convert to "2017-06-05T00:00:00.000000"
Edit:- Zeros can be get truncated from any of the field HH, MM, SS. If there are series of zeros at tail for time-stamp it will be truncated

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
donm
  • 1,160
  • 13
  • 20
  • Are you asking how to format a date in Java, suing a specific format? https://docs.oracle.com/javase/8/docs/api/java/time/format/DateTimeFormatter.html – JB Nizet Jan 23 '18 at 19:52
  • Nope, I have a string which needs to be converted to another string with different format. Also truncated zeros in string like this `"2017-06 05T19:27"` won't be parsed to Timestamp – donm Jan 23 '18 at 20:05
  • 1
    So, parse the string to a date, and format the date. Or, if all your strings are in the form yyyy-MM-dd'T'HH:mm, simply concatenate ":00.000000". If that's not what you want, please edit your question and clarify it. – JB Nizet Jan 23 '18 at 20:05
  • 1
    And what are you asking? How to *parse* a string in that format into a timestamp? If so, see first comment, or do a **web search** for how to parse string to date in Java. There are a gazillion examples out there. – Andreas Jan 23 '18 at 20:12
  • test the length of the string, and append the appropriate suffix. – JB Nizet Jan 23 '18 at 21:06
  • A tip, try to give your question proper tags to attract more attention from people who think they know something about the subject that you are asking about. – Ole V.V. Jan 28 '18 at 09:56

2 Answers2

1
    DateTimeFormatter desiredFormatter 
            = DateTimeFormatter.ofPattern("uuuu-MM-dd'T'HH:mm:ss.SSSSSS");
    String truncatedDateTimeString = "2017-06-05T19:27";
    LocalDateTime dateTime = LocalDateTime.parse(truncatedDateTimeString);
    String fixedDateTimeString = dateTime.format(desiredFormatter);
    System.out.println(fixedDateTimeString);

This prints

2017-06-05T19:27:00.000000

If also the minutes and hours are missing, we need some more trickery, though. Look into DateTimeFormatterBuilder and its parseDefaulting method. Use square brackets [] in the format pattern string to surround the parts that may be missing. I am not sure what your string will look like if the hours have been truncated — will the T be missing too?

On the other hand the above also works if the string was 2017-06-05T19:27:10.917360, and in this case just prints the same string back.

Also I am not sure which problem you are really trying to solve. Trailing zeroes are redundant, so what is the problem in them being truncated?

Edit: The following method fleshes out what I said about DateTimeFormatterBuilder, its parseDefaulting method and square brackets in the format pattern string:

public static String addTrailingZerosToTimestamp(String timeStamp) {
    DateTimeFormatter truncatedFormatter = new DateTimeFormatterBuilder()
            .appendPattern("uuuu-MM-dd['T'HH[:mm[:ss[.SSSSSS]]]]")
            .parseDefaulting(ChronoField.HOUR_OF_DAY, 0)
            .parseDefaulting(ChronoField.MINUTE_OF_HOUR, 0)
            .parseDefaulting(ChronoField.SECOND_OF_MINUTE, 0)
            .parseDefaulting(ChronoField.NANO_OF_SECOND, 0)
            .toFormatter();
    DateTimeFormatter desiredFormatter 
            = DateTimeFormatter.ofPattern("uuuu-MM-dd'T'HH:mm:ss.SSSSSS");
    LocalDateTime dateTime = LocalDateTime.parse(timeStamp, truncatedFormatter);
    return dateTime.format(desiredFormatter);
}

It works with 2017-06-05, 2017-06-05T19, 2017-06-05T19:27, 2017-06-05T19:27:10 and 2017-06-05T19:27:10.917360, but not with 2017-06-05T19:27:10.917.

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
  • It helped, but I have written logic for this to be sure about every case like missing `:`. – donm Feb 08 '18 at 22:29
  • I see your answer, and thanks for posting it. See my edit that takes `2017-06-05` and `2017-06-05T19` into account. My way of becoming sure about every case would be writing a unit test that tests all thinkable cases. – Ole V.V. Feb 09 '18 at 08:21
0

Used below code for the purpose

public static String addTrailingZerosToTimestamp(String timeStamp) {
    String param = timeStamp;
    String newCmitTime = "";
    if (param.length() < 26) {

        String currentCmitDate = param.substring(0, 10);
        if (param.length() <= 11) {
            param = currentCmitDate + "T" + "00:00:00.000000";

        } else {
            String currentCmitTime = param.substring(11, param.length());

            String[] timeSplit = currentCmitTime.split("\\:|\\.");

            switch (timeSplit.length) {
                case 4:
                    newCmitTime = currentCmitTime;
                    break;
                case 3:
                    newCmitTime = currentCmitTime + ".000000";
                    break;
                case 2:
                    newCmitTime = currentCmitTime + ":00.000000";
                    break;
                case 1:
                    newCmitTime = currentCmitTime + ":00:00.000000";
                    break;
                case 0:
                    newCmitTime = currentCmitTime + "00:00:00.000000";
                    break;
            }
            param = currentCmitDate + "T" + newCmitTime;
       }
    } else {
        return param;
    }
    return param;
}
donm
  • 1,160
  • 13
  • 20
  • If it covers your need and you are happy, so am I. It’s a bit much manual work for my taste and doesn’t seem to cover for example `2017-06 05T19:27:00.000` (3 decimals instead of 6), but as long as it covers the cases you actually meet… – Ole V.V. Feb 09 '18 at 07:49
  • This makes my eyes bleed. – gooboo Dec 21 '22 at 00:08