获取剩余的秒数

一、maven配置

<dependency>
      <groupId>joda-time</groupId>
      <artifactId>joda-time</artifactId>
      <version>2.9.9</version>
</dependency>

二、工具类

package com.hk;

import org.joda.time.DateTime;
import org.joda.time.Minutes;
import org.joda.time.Seconds;

/**
 * User: hk
 * Date: 2017/8/7 上午11:24
 * version: 1.0
 */
public final class DateKit {

    /**
     * 获取今天剩余的秒数
     *
     * @return 秒数
     */
    public static int oddSecondOfDay() {
        DateTime start = new DateTime();
        DateTime end = new DateTime().withHourOfDay(23).withMinuteOfHour(59).withSecondOfMinute(59);
        return Seconds.secondsBetween(start, end).getSeconds();
    }

    /**
     * 获取本周剩余的秒数
     *
     * @return 秒数
     */
    public static int oddSecondOfWeek() {
        DateTime start = new DateTime();
        DateTime end =
            new DateTime().dayOfWeek().withMaximumValue().withHourOfDay(23).withMinuteOfHour(59).withSecondOfMinute(59);
        return Seconds.secondsBetween(start, end).getSeconds();
    }

    /**
     * 获取本月剩余的秒数
     *
     * @return 秒数
     */
    public static int oddSecondOfMonth() {
        DateTime start = new DateTime();
        DateTime end =
            new DateTime().dayOfMonth().withMaximumValue().withHourOfDay(23).withMinuteOfHour(59).withSecondOfMinute(59);
        return Seconds.secondsBetween(start, end).getSeconds();
    }

    /**
     * 获取今年剩余的秒数
     *
     * @return 秒数
     */
    public static int oddSecondOfYear() {
        DateTime start = new DateTime();
        DateTime end =
            new DateTime().dayOfYear().withMaximumValue().withHourOfDay(23).withMinuteOfHour(59).withSecondOfMinute(59);
        return Seconds.secondsBetween(start, end).getSeconds();
    }



    public static void main(String[] args) {
        System.out.println(oddSecondOfDay());
        System.out.println(oddSecondOfWeek());
        System.out.println(oddSecondOfMonth());
        System.out.println(oddSecondOfYear());
    }
}

 

posted @ 2017-08-07 12:22  拓仲  阅读(1053)  评论(0编辑  收藏  举报