Человекочитаемый в Android Java

Например

Дата конвертации: 10 05 2020, 01:00:00. Текущая дата: 20 05 2020, 01:00:00.

Должно отображаться 10 дней назад.

Я использую класс календаря

Calendar cal = Calendar.getInstance();


person dAv dEv    schedule 17.09.2020    source источник
comment
Не используйте Calendar, используйте LocalDateTime, OffsetDateTime или ZonedDateTime...   -  person deHaar    schedule 17.09.2020


Ответы (2)


Вы можете использовать для этого java.time, теперь доступен более низкий API версиях для Android из-за дешугаринга Android API:

public static void main(String[] args) {
    // create the datetime ten days ago
    LocalDateTime tenDaysBefore = LocalDateTime.of(2020, 5, 10, 1, 0, 0);
    // create the one that is used as "today"
    LocalDateTime current = LocalDateTime.of(2020, 5, 20, 1, 0, 0);
    // calculate the period between them (this only considers the date part)
    Period period = Period.between(tenDaysBefore.toLocalDate(), current.toLocalDate());
    // define a formatter for human readable output
    DateTimeFormatter outputDtf = DateTimeFormatter.ofPattern("EEEE, dd 'of' MMMM uuuu",
                                                                Locale.ENGLISH);
    // and output a meaningful sentence
    System.out.println(tenDaysBefore.format(outputDtf) + " was (approximately) "
                        + period.getDays() + " days ago assuming "
                        + current.format(outputDtf) + " is \"today\"");
}

Это выводит

Sunday, 10 of May 2020 was (approximately) 10 days ago assuming Wednesday, 20 of May 2020 is "today"
person deHaar    schedule 17.09.2020

Попробуй это:

Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.DATE, calendar.get(Calendar.DATE) - 10);
System.out.println(DateFormat.format("yyyy-MM-dd hh:mm:ss", calendar).toString());

Я использую это. Это автоматически удалит 10 дней с текущей даты.

Выход: I/System.out: 2020-09-07 12:51:50

person Akshay Kalola    schedule 17.09.2020