26

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);
Arvind Kumar Avinash
  • 50,121
  • 5
  • 26
  • 72
Cork Kochi
  • 1,364
  • 1
  • 22
  • 36

6 Answers6

30

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.

Rodrigue
  • 3,465
  • 2
  • 36
  • 49
assylias
  • 297,541
  • 71
  • 621
  • 741
  • 3
    The service I am using is returning a java.util.Date object – Cork Kochi Feb 13 '17 at 18:39
  • 1
    @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 )`](https://docs.oracle.com/javase/8/docs/api/java/util/Date.html#from-java.time.Instant-) and [`Date::toInstant()`](https://docs.oracle.com/javase/8/docs/api/java/util/Date.html#toInstant--). – Basil Bourque Feb 13 '17 at 20:56
1
DateTime dt = new DateTime(date);
DateTimeFormatter dtf = DateTimeFormat.forPattern("yyyy-MM-dd'T'HH:mm:ss");
dt.toString(dtf)
Madbreaks
  • 17,159
  • 7
  • 50
  • 64
Cork Kochi
  • 1,364
  • 1
  • 22
  • 36
0

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 );
ΦXocę 웃 Пepeúpa ツ
  • 43,054
  • 16
  • 58
  • 83
0
DateTimeFormatterOBJECT=DateTimeFormatter.ofPattern("DD/MMM/YYYY HH//MM/SS");

String MyDateAndTime= LocalDate.now().format(DateTimeFormatterOBJECT);
Eric
  • 6,347
  • 5
  • 37
  • 61
  • 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 ; – RANA DINESH Jan 18 '18 at 17:23
  • 4
    To add clarifications to your answer, please edit your answer instead of using comments. – Hexfire Jan 18 '18 at 17:45
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));
dkb
  • 3,238
  • 3
  • 26
  • 41
Archmede
  • 1,070
  • 1
  • 12
  • 28
  • 2
    You can also use `withZoneUTC()` instead of `withZone(DateTimeZone.UTC)` which results in an unnecessary import of `DateTimeZone` class. – nabster Feb 10 '19 at 21:07
0
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));
  • 1
    While this code may provide a solution to the question, it's better to add context as to why/how it works. This can help future users learn and eventually apply that knowledge to their own code. You are also likely to have positive-feedback/upvotes from users, when the code is explained. – Amit Verma Feb 13 '21 at 09:00