We need a method that receives the date and time of an event at the entrance, it compares it with the current one and says: "The event will happen tomorrow / day after tomorrow / yesterday / in an hour"

This is already on Vk.com ("I came 15 minutes ago").

Has anyone done this before? Are there any guides how to do it concisely?)

Thank you in advance!

  • Laconic? Forget it! To begin with, you will have to make a distinction between “12 minutes ago”, “22 minutes s ago” and “1 minutes at the back”. Do you think language will do it for you? Hands, all hands. - VladD
  • @VladD As for pure Java, there are ready-made libraries, like ICU . And in Android, for example, the mechanism built into the SDK is present . So it's not that straight "all by hand". - pavlofff
  • EnSO reference - pavlofff
  • @pavlofff: Well, yes, there are libraries for plural forms, but this is only the first step. First you need to strictly define the logic, and then take and stupidly implement it. There is no magic, pure work. "Yesterday"? "A week ago"? "Last Thursday"? "At Easter"? All rules need to be specified by yourself. There can be no ready-made set, because everyone has their own requirements. For example, if this is an online store customer, then the stronghold “from the date of your last visit” may be interesting. - VladD
  • @pavlofff: Well, it makes sense to connect a rather cumbersome ICU if you need support for multiple languages. If everything is limited to Russian, it's easier to code with your hands. - VladD

2 answers 2

Maybe a person just doesn’t know how to get the time difference between two detectors in different time intervals (days, hours ...)

If the old version is java 7 and lower, then use jodatime . There are many methods for calculating the date and time. So for example you can count how many days before the event.

 Date event = ...; Date now = ...; // перейти в jodatime LocalDate localEvent = new LocalDate(event); LocalDate localNow = new LocalDate(now); Days days = Days.daysBetween(localNow, localEvent); int d = days.getDays(); // сколько дней до события 

Directly tracing from the official FAQ
There are also methods for calculating the number of hours, minutes, etc.

 if (d == -1) { System.out.println("Вчера"); } else if (d == 1) { System.out.println("Завтра"); } else if (d == 2) { System.out.println("Послезавтра"); } else if (d > 0) { System.out.printf("Через %d дня\n", d); } else if (d == 0) { // подсчитать часы. только потребуется перевод в LocalDateTime или LocalTime } 

The new java 8 seems to have borrowed a lot from jodatime.

Here's another thing. LocalData from jodatime takes into account the timezone and new LocalDate (date) may differ from the date (+ | - day). Then you can use the method LocalDate.fromDateFields (Date date)

 LocalDate local = LocalDate.fromDateFields(date) 

Then timezone is ignored, local will show the exact same day as date.

    I suppose that a certain time difference between the current time and the date and between those indicated in some event is matched with some intervals of time gap, which corresponds to a certain phrase or just the output of the date of the event and so on.