Joda-Time Library Specifying mili second precision

Viewed 161

I am converting the date and time string to Joda DateTime object, using this snipped of the code -

String time = "124204";
String date = "05/09/25";
DateTimeFormatter formatter = DateTimeFormat.forPattern("dd/mm/yyhhmmss").withZone(DateTimeZone.UTC);
String dateInString = date.concat(time);
DateTime dateTime = DateTime.parse(dateInString, formatter);
System.out.println(dateTime);

I am getting the ouput as - 2025-01-05T00:42:04.000Z

But I want it as - 2025-01-05T00:42:04.000000Z

I want the mili second precision to be 6 digits.

How can I do it? Thanks for the help.

3 Answers

You are confused.

You seem to think you have a string. You don't; you have an object of type DateTime. What you are seeing is the result of invoking toString() on one of these objects, and, like all toString() methods, this is just a debugging aid. It's for your developers' eyeballs and absolutely nothing else (and if you're using it for something else, stop doing that).

If you want the output in some specific format, then make a second DateTimeFormat object and use that. You're using your first to turn strings into datetime objects (the parse method). These formatter objects go both ways; they can also turn datetime objects back into strings, using the format method. Do that. Make the pattern you want, call .format on it handing it the dateTime object you have.

NB: Why are you using jodatime? It is obsoleted by the java.time package, which is written by the same author as jodatime.

As long as you are on at least Java 8, use java.time:

    String time = "124204";
    String date = "05/09/25";
    DateTimeFormatter parser = DateTimeFormatter.ofPattern("dd/MM/yyHHmmss").withZone(ZoneId.of("UTC"));
    String dateInString = date.concat(time);
    ZonedDateTime dateTime = ZonedDateTime.parse(dateInString, parser);
    DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSSSSSX");
    System.out.println(dateTime.format(formatter));

This will print 2025-09-05T12:42:04.000000Z. Not sure if you really meant to use the 12-hour format in your input, and I have no experience with it, so I have ignored that.

Joda-Time solution (outdated)

You may not need those 6 decimals on the seconds in your result. You may want to consider that question again. The format you are asking for is ISO 8601. The ISO 8601 format does not dictate the number of decimals but leaves it to be agreed between the parties exchanging the data. Most implementations accept varying number of decimals (up to some limit, but providing fewer than the limit is no problem in the cases I have met).

The others are correct that you may very well consider upgrading to java.time, the successor of Joda-Time. In case you cannot afford to do that right now, here’s a way of obtaining the string that you want with Joda-Time.

    String time = "124204";
    String date = "05/09/25";

    DateTimeFormatter timeFormatter = DateTimeFormat.forPattern("HHmmss");
    DateTimeFormatter dateFormatter = DateTimeFormat.forPattern("dd/MM/yy");

    LocalTime lt = LocalTime.parse(time, timeFormatter);
    LocalDate ld = LocalDate.parse(date, dateFormatter);
    
    DateTime dateTime = ld.toLocalDateTime(lt).toDateTime(DateTimeZone.UTC);
    
    DateTimeFormatter resultFormatter
            = DateTimeFormat.forPattern("yyyy-MM-dd'T'HH:mm:ss.SSSSSSZZ");
    
    String dateTimeString = dateTime.toString(resultFormatter);
    
    System.out.println(dateTimeString); 

Output:

2025-09-05T12:42:04.000000+00:00

I noticed that you asked for 2025-01-05T00:42:04.000000Z. Maybe you had not noticed that hour of day in your time string was 12 (not 00) and the month in your date string was 09 (not 01). In your own code you got incorrect hour and month because you used lower case pattern letters throughout in your format pattern. We need upper case HH for hour of day and upper case MM for month.

Edit: You also wanted Z rather than +00:00 to denote UTC (offset zero from UTC) like you get from the no-arg DateTime.toString(). Again ISO 8601 allows both, so it may not matter. I was surprised, though, not to find an easy way to get Z. The following formatter gives it, but I may be missing something simpler.

    DateTimeFormatter resultFormatter = new DateTimeFormatterBuilder()
            .append(ISODateTimeFormat.dateHourMinuteSecond())
            .appendPattern(".SSSSSS")
            .appendTimeZoneOffset("Z", true, 2, 2)
            .toFormatter();

2025-09-05T12:42:04.000000Z

I have also used a different approach to parsing. While concatenating your two strings and parsing the result works, I find it clearer to parse each string separately.

As others have said, when you ask for a specific format, for example a specific number of decimals on the seconds, you can have that format only in a string. So the solution to that is to format your DateTime into a string.

Usage: A millisecond is a 1000th of a second. So asking for 6 digits of milliseconds does not make sense and would not give you higher precision. I took your question to mean 6 decimals of fraction of second — also known as microseconds (millionths of second).

Links

Related