'How to convert from string format of date (dd/mm/yyyy) to string format of date (yyyy-mm-dd'T'HH:mm:ss.SSS)

I have to convert

"fromTime" : "04-10-2021"`

to

"fromTime" : "2021-10-04T00:00:00.000"

and

"toTime" : "06-10-2021"

to

"toTime" : "2021-10-06T23:59:59.000"

in java . Help please!



Solution 1:[1]

Generally speaking, you really want to avoid representing date/time as String where ever possible.

Displaying date/time values to the user should be done in styles that respect their localisation and transmitting date/time values using JSON or XML should use common/standardised formats. This reduces a LOT of issues (don't even get me started)

For example, had I the choice, I would prefer to use ISO_LOCAL_DATE (uuuu-MM-dd) and ISO_LOCAL_DATE_TIME (uuuu-MM-dd'T'HH:mm:ss). I'm already worried about the lack of time zone information ?

A "simple" approach might be to do something like...

DateTimeFormatter dateFormatter = DateTimeFormatter.ofPattern("dd-MM-uuuu");
DateTimeFormatter timeFormatter = DateTimeFormatter.ofPattern("dd-MM-uuuu.SSS");

LocalDateTime locatDateTime = LocalDate
        .parse("04-10-2021", dateFormatter)
        .atTime(0, 0, 0, 0);

String dateAndTime = locatDateTime.format(timeFormatter);
System.out.println(dateAndTime);

locatDateTime = LocalDate
        .parse("04-10-2021", dateFormatter)
        .atTime(23, 59, 59, 0);

dateAndTime = locatDateTime.format(timeFormatter);
System.out.println(dateAndTime);

Basically...

  • Parse the date value through a DateTimeFormatter using a matching format patten
  • Convert the LocalDate to a LocalDateTime by passing in the values we want to use
  • Format the LocalDateTime through a DateTimeFormatter to the desired format

which prints...

04-10-2021T00:00:00.000
06-10-2021T23:59:59.000

See:

for more details

Now, if all you really want to do, is make it "start of" and "end of" day, you might be able to use...

LocalDateTime startOfDay = LocalDate
        .parse("04-10-2021", dateFormatter)
        .atStartOfDay();

LocalDateTime endOfDay = LocalDate
        .parse("06-10-2021", dateFormatter)
        .plusDays(1)
        .atStartOfDay()
        .minusSeconds(1);

Right about now, I'd be starting a utility class of some kind which had a method which could take a date String (and possibly an optional format) and generate a LocalDate and then another method which could take LocalDate which could convert it to a LocalDateTime at either the startOfDay or endOfDay

Solution 2:[2]

You have to do these steps:

  1. create a DateTimeFormatter for dd-MM-yyyy
  2. parse the date with LocalDate.parse with atTime
  3. create a DateTimeFormatter for yyyy-MM-dd'T'HH:mm:ss.SSS
  4. format the date in order to have the final result
public String changeFormatDate(String date, int hour, int minute, int second, int nanoOfSecond) {
    DateTimeFormatter formatterFrom = DateTimeFormatter.ofPattern("dd-MM-yyyy"); // 1
    LocalDateTime localDateTimeFrom = LocalDate.parse(date, formatterFrom).atTime(hour, minute, second, nanoOfSecond); // 2

    DateTimeFormatter formatterTo = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSS"); // 3
    return localDateTimeFrom.format(formatterTo); //4
}

Call example:

String fromTime = changeFormatDate("04-10-2021", 0, 0, 0, 0);
String toTime = changeFormatDate("06-10-2021", 23, 59, 59, 999999999);

Solution 3:[3]

Here's an example using java.time:

public static void main(String[] args) {
    // input dates / days
    String fromDate = "04-10-2021";
    String toDate = "06-10-2021";
    // prepare the times of day
    LocalTime startOfDay = LocalTime.MIN; // 00:00:00.000
    LocalTime endOfDay = LocalTime.of(23, 59, 59); // 23:59:59.000
    // create an object that takes care of parsing the expected format
    DateTimeFormatter customDateParser = DateTimeFormatter.ofPattern("dd-MM-uuuu");
    // then parse the dates using the formatter / parser
    LocalDate fromDay = LocalDate.parse(fromDate, customDateParser);
    LocalDate toDay = LocalDate.parse(toDate, customDateParser);
    // then concatenate the parsed dates with the prepared times of day
    LocalDateTime fromTime = LocalDateTime.of(fromDay, startOfDay);
    LocalDateTime toTime = LocalDateTime.of(toDay, endOfDay);
    // finally define a formatter for the String output of the LocalDateTimes
    DateTimeFormatter customFormatter = DateTimeFormatter.ofPattern("uuuu-MM-dd'T'HH:mm:ss.SSS");
    // and print their toString() methods
    System.out.println("from " + fromTime.format(customFormatter) 
                     + " to " + toTime.format(customFormatter));
}
from 2021-10-04T00:00:00.000 to 2021-10-06T23:59:59.000

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2
Solution 3