I need to print date like today,Yesterday,2 days ago like that for that i have done
I am getting date like : String date1 = \"Thu Nov 13 19:01:25 GMT+05:30 2014\";
In you DateTimeFormatter, it expects a Date in a certain format, i.e. "EEE hh:mma MMM d, yyyy"
:
DateTime dateTime = DateTimeFormat.forPattern("EEE hh:mma MMM d, yyyy")
.parseDateTime(date);
But what you pass, is of format "MM/dd/yyyy"
:
SimpleDateFormat outputFormat1 = new SimpleDateFormat("MM/dd/yyyy");
op = outputFormat1.format(d);
op = formatToYesterdayOrToday(op);
So, what you could do is to change your DateTimeFormatter to expect you initial format "MM/dd/yyyy"
:
DateTime dateTime = DateTimeFormat.forPattern("MM/dd/yyyy")
.parseDateTime(date);
This should solve the error that you get. I don't know if the whole thing in the end gives you what you want ("today", "yesterday", "2 days ago", ...).
DateTimeFormatter formatter = DateTimeFormat.forPattern("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
DateTime datetime = formatter.parseDateTime(time); // time = string time data
LocalDate localDate = new LocalDateTime(datetime).toLocalDate();
LocalDate localDateNow = new LocalDateTime().toLocalDate();
int diff = Days.daysBetween(localDate, localDateNow).getDays();
Then just use the diff to evaluate which day.
This can now be easily achieved using the method in Android DateUtils as below.
DateUtils.getRelativeTimeSpanString(feedItem.timeStamp)
You can get outputs like,
1 hours ago, Yesterday, 2 days ago
Imagine you have data like this:
{"data":[{"id":"79tiyfjgdfg","sales_name":"Sengkuni","sales_branch":"Kingdom of Hastina","message":"Leader of Kurowo sent you a gold","timestamp":"2020-03-23 10:16:01"}]}
and call function like this:
holder.timestamp.setText(Utils.beautifyDate(context, notifications.get(position).getTimestamp(), "EEEE, MMMM d, YYYY"));
for another format you can check from https://developer.android.com/reference/java/text/SimpleDateFormat
Use this function below
public static String beautifyDate(Context context, String timestamp, String formatDate) {
String beautifyFormat;
SimpleDateFormat fmt = new SimpleDateFormat("yyyy-M-dd H:mm:ss", Locale.getDefault());
Date date = new Date();
try {
date = fmt.parse(timestamp);
} catch (Exception e) {
e.printStackTrace();
}
String mYear = new SimpleDateFormat("yyyy", Locale.getDefault()).format(date);
String mMonth = new SimpleDateFormat("M", Locale.getDefault()).format(date);
String mDay = new SimpleDateFormat("dd", Locale.getDefault()).format(date);
String mHour = new SimpleDateFormat("H", Locale.getDefault()).format(date);
String mMinutes = new SimpleDateFormat("mm", Locale.getDefault()).format(date);
Calendar now = Calendar.getInstance();
int years = now.get(Calendar.YEAR);
int months = now.get(Calendar.MONTH) + 1;
int days = now.get(Calendar.DATE);
int hours = now.get(Calendar.HOUR_OF_DAY);
int minutes = now.get(Calendar.MINUTE);
if (mYear.equals(String.valueOf(years)) && mMonth.equals(String.valueOf(months)) && mDay.equals(String.valueOf(days))) {
Log.i("timestamp", "on same day");
hours -= Integer.parseInt(mHour);
beautifyFormat = hours > 1 ? hours + " " + context.getString(R.string.hours_ago): hours + " " + context.getString(R.string.hour_ago);
if (hours == 0) {
minutes -= Integer.parseInt(mMinutes);
beautifyFormat = minutes > 1 ? minutes + " " + context.getString(R.string.minutes_ago): context.getString(R.string.moments_ago);
}
} else {
days = now.get(Calendar.DATE) - Integer.parseInt(mDay);
if (days == 1) {
beautifyFormat = context.getString(R.string.tomorrow);
} else {
beautifyFormat = new SimpleDateFormat(formatDate, Locale.getDefault()).format(date);
}
}
return beautifyFormat;
}
simply adjust the time you want to be able to see results like today or tomorrow and others
Hope it is useful.
This should give the output you want.
Use SimpleDateFormat to parse your date and then use
date.getTime()
in the following;
long now = System.currentTimeMillis();
DateUtils.getRelativeTimeSpanString(your time in long, now, DateUtils.MINUTE_IN_MILLIS);
for Android you can use the most simple way with Joda-Time-Android library:
Date yourTime = new Date();
DateTime dateTime = new DateTime(yourTime); //or simple DateTime.now()
final String result = DateUtils.getRelativeTimeSpanString(getContext(), dateTime);