fix [消息列表时间格式]

This commit is contained in:
zlzw 2024-03-25 13:51:10 +08:00
parent 253fe38067
commit e50b00152d
3 changed files with 193 additions and 175 deletions

View File

@ -1,8 +1,17 @@
package com.yunbao.common.utils;
import android.content.Context;
import android.provider.Settings;
import android.text.TextUtils;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import io.rong.common.RLog;
import io.rong.imkit.utils.language.LangUtils;
import io.rong.imkit.utils.language.RongConfigurationManager;
/**
* Created by cxf on 2018/7/19.
*/
@ -48,4 +57,183 @@ public class DateFormatUtil {
public static String getCurTimeString2() {
return sFormat3.format(new Date());
}
/**
* 以下都是融云聊天时间
*/
public static String getDateTimeString(long dateMillis, boolean showTime, Context context) {
if (dateMillis <= 0L) {
return "";
} else {
String formatDate = null;
Date date = new Date(dateMillis);
int type = judgeDate(date);
long time = java.lang.System.currentTimeMillis();
Calendar calendarCur = Calendar.getInstance();
Calendar calendardate = Calendar.getInstance();
calendardate.setTimeInMillis(dateMillis);
calendarCur.setTimeInMillis(time);
int month = calendardate.get(2);
int year = calendardate.get(1);
int weekInMonth = calendardate.get(4);
int monthCur = calendarCur.get(2);
int yearCur = calendarCur.get(1);
int weekInMonthCur = calendarCur.get(4);
switch (type) {
case 6:
formatDate = getTimeString(dateMillis, context);
break;
case 15:
String formatString = context.getResources().getString(io.rong.imkit.R.string.rc_date_yesterday);
if (showTime) {
formatDate = formatString + " " + getTimeString(dateMillis, context);
} else {
formatDate = formatString;
}
break;
case 2014:
if (year == yearCur) {
if (month == monthCur && weekInMonth == weekInMonthCur) {
formatDate = getWeekDay(context, calendardate.get(7));
} else {
formatDate = formatDate(date, "M/d");
}
} else {
formatDate = formatDate(date, "yyyy/M/d");
}
if (showTime) {
formatDate = formatDate + " " + getTimeString(dateMillis, context);
}
}
return formatDate;
}
}
public static int judgeDate(Date date) {
Calendar calendarToday = Calendar.getInstance();
calendarToday.set(11, 0);
calendarToday.set(12, 0);
calendarToday.set(13, 0);
calendarToday.set(14, 0);
Calendar calendarYesterday = Calendar.getInstance();
calendarYesterday.add(5, -1);
calendarYesterday.set(11, 0);
calendarYesterday.set(12, 0);
calendarYesterday.set(13, 0);
calendarYesterday.set(14, 0);
Calendar calendarTomorrow = Calendar.getInstance();
calendarTomorrow.add(5, 1);
calendarTomorrow.set(11, 0);
calendarTomorrow.set(12, 0);
calendarTomorrow.set(13, 0);
calendarTomorrow.set(14, 0);
Calendar calendarTarget = Calendar.getInstance();
calendarTarget.setTime(date);
if (calendarTarget.before(calendarYesterday)) {
return 2014;
} else if (calendarTarget.before(calendarToday)) {
return 15;
} else {
return calendarTarget.before(calendarTomorrow) ? 6 : 2014;
}
}
private static String getWeekDay(Context context, int dayInWeek) {
String weekDay = "";
switch (dayInWeek) {
case 1:
weekDay = context.getResources().getString(io.rong.imkit.R.string.rc_date_sunday);
break;
case 2:
weekDay = context.getResources().getString(io.rong.imkit.R.string.rc_date_monday);
break;
case 3:
weekDay = context.getResources().getString(io.rong.imkit.R.string.rc_date_tuesday);
break;
case 4:
weekDay = context.getResources().getString(io.rong.imkit.R.string.rc_date_wednesday);
break;
case 5:
weekDay = context.getResources().getString(io.rong.imkit.R.string.rc_date_thursday);
break;
case 6:
weekDay = context.getResources().getString(io.rong.imkit.R.string.rc_date_friday);
break;
case 7:
weekDay = context.getResources().getString(io.rong.imkit.R.string.rc_date_saturday);
}
return weekDay;
}
public static boolean isTime24Hour(Context context) {
String timeFormat = Settings.System.getString(context.getContentResolver(), "time_12_24");
return timeFormat != null && timeFormat.equals("24");
}
private static String getTimeString(long dateMillis, Context context) {
if (dateMillis <= 0L) {
return "";
} else {
Date date = new Date(dateMillis);
String formatTime;
if (isTime24Hour(context) || true) {
formatTime = formatDate(date, "HH:mm");
} else {
Calendar calendarTime = Calendar.getInstance();
calendarTime.setTimeInMillis(dateMillis);
int hour = calendarTime.get(10);
if (calendarTime.get(9) == 0) {
if (hour < 6) {
if (hour == 0) {
hour = 12;
}
formatTime = context.getResources().getString(io.rong.imkit.R.string.rc_date_morning);
} else {
formatTime = context.getResources().getString(io.rong.imkit.R.string.rc_date_am);
}
} else if (hour == 0) {
formatTime = context.getResources().getString(io.rong.imkit.R.string.rc_date_noon);
hour = 12;
} else if (hour <= 5) {
formatTime = context.getResources().getString(io.rong.imkit.R.string.rc_date_pm);
} else {
formatTime = context.getResources().getString(io.rong.imkit.R.string.rc_date_night);
}
int minuteInt = calendarTime.get(12);
String minuteStr = Integer.toString(minuteInt);
if (minuteInt < 10) {
minuteStr = "0" + minuteStr;
}
String timeStr = hour + ":" + minuteStr;
if (RongConfigurationManager.getInstance().getLanguageLocal(context) == LangUtils.RCLocale.LOCALE_CHINA) {
formatTime = formatTime + " " + timeStr;
} else {
formatTime = timeStr + " " + formatTime;
}
}
return formatTime;
}
}
public static String formatDate(Date date, String fromat) {
if (TextUtils.isEmpty(fromat)) {
return "";
} else {
try {
SimpleDateFormat sdf = new SimpleDateFormat(fromat);
return sdf.format(date);
} catch (IllegalArgumentException var3) {
RLog.e("RLong", "the given pattern is invalid.");
return "";
}
}
}
}

