gpt4 book ai didi

java - Android:显示 24 小时后收到消息后的天数

转载 作者:行者123 更新时间:2023-12-01 14:15:51 24 4
gpt4 key购买 nike

我在我的 Android 应用程序中实现了以下代码,该代码分割从 SQL 检索到的日期/时间值并显示自收到消息以来的分钟/小时数:

//Splits the date and time string retrieved from SQL into two separate strings, 
//and stores them into an array
String[] dt_array = m1.getTime().split(" ");
//String date = dt_array[0];
String time = dt_array[1];


//Splits the time into two strings, one for hours and one for minutes, and stores them
//into an array
String[] timeSplit = time.split(":");
String hourString = timeSplit[0];
String minuteString = timeSplit[1];

//converts the new separate time strings to integer variables
int msgHour = Integer.parseInt(hourString);
int msgMinute =Integer.parseInt(minuteString);

Calendar currentTime = Calendar.getInstance();

//retrieves current hour and minute from device and stores in separate ints
int currentHour = currentTime.get(Calendar.HOUR);
int currentMinute = currentTime.get(Calendar.MINUTE);

int hoursSince;
int minutesSince;
String timeDisplay = null;


if (currentHour != 1)
{
//tells app to display minutes if message was received under an hour ago
if(currentHour - msgHour < 1)
{

minutesSince = currentMinute - msgMinute;
//if only a minute has passed, text should read "1 minute ago", otherwise use plural "minutes"
if (minutesSince == 1)
timeDisplay = minutesSince + " minute ago";
else
timeDisplay = minutesSince + " minutes ago";
}
else if (currentHour - msgHour >= 1)
{
hoursSince = currentHour - msgHour;
timeDisplay = hoursSince + " hours ago";
}

}
else
{
//for messages that were received at a 12th hour, if the hour is currently 1, correctly displays 1 hour ago
if(msgHour == 12 && currentHour == 1)
{
hoursSince = 1;
timeDisplay = hoursSince + " hours ago";
}

}

我想要的是添加一个函数,如果小时数超过 24,则显示自收到以来的天数。我知道这应该很容易,但我不太确定如何在 Android 上进行操作。

最佳答案

您确定自发布消息以来的时间量的方法过于复杂,并且在分钟和小时翻转时容易出现问题。像这样的事情会简单得多:

SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy HH:mm"); //change format as needed
Date messageDate = formatter.parse(date);
long since = System.currentTimeMillis() - messageDate.getTime();
long seconds = since/1000;
long minutes = seconds/60;
long hours = minutes/60;
long days = hours/24;

从这里开始,构建您想要的消息对您来说应该是微不足道的。

关于java - Android:显示 24 小时后收到消息后的天数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18111945/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com