-5

how can i extract year month and day from long date with "dd/MM/yyyy" format.

    long date = a.creationDate;

    SimpleDateFormat dateFormatNew = new SimpleDateFormat("dd/MM/yyyy");
    String formattedDate = dateFormatNew.format(date);

1 Answers1

0

If you want to extract the single values of year, month and day of month from a datetime given in milliseconds, you should nowadays use java.time for that.
See this example:

public static void main(String[] args) {
    // example millis of "now"
    long millis = Instant.now().toEpochMilli(); // use your a.creationDate; here instead
    // create an Instant from the given milliseconds
    Instant instant = Instant.ofEpochMilli(millis);
    // create a LocalDateTime from the Instant using the time zone of your system
    LocalDateTime ldt = LocalDateTime.ofInstant(instant, ZoneId.systemDefault());
    // then print the single parts of that LocalDateTime
    System.out.println("Year: " + ldt.getYear()
        + ", Month: " + ldt.getMonthValue()
        + " (" + ldt.getMonth().getDisplayName(TextStyle.FULL, Locale.ENGLISH)
        + "), Day: " + ldt.getDayOfMonth()
        + " (" + ldt.getDayOfWeek().getDisplayName(TextStyle.FULL, Locale.ENGLISH)
        + ")");
}

The output is this:

Year: 2020, Month: 3 (March), Day: 6 (Friday)

If you are supporting Android API levels below 26, you will, unfortunately, have to import a backport library, read this for instructions...

deHaar
  • 17,687
  • 10
  • 38
  • 51