java.util.date to String using DateTimeFormatter
Asked Answered
N

7

36

How can I convert a java.util.Date to String using

 DateTimeFormatter dateTimeFormatter = DateTimeFormat.forPattern("yyyy-MM-dd'T'HH:mm:ss")

The Date object which I get is passed

DateTime now = new DateTime(date);
Naresh answered 13/2, 2017 at 17:46 Comment(12)
Please show your complete codeRichart
Possible duplicate of: #5684228Ctenophore
@PeterKuebler I am trying to use DateTimeFormatterNaresh
Why not use SimpleDateFormat?Sibeal
new DateTime(date).toString(dateTimeFormatter)Sibeal
@CorkKochi If you are on Java 8 you should consider using the new java time api instead of the legacy Date api or joda time.Phraseologist
not clear for me, is DateTimeFormatter comming from java.time.format.DateTimeFormatter; or org.joda.time.format.DateTimeFormatter???Shadow
I assume it's Joda, but I don't know why it's tagged java-8.Sibeal
@Sibeal Any possibilities to do in java8Naresh
It's not clear what you're trying to do. Joda, Java and Java 8 have separate date libraries, with their own formatting and parsing mechanisms, all of which are well documented on this site and elsewhere. What exactly are you looking for?Sibeal
@Sibeal can you tell how it can be done in java8, I searched but I couldnt findNaresh
FYI: The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.Louielouis
P
41

If you are using Java 8, you should not use java.util.Date in the first place (unless you receive the Date object from a library that you have no control over).

In any case, you can convert a Date to a java.time.Instant using:

Date date = ...;
Instant instant = date.toInstant();

Since you are only interested in the date and time, without timezone information (I assume everything is UTC), you can convert that instant to a LocalDateTime object:

LocalDateTime ldt = instant.atOffset(ZoneOffset.UTC).toLocalDateTime();

Finally you can print it with:

DateTimeFormatter fmt = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss");
System.out.println(ldt.format(fmt));

Or use the predefined formatter, DateTimeFormatter.ISO_LOCAL_DATE_TIME.

System.out.println(ldt.format(DateTimeFormatter.ISO_LOCAL_DATE_TIME));

Note that if you don't provide a formatter, calling ldt.toString gives output in standard ISO 8601 format (including milliseconds) - that may be acceptable for you.

Phraseologist answered 13/2, 2017 at 18:37 Comment(3)
The service I am using is returning a java.util.Date objectNaresh
@CorkKochi If you must use a java.util.Date object, convert as shown in this Answer. See new conversion methods added to the old classes, such as Date.from( Instant ) and Date::toInstant().Louielouis
There is nothing wrong with Date. It is simply a wrapper around an epoch timestamp.Sake
P
9
java.util.Date date = new java.util.Date(System.currentTimeMillis());
Instant instant = date.toInstant();

LocalDateTime ldt = instant.atZone(ZoneId.systemDefault()).toLocalDateTime();

DateTimeFormatter fmt = DateTimeFormatter.ofPattern("dd.MM.yyyy HH:mm:ss");
System.out.println(ldt.format(fmt));
Pigling answered 13/2, 2021 at 7:19 Comment(0)
E
2

Summarizing and simplifying @assylias and @Adrian Faur:

 dateTimeFormatter.format(date.toInstant().atZone(ZoneId.systemDefault()));

No need to convert it to LocalDateTime, format accept TemporalAccessor

Elli answered 27/9, 2023 at 20:57 Comment(0)
N
0
DateTime dt = new DateTime(date);
DateTimeFormatter dtf = DateTimeFormat.forPattern("yyyy-MM-dd'T'HH:mm:ss");
dt.toString(dtf)
Naresh answered 13/2, 2017 at 18:23 Comment(1)
dt.toString(DateTimeFormatter.ISO_INSTANT)Harty
Y
0
DateTimeFormatterOBJECT=DateTimeFormatter.ofPattern("DD/MMM/YYYY HH//MM/SS");

String MyDateAndTime= LocalDate.now().format(DateTimeFormatterOBJECT);
Yoshida answered 18/1, 2018 at 17:18 Comment(2)
You can use above code to get simple date in your own format by specifying the format in first line of code and you can also use any plus or minus date function in 2nd line of code ;Yoshida
To add clarifications to your answer, please edit your answer instead of using comments.Outleap
A
0

You can use the joda time formatter class:

import org.joda.time.DateTime;
import org.joda.time.DateTimeZone;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;
# conflict with import java.time.format.DateTimeFormatter;

final DateTimeFormatter formatter = DateTimeFormat.forPattern("yyyy-MM-dd'T'HH:mm:ss")
            .withZone(DateTimeZone.UTC);

    System.out.println(DateTime.now().toString(formatter));
Ansell answered 13/8, 2018 at 19:49 Comment(1)
You can also use withZoneUTC() instead of withZone(DateTimeZone.UTC) which results in an unnecessary import of DateTimeZone class.Inviolable
P
-1

since I asume you are using joda API: ergo, DateTimeFormatter is comming from org.joda.time.format.DateTimeFormatter:

 String dateTime = "02-13-2017 18:20:30";
// Format for input
DateTimeFormatter dtf = DateTimeFormat.forPattern("MM-dd-yyyy HH:mm:ss");
// Parsing the date
DateTime jodatime = dtf.parseDateTime(dateTime);

System.out.println(jodatime );
Pietrek answered 13/2, 2017 at 18:6 Comment(2)
@CorkKochi I hope you're not actually calling new DateTime(new Date()).Sibeal
@Sibeal I posted the answerNaresh

© 2022 - 2024 — McMap. All rights reserved.