I get the date string from the server in this format: 2019-04-11 02:43:40

How can you compare the received date from the server, with the current date and time and show it in the application in the format: 6 минут назад or 8 часов назад ?

PS: The server records the date in the Moscow time zone. (GMT + 3)

PPS: I suppose that you first need to get the date from the device "in the device time zone", for example: 2019-04-11 02:45:00 GMT+4 , but then I don’t understand what to do = I don’t understand .. or there are ready-made libraries that can do it ??

And if my assumption is wrong, then correct me))

UPDATE

I can not get Moscow time using the Joda-time library:

 DateTimeZone timeZone = DateTimeZone.forID("Europe/Moscow"); DateTime nowMOSCOW = DateTime.now(timeZone); 

I get in the logs:

 I/System.out: SSS: 2019-04-10T23:01:55.869+03:00 

Not quite clear, the library and this code somehow depends on the time on the device? Or regardless of how much time is installed on my device and in what time zone I am, should I get Moscow time ??

  • About ready libraries, maybe there is. And at the moment I can suggest using SimpleDateFormat. Lead the date itself to the desired type. And the time 6 минут назад get through System.currentTimeMillis()-дата поста или что у вас - Dred
  • I do not know whether this is the best option, but you can use the jodatime library as in this answer . For declension (like 1 minute, 2 minutes, etc.) use plurals / Maybe someone will offer a better option - pavlofff
  • more solutions - pavlofff pm
  • SimpleDateFormat format = new SimpleDateFormat ("yyyy-MM-dd hh: mm: ss"); long minAgo = (new Date (). getTime () - format.parse (date). getTime ()) / 60000; Maybe I did not understand, but this code will allow you to get the difference in minutes. then output as you like ... - Dmitry
  • one
    The date to pass a string is bad practice, well, except for displaying on the frontend as it is. Much easier and more correct to work with numeric UTC representation - JavaJunior

0