-6

how to convert ISO_LOCAL_DATE to date time format : yyyy-MM-dd'T'HH:mm:ss.SSSZ in java

Ex: given date: 2016-01-25 to 2016-01-25T00:00:00.000+0100

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
Viswanath Nuggu
  • 63
  • 1
  • 1
  • 9
  • 2016-01-25 is likely to be a String. Check the date object it came from – Stultuske Mar 22 '21 at 08:11
  • Easy, just use string concatenation: `s = "2016-01-25"; s += "T00:00:00.000+0100";` – Andreas Mar 22 '21 at 08:41
  • Do you always want the time of day to be 00:00:00.000? What do you want the UTC offset to be (+0100 in your example)? And is `2016-01-25` a string? A `LocalDate`? Something else? – Ole V.V. Mar 22 '21 at 10:33
  • Probably not, @blackgreen. Java and JavaScript are vastly different languages. But you are right that there are a lot of related helpful questions out there, the OP would benefit greatly from using his search engine. – Ole V.V. Mar 22 '21 at 12:10

3 Answers3

2

I am assuming that have got a string, for example 2016-01-25, and that you want a string containing the start of the day in the JVM’s default time zone (it wasn’t clear from the question). I first define a formatter for the format that you want (it’s ISO 8601):

private static DateTimeFormatter formatter
        = DateTimeFormatter.ofPattern("uuuu-MM-dd'T'HH:mm:ss.SSSxx");

Now your conversion goes:

    String isoLocalDateString = "2016-01-25";
    LocalDate date = LocalDate.parse(isoLocalDateString);
    ZonedDateTime dateTime = date.atStartOfDay(ZoneId.systemDefault());
    String dateTimeString = dateTime.format(formatter);
    System.out.println(dateTimeString);

When running in my time zone, Europe/Copenhagen, output from this example code is what you asked for:

2016-01-25T00:00:00.000+0100

In rare cases where summer time (DST) begins at the first moment of the day, the time of day will not be 00:00:00.000.

For parsing with ISO_LOCAL_DATE we don’t need to specify the formatter since this formatter is the default for LocalDate.parse().

All of this said, you should not normally want to convert a date from one string format to another string format. Inside your program keep dates as LocalDate objects. When you get string input, parse into a LocalDate. Only when you need to give string output, for example in data exchange with another system, format into a string in the required format.

Link: Wikipedia article: ISO 8601

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
1

There are various methods on LocalDate for this, including:

  • LocalDate::toDateTimeAtCurrentTime()
  • LocalDate::toDateTimeAtStartOfDay()
  • LocalDate::toDateTime( LocalTime )
  • LocalDate::toDateTime( LocalTime , DateTimeZone )
Ankit Singh
  • 21
  • 1
  • 7
  • I think that the OP intended to ask about java.time, the modern Java date and time API, and I think your answer is about Joda-Time. Still +1 for showing that there are different ways that give different results. – Ole V.V. Mar 22 '21 at 10:48
0

It is as simple as LocalDateTime localDateTime = yourLocalDate.atStartOfDay()

Update Adding timestamp is as simple as:

ZoneId zoneId = ZoneId.of("America/New_York");
ZonedDateTime = zdt = localDateTime.atZone(zoneId);

Can be put together as

ZonedDateTime zdt = yourLocalDate.atStartOfDay().atZone(ZoneId.of("America/New_York"));
pedrohreis
  • 1,030
  • 2
  • 14
  • 33