剩余时间与乔达时间
问题描述:
我想学习剩余时间到18.30在我的Android应用程序。但是,在乔达时间码中有年,月和日。我想用没有日期的剩余时间使用乔达时间。剩余时间与乔达时间
我的代码不会刷新剩余时间,重新开始时间在日期和时间过去时为负值。所以,我想删除日期值。
DateTime startDate = DateTime.now(); // now() : since Joda Time 2.0
DateTime endDate = new DateTime(2014, 10, 30, 18, 30);
Period period = new Period(startDate, endDate, PeriodType.dayTime());
PeriodFormatter formatter = new PeriodFormatterBuilder()
.appendDays().appendSuffix(" day ", " days ")
.appendHours().appendSuffix(" hour ", " hours ")
.appendMinutes().appendSuffix(" minute ", " minutes ")
.appendSeconds().appendSuffix(" second ", " seconds ")
.toFormatter();
tw.setText(formatter.print(period));
答
解释您的文字是不是很理解的,因为一些英语的错误。所以这可能是没有答案的主要原因。但是你的图很清楚,并且已经显示你的努力(所以我想知道为什么直到现在还没有答案)。事实上,您正在搜索剩余时间,直到下午18:30的当地时间。
这里,乔达的解决方案:
DateTime startDate = DateTime.now();
DateTime endDate = startDate.withTime(18, 30, 0, 0); // can move back in time!
if (startDate.toLocalTime().isAfter(new LocalTime(18, 30))) {
endDate = endDate.plusDays(1); // compensation for having moved back
}
Period period = new Period(startDate, endDate, PeriodType.dayTime());
PeriodFormatter formatter = new PeriodFormatterBuilder()
.appendDays().appendSuffix(" day ", " days ")
.appendHours().appendSuffix(" hour ", " hours ")
.appendMinutes().appendSuffix(" minute ", " minutes ")
.appendSeconds().appendSuffix(" second ", " seconds ")
.toFormatter();
System.out.println(formatter.print(period));
// output at now about 18:28 => 1 minute 48 seconds
// output at now about 18:31 => 23 hours 59 minutes 1 second
正如你所看到的解决方案是有点尴尬。这种编码一直是开发我自己的图书馆Time4J的原因之一。它提供了以下解决方案较短,没有任何如果条件:
PlainTimestamp start = SystemClock.inLocalView().now();
PlainTimestamp end =
start.with(PlainTime.COMPONENT.setToNext(PlainTime.of(18, 30)));
Duration<ClockUnit> duration = Duration.inClockUnits().between(start, end);
String p = PrettyTime.of(Locale.UK).print(duration, TextWidth.WIDE, true, 3);
System.out.println(p); // 23 hours, 40 minutes and 1 second
我个人认为乔达时间已经过时,因为大多数人,而不是想使用内置的Java-8的新图书馆居住在包java.time(但是不能进行持续时间格式化)。 Joda-Time也没有真正的未来发展(如果我们从几年开始诚实的话)。 Joda-Time的项目负责人似乎希望根据他的博客文章在他的新的Threeten-Extra-library中进一步发展。