Android 从Joda时间输出中删除毫秒和秒
我试图输出某个日期(西班牙马德里8月15日)的剩余时间,通过我的代码,我可以得到一个文本视图来正确显示它。但是,它还显示我想删除的毫秒和秒,以及我想转换为天的“月” 你能帮我一下吗Android 从Joda时间输出中删除毫秒和秒,android,string,textview,jodatime,Android,String,Textview,Jodatime,我试图输出某个日期(西班牙马德里8月15日)的剩余时间,通过我的代码,我可以得到一个文本视图来正确显示它。但是,它还显示我想删除的毫秒和秒,以及我想转换为天的“月” 你能帮我一下吗 DateTimeZone timeZone = DateTimeZone.forID("Europe/Madrid"); DateTime target = new DateTime(2015, 8, 15, 0, 0, 0, timeZone); DateTime now = new Dat
DateTimeZone timeZone = DateTimeZone.forID("Europe/Madrid");
DateTime target = new DateTime(2015, 8, 15, 0, 0, 0, timeZone);
DateTime now = new DateTime(timeZone);
Period period = new Period(now, target);
PeriodFormatter formatter = PeriodFormat.getDefault();
String output = formatter.print(period);
TextView tv = (TextView) v.findViewById(R.id.tv);
tv.setText(output);
试试这个
Calendar c = Calendar.getInstance();
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm");
String formattedDate = df.format(c.getTime());
try {
df.setTimeZone(TimeZone.getDefault());
long timeStamp = df.parse(formattedDate).getTime();
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
纯Joda解决方案如下所示:
DateTimeZone timeZone = DateTimeZone.forID("Europe/Madrid");
DateTime target = new DateTime(2015, 8, 15, 0, 0, 0, timeZone);
DateTime now = new DateTime(timeZone);
Period period = new Period(now, target);
PeriodFormatter formatter = PeriodFormat.getDefault();
String output = formatter.print(period);
System.out.println(output); // 4 weeks, 2 days, 17 hours, 2 minutes, 10 seconds and 817 milliseconds
period = new Period(
now,
target,
PeriodType.dayTime().withSecondsRemoved().withMillisRemoved());
output = formatter.print(period);
System.out.println(output); // 30 days and 17 hours
决定性的变化是使用一个特殊的变量
只要您只需要完整的英语单词(使用PeriodFormat.getDefault()
),Joda Time就可以了(内置支持9种语言-其他lib具有更好的i18n功能)。否则,正如@VV的另一个错误答案所假装的那样,标准Java根本不提供持续时间处理和格式设置