zoukankan      html  css  js  c++  java
  • 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);
    
        }
    }

  • 相关阅读:
    CF869E The Untended Antiquity 解题报告
    Walk 解题报告
    CF911F Tree Destruction 解题报告
    P4397 [JLOI2014]聪明的燕姿
    洛谷 P2329 [SCOI2005]栅栏 解题报告
    洛谷 P3747 [六省联考2017]相逢是问候 解题报告
    set-erase
    set-empty
    set-empty
    set-end
  • 原文地址:https://www.cnblogs.com/30go/p/9166483.html
Copyright © 2011-2022 走看看