Tod*_*ies 34 string android date
我正在Android应用中创建一个功能,以获取任意日期(过去,现在或将来)并找到相对于现在的差异.
我now和due变量都是longs,这是我的代码:
long now = System.currentTimeMillis();
long due = now + 864000;
Log.d("Time in 1 day", DateUtils.getRelativeTimeSpanString(due,now, DateUtils.DAY_IN_MILLIS));
Run Code Online (Sandbox Code Playgroud)
我所要的输出是这样的yesterday,today,in 4 days或19/12/2012.但是,当前的输出返回in 0 days...
我不希望时间出现在这些日期字符串上.
我做错了什么,是在Android上格式化日期的最佳方法?
Nik*_*ski 28
我想到的是改变:
DateUtils.getRelativeTimeSpanString(due, now, 0L, DateUtils.FORMAT_ABBREV_ALL);
由于文档说它返回相对于现在的时间.
如果失败则使用一些出色的库:
最后我实现了你想要的......
将其解压缩到任何文件夹,并将joda-time-2.2.jar放入androidProject/libs文件夹.
主要活动
import org.joda.time.DateTime;
import org.joda.time.Days;
import org.joda.time.Months;
import org.joda.time.MutableDateTime;
import org.joda.time.Weeks;
import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
public class MainActivity extends Activity
{
private int day ;
private int month ;
private int year ;
private int hour ;
private int minute ;
private long selectedTimeInMillis;
private long currentTimeInMillis;
private String strDay ="";
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
year = 2013;
month = 8;
day = 10;
hour = 15;
minute = 28;
DateTime selectedTime = new DateTime(year,month,day,hour,minute);
selectedTimeInMillis = selectedTime.getMillis();
MutableDateTime epoch = new MutableDateTime();
epoch.setDate(selectedTimeInMillis); //Set to Epoch time
DateTime now = new DateTime();
currentTimeInMillis = now.getMillis();
int days = Days.daysBetween(epoch, now).getDays();
int weeks = Weeks.weeksBetween(epoch, now).getWeeks();
int months = Months.monthsBetween(epoch, now).getMonths();
Log.v("days since epoch: ",""+days);
Log.v("weeks since epoch: ",""+weeks);
Log.v("months since epoch: ",""+months);
if(selectedTimeInMillis < currentTimeInMillis) //Past
{
long yesterdayTimeInMillis = currentTimeInMillis - 86400000;
DateTime today = new DateTime(currentTimeInMillis);
int year = today.getDayOfYear();
int intToday = today.getDayOfMonth();
DateTime yesterday = new DateTime(yesterdayTimeInMillis);
int intYesterday = yesterday.getDayOfMonth();
DateTime selectedDay = new DateTime(selectedTimeInMillis);
int intselectedDay = selectedDay.getDayOfMonth();
int intselectedYear = selectedDay.getDayOfYear();
if(intToday == intselectedDay & year == intselectedYear)
{
strDay = "today";
}
else if(intYesterday == intselectedDay)
{
strDay = "yesterday";
}
else
{
strDay = "before "+ days +" days from today";
}
}
else if(selectedTimeInMillis > currentTimeInMillis) //Future
{
long tomorrowTimeInMillis = currentTimeInMillis + 86400000;
DateTime tomorrow = new DateTime(tomorrowTimeInMillis);
int intTomorrow = tomorrow.getDayOfMonth();
DateTime today = new DateTime(selectedTimeInMillis);
int intToday = today.getDayOfMonth();
if(intToday == intTomorrow)
{
strDay = "tomorrow";
}
else
{
days = -days;
strDay = "after "+ days +" days from today";
}
}
Log.v("strDay: ",""+strDay);
}
}
Run Code Online (Sandbox Code Playgroud)
你只需要改变一天的值,你就会得到欲望的输出.目前我已将日期10作为输入,因此输出将是今天.
我设置日期/日= 10,月= 8,年= 2013,小时= 15,分钟= 28
对于过去的日期:
input day 9 output yesterday
input day 3 output before 7 days from today
input year 2012 and day 10 output before 365 days from today
Run Code Online (Sandbox Code Playgroud)
对于未来日期:
input day 11 output tomorrow
input day 27 output after 17 days from today
input day 23 and year 2016 output after 1109 days from today
Run Code Online (Sandbox Code Playgroud)
小智 -2
因此,您的计算基于毫秒单位,然后使用 SimpleDateFormat 格式化结果。
为此,您可以轻松使用 SimpleDateFormat 格式化程序,如下所示:
Date date = new Date(milliseconds);
SimpleDateFormat formatter = new SimpleDateFormat("EEEE dd MMMM yyyy");
String strDate = formatter.format(date);
Run Code Online (Sandbox Code Playgroud)
因此,您的计算应该基于毫秒单位,然后使用 SimpleDateFormat 格式化结果。
该模式(“EEEE dd MMMM yyyy”)允许您获取日期格式,例如 Monday, 04 February 2013。
您可以根据需要更改模式:“EEEE dd/MM/yy”,...
| 归档时间: |
|
| 查看次数: |
23198 次 |
| 最近记录: |