java时间操作:获取当天剩余时间
Posted 胖子学习天地
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java时间操作:获取当天剩余时间相关的知识,希望对你有一定的参考价值。
mport org.apache.commons.lang.time.DateUtils; import org.joda.time.DateTime; import org.joda.time.Duration; import org.junit.Test; import java.time.LocalDateTime; import java.time.temporal.ChronoUnit; import java.util.Calendar; /** * Created by Administrator on 2016/12/12. */ public class TimeTest { @Test public void getTime() { //方法一 long milliSecondsLeftToday = 86400000 - DateUtils.getFragmentInMilliseconds(Calendar.getInstance(), Calendar.DATE); long secondsLeftToday = 86400 - DateUtils.getFragmentInSeconds(Calendar.getInstance(), Calendar.DATE); System.out.println("当天剩余毫秒1:" + milliSecondsLeftToday); System.out.println("当天剩余秒1:" + secondsLeftToday); //方法二 DateTime dateTime = new DateTime().millisOfDay().withMaximumValue(); long millSeconds2 = new Duration(new DateTime(), dateTime).getMillis(); long count = new Duration(new DateTime(), dateTime).getStandardSeconds(); System.out.println("当天剩余毫秒2:" + millSeconds2); System.out.println("当天剩余秒2:" + count); //方法三:LocalDateTime和ChronoUnit为1.8新增 LocalDateTime midnight = LocalDateTime.now().plusDays(1).withHour(0).withMinute(0).withSecond(0).withNano(0); long millSeconds = ChronoUnit.MILLIS.between(LocalDateTime.now(),midnight); long seconds = ChronoUnit.SECONDS.between(LocalDateTime.now(), midnight); System.out.println("当天剩余毫秒3:" + millSeconds); System.out.println("当天剩余秒3:" + seconds); } }
以上是关于java时间操作:获取当天剩余时间的主要内容,如果未能解决你的问题,请参考以下文章