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); }
}
05-08 08:38