View File

@ -12,6 +12,7 @@ import android.widget.TextView;
import androidx.annotation.NonNull;
import com.yunbao.common.message.content.MessageChatTipsContent;
import com.yunbao.common.utils.DateFormatUtil;
import com.yunbao.live.R;
import com.yunbao.live.event.InputPanelViewHolderEvent;
@ -95,7 +96,7 @@ public class PDLiveMessageListAdapter extends MessageListAdapter {
TextView tv = holder.getView(R.id.rc_time);
long sentTime = mDataList.get(position).getSentTime();
boolean isSender = mDataList.get(position).getMessage().getMessageDirection().equals(Message.MessageDirection.SEND);
tv.setText(getDateTimeString(sentTime, true, holder.getContext()));
tv.setText(DateFormatUtil.getDateTimeString(sentTime, true, holder.getContext()));
if (holder.getView(R.id.left_liveStatus) != null) {
if (isLeftLive && !isSender) {
holder.getView(R.id.left_liveStatus).setVisibility(View.VISIBLE);
@ -113,178 +114,5 @@ public class PDLiveMessageListAdapter extends MessageListAdapter {
}
private static String getDateTimeString(long dateMillis, boolean showTime, Context context) {
if (dateMillis <= 0L) {
return "";
} else {
String formatDate = null;
Date date = new Date(dateMillis);
int type = judgeDate(date);
long time = java.lang.System.currentTimeMillis();
Calendar calendarCur = Calendar.getInstance();
Calendar calendardate = Calendar.getInstance();
calendardate.setTimeInMillis(dateMillis);
calendarCur.setTimeInMillis(time);
int month = calendardate.get(2);
int year = calendardate.get(1);
int weekInMonth = calendardate.get(4);
int monthCur = calendarCur.get(2);
int yearCur = calendarCur.get(1);
int weekInMonthCur = calendarCur.get(4);
switch (type) {
case 6:
formatDate = getTimeString(dateMillis, context);
break;
case 15:
String formatString = context.getResources().getString(string.rc_date_yesterday);
if (showTime) {
formatDate = formatString + " " + getTimeString(dateMillis, context);
} else {
formatDate = formatString;
}
break;
case 2014:
if (year == yearCur) {
if (month == monthCur && weekInMonth == weekInMonthCur) {
formatDate = getWeekDay(context, calendardate.get(7));
} else {
formatDate = formatDate(date, "M/d");
}
} else {
formatDate = formatDate(date, "yyyy/M/d");
}
if (showTime) {
formatDate = formatDate + " " + getTimeString(dateMillis, context);
}
}
return formatDate;
}
}
public static int judgeDate(Date date) {
Calendar calendarToday = Calendar.getInstance();
calendarToday.set(11, 0);
calendarToday.set(12, 0);
calendarToday.set(13, 0);
calendarToday.set(14, 0);
Calendar calendarYesterday = Calendar.getInstance();
calendarYesterday.add(5, -1);
calendarYesterday.set(11, 0);
calendarYesterday.set(12, 0);
calendarYesterday.set(13, 0);
calendarYesterday.set(14, 0);
Calendar calendarTomorrow = Calendar.getInstance();
calendarTomorrow.add(5, 1);
calendarTomorrow.set(11, 0);
calendarTomorrow.set(12, 0);
calendarTomorrow.set(13, 0);
calendarTomorrow.set(14, 0);
Calendar calendarTarget = Calendar.getInstance();
calendarTarget.setTime(date);
if (calendarTarget.before(calendarYesterday)) {
return 2014;
} else if (calendarTarget.before(calendarToday)) {
return 15;
} else {
return calendarTarget.before(calendarTomorrow) ? 6 : 2014;
}
}
private static String getWeekDay(Context context, int dayInWeek) {
String weekDay = "";
switch (dayInWeek) {
case 1:
weekDay = context.getResources().getString(string.rc_date_sunday);
break;
case 2:
weekDay = context.getResources().getString(string.rc_date_monday);
break;
case 3:
weekDay = context.getResources().getString(string.rc_date_tuesday);
break;
case 4:
weekDay = context.getResources().getString(string.rc_date_wednesday);
break;
case 5:
weekDay = context.getResources().getString(string.rc_date_thursday);
break;
case 6:
weekDay = context.getResources().getString(string.rc_date_friday);
break;
case 7:
weekDay = context.getResources().getString(string.rc_date_saturday);
}
return weekDay;
}
public static boolean isTime24Hour(Context context) {
String timeFormat = Settings.System.getString(context.getContentResolver(), "time_12_24");
return timeFormat != null && timeFormat.equals("24");
}
private static String getTimeString(long dateMillis, Context context) {
if (dateMillis <= 0L) {
return "";
} else {
Date date = new Date(dateMillis);
String formatTime;
if (isTime24Hour(context) || true) {
formatTime = formatDate(date, "HH:mm");
} else {
Calendar calendarTime = Calendar.getInstance();
calendarTime.setTimeInMillis(dateMillis);
int hour = calendarTime.get(10);
if (calendarTime.get(9) == 0) {
if (hour < 6) {
if (hour == 0) {
hour = 12;
}
formatTime = context.getResources().getString(string.rc_date_morning);
} else {
formatTime = context.getResources().getString(string.rc_date_am);
}
} else if (hour == 0) {
formatTime = context.getResources().getString(string.rc_date_noon);
hour = 12;
} else if (hour <= 5) {
formatTime = context.getResources().getString(string.rc_date_pm);
} else {
formatTime = context.getResources().getString(string.rc_date_night);
}
int minuteInt = calendarTime.get(12);
String minuteStr = Integer.toString(minuteInt);
if (minuteInt < 10) {
minuteStr = "0" + minuteStr;
}
String timeStr = hour + ":" + minuteStr;
if (RongConfigurationManager.getInstance().getLanguageLocal(context) == LangUtils.RCLocale.LOCALE_CHINA) {
formatTime = formatTime + " " + timeStr;
} else {
formatTime = timeStr + " " + formatTime;
}
}
return formatTime;
}
}
public static String formatDate(Date date, String fromat) {
if (TextUtils.isEmpty(fromat)) {
return "";
} else {
try {
SimpleDateFormat sdf = new SimpleDateFormat(fromat);
return sdf.format(date);
} catch (IllegalArgumentException var3) {
RLog.e("RLong", "the given pattern is invalid.");
return "";
}
}
}
}

View File

@ -13,6 +13,7 @@ import androidx.recyclerview.widget.RecyclerView;
import com.bumptech.glide.Glide;
import com.google.gson.Gson;
import com.yunbao.common.bean.IMLoginModel;
import com.yunbao.common.utils.DateFormatUtil;
import com.yunbao.common.utils.DpUtil;
import com.yunbao.common.utils.RouteUtil;
import com.yunbao.common.utils.ToastUtil;
@ -89,6 +90,7 @@ public class PDLiveCustomConversationProvider extends BaseConversationProvider {
//设置时间
String date = new SimpleDateFormat("HH:mm", Locale.getDefault()).format(new Date(uiConversation.mCore.getSentTime()));
date = DateFormatUtil.getDateTimeString(uiConversation.mCore.getSentTime(), true, holder.getContext());
holder.setText(R.id.rc_conversation_date, date);
holder.getView(R.id.rc_conversation_portrait).setOnClickListener(view -> {
RouteUtil.forwardUserHome(targetId);