Java 将当前日期时间[时间戳]与从日历对话框中选择的日期时间进行比较,并以用户可理解的方式显示
当用户从日历对话框中选择日期时间时,我想显示一组用户可以理解的日期时间格式 例如:Java 将当前日期时间[时间戳]与从日历对话框中选择的日期时间进行比较,并以用户可理解的方式显示,java,android,date,calendar,Java,Android,Date,Calendar,当用户从日历对话框中选择日期时间时,我想显示一组用户可以理解的日期时间格式 例如: 我不选择日期,默认的当前时间戳是格式化和显示的。我希望文本视图显示“现在”,而不是显示“2014年5月9日07:40 AM”的日期时间 如果我从日历对话框中选择时间为“08:00 AM”(当前时间从wrt到07:00 AM),我应该显示为“在另一个1小时内” 任何超过一天的东西都会说“日期-时间”(现有格式) 我最近通过使用实现了相同的东西 通过使用它,你可以得到不同格式的时间差。示例代码 Calendar ca
我最近通过使用实现了相同的东西 通过使用它,你可以得到不同格式的时间差。示例代码
Calendar cal = Calendar.getInstance();
cal.clear();
cal.set(Calendar.YEAR, 2014);
cal.set(Calendar.MONTH, Calendar.MAY);
cal.set(Calendar.DATE, 09);
cal.set(Calendar.HOUR_OF_DAY, 9);
Date startDate = cal.getTime();
//Use JodaTime to calculate difference
Period period = getTimePassedSince(startDate);
//Extract values and display
long days= Math.abs(period.getDays()));
long hours= Math.abs(period.getHours()));
long minitues =Math.abs(period.getMinutes()));
long secnds =Math.abs(period.getSeconds()));
...
public static Period getTimePassedSince(Date initialTimestamp){
DateTime initDT = new DateTime(initialTimestamp.getTime());
DateTime now = new DateTime();
Period p = new Period(initDT, now, PeriodType.dayTime()).normalizedStandard( PeriodType.dayTime());
return p;
}
可能的副本。这个问题有一个链接到图书馆的答案。你们是故意在这里使用日历类的吗?如果没有,您也可以对该部分使用Joda Time:
DateTime DateTime=new DateTime(2014,DateTimeConstants.MAY,9,9,0,0,DateTimeZone.forID(“欧洲/巴黎”);
FYI,您可以使用内置的Joda时间功能,而不是将天数、小时等提取为整数来生成文本:。示例代码可以获取类似“15年8个月”的内容。…PeriodFormatter yearsAndMonths=new PeriodFormatterBuilder().printZeroAlways().appendYears().appendSuffix(“年”,“年”).appendSeparator(“and”).printZeroRarelyLast().appendMonths().appendSuffix(“月”、“月”).toFormatter();
您可以将java.util.Date直接传递给DateTime构造函数。无需.getTime()
。例如:DateTime initDT=new DateTime(initialTimestamp)
谢谢莉娜和巴兹尔。这对我帮助很大。我使用了莉娜共享的代码,它就像一个符咒一样有效:)