【发布时间】:2020-05-30 11:42:13
【问题描述】:
我想获得两个日期时间之间的百分比,以便我可以使用进度条。
我有以下代码,我在两个日期时间中传递并进行求和,但出现错误。
private void getpercentage(String dateTimeStart, String dateTimeExpiration) {
LocalDateTime start = LocalDateTime.parse(dateTimeStart.replace( " " , "T" ) );
LocalDateTime end = LocalDateTime.parse(dateTimeExpiration.replace( " " , "T" ) );
String start_date = start.toString().replace( "T", " " );
String end_date = end.toString().replace( "T", " " );
String p = Math.round( (end_date - start_date) * 100) + '%';
Log.d("type", "Date parsed : " + p);
}
【问题讨论】:
-
百分比?你能解释一下这两个日期时间的含义吗?
-
也许将您的
LocalDateTime对象转换为纪元然后使用它们进行计算会更简单。检查方法toEpochSecond(ZoneOffset zos)。顺便说一句,为了从中计算百分比,我猜你应该除以一些东西? -
将它们转换为整数(无论你喜欢什么方式)。
progress=(current-start)/(end-start)*100
标签: java android time elapsedtime localdatetime