How to convert an Instant to a date format?

232,427

Solution 1

If you want to convert an Instant to a Date:

Date myDate = Date.from(instant);

And then you can use SimpleDateFormat for the formatting part of your question:

SimpleDateFormat formatter = new SimpleDateFormat("dd MM yyyy HH:mm:ss");
String formattedDate = formatter.format(myDate);

Solution 2

An Instant is what it says: a specific instant in time - it does not have the notion of date and time (the time in New York and Tokyo is not the same at a given instant).

To print it as a date/time, you first need to decide which timezone to use. For example:

System.out.println(LocalDateTime.ofInstant(i, ZoneOffset.UTC));

This will print the date/time in iso format: 2015-06-02T10:15:02.325

If you want a different format you can use a formatter:

LocalDateTime datetime = LocalDateTime.ofInstant(i, ZoneOffset.UTC);
String formatted = DateTimeFormatter.ofPattern("yyyy-MM-dd hh:mm:ss").format(datetime);
System.out.println(formatted);

Solution 3

try Parsing and Formatting

Take an example Parsing

String input = ...;
try {
    DateTimeFormatter formatter =
                      DateTimeFormatter.ofPattern("MMM d yyyy");
    LocalDate date = LocalDate.parse(input, formatter);
    System.out.printf("%s%n", date);
}
catch (DateTimeParseException exc) {
    System.out.printf("%s is not parsable!%n", input);
    throw exc;      // Rethrow the exception.
}

Formatting

ZoneId leavingZone = ...;
ZonedDateTime departure = ...;

try {
    DateTimeFormatter format = DateTimeFormatter.ofPattern("MMM d yyyy  hh:mm a");
    String out = departure.format(format);
    System.out.printf("LEAVING:  %s (%s)%n", out, leavingZone);
}
catch (DateTimeException exc) {
    System.out.printf("%s can't be formatted!%n", departure);
    throw exc;
}

The output for this example, which prints both the arrival and departure time, is as follows:

LEAVING:  Jul 20 2013  07:30 PM (America/Los_Angeles)
ARRIVING: Jul 21 2013  10:20 PM (Asia/Tokyo)

For more details check this page- https://docs.oracle.com/javase/tutorial/datetime/iso/format.html

Share:
232,427
hari m
Author by

hari m

Updated on June 04, 2020

Comments

  • hari m
    hari m almost 4 years

    I can convert a java.util.Date to a java.time.Instant (Java 8 and later) this way:

    Calendar cal = Calendar.getInstance();
    cal.set(Calendar.HOUR_OF_DAY, 8);
    cal.set(Calendar.MINUTE, 30);
    Date startTime = cal.getTime();
    Instant i = startTime.toInstant();
    

    Can anybody let me know about the convert that instant to date with a specific date & time format? i.e 2015-06-02 8:30:00

    I have gone through api but could not find a satisfactory answer.

  • ms_27
    ms_27 over 4 years
    Use DateTimeFormatter instead of SimpleDateFormat as it is not thread safe. I have faced some issue in past because of this. More details here
  • pdem
    pdem about 3 years
    Date and SimpleDateFormat are old pre java8 implementation, It should be avoided. The not thread safe thing is one of the problem, the non management of time zone and ofset is another one. But if you really want to face a time zone bug then stick to old Date implementation.
  • John Fantastico
    John Fantastico about 3 years
    Whenever saying "such and such should be avoided", try to provide the alternatives.