Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find time is today or yesterday in android

Tags:

android

time

I am developing an application for sending SMS. I am storing the current time and showing in the sent history page by retrieving the time from the database. In the sent history page I want to display the time of the message was sent. Here I want to check that the message has been sent today or yesterday or the the day before yesterday like that. If the message was sent yesterday means then I need to display "Yesterday 20:00" like that and even the message was sent yesterday before means "Monday 20:00". I don't know how it has to be done. Please help me if anybody knows.

like image 961
Manikandan Avatar asked Oct 10 '12 11:10

Manikandan


People also ask

How can I get current time in Android?

text. DateFormat. getDateTimeInstance(). format(new Date()); // textView is the TextView view that should display it textView.

How can I get tomorrow date in Android?

get(Calendar. DAY_OF_MONTH) + 1; will it display tomorrow's date. or just add one to today's date? For example, if today is January 31.


1 Answers

To check if date is today, use Android utils library

DateUtils.isToday(long timeInMilliseconds) 

This utils class also offers human readable strings for relative times. For example,

DateUtils.getRelativeTimeSpanString(long timeInMilliseconds) -> "42 minutes ago" 

The are several parameters you can play with to define how precise the time span should be

See DateUtils

like image 64
Maragues Avatar answered Sep 19 '22 03:09

Maragues