java時間比較工具類分享

來源:互聯網
上載者:User

標籤:public   java   import   開發   

開發中經常需要比較時間,寫了一個簡易的工具類,分享一下:


?

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192 package com.ijiuyuan.common.utils; import java.text.ParseException;import java.text.SimpleDateFormat;import java.util.Calendar;import java.util.Date;  public class DiffTime {    public static void main(String[] args){                 SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");        Date startTime = null;        Date endTime = null;        try {            startTime = sdf.parse("2014-06-24 10:19:40");            endTime=sdf.parse("2014-06-16 10:19:40");        } catch (ParseException e) {            // TODO Auto-generated catch block            e.printStackTrace();        }                 long nd = 1000 * 24 * 60 * 60;// 一天的毫秒數        long md = 1000 * 1 * 1 * 60;// 分鐘的毫秒數        long sd = 1000 * 1 * 1 * 1;// 毫秒數        long diff =startTime.getTime()- new Date().getTime() ;                 long day = diff / nd;// 計算差多少天        long minute=diff/md;        long second=diff/sd;        System.out.println(second);        if(diff>=nd*7){            System.out.println("預備中:"+second);        }                 long endday = (endTime.getTime()- new Date().getTime()) / md;// 計算差多少天                          System.out.println(endday);                 if (day <= 7 && day >= 0) {                                      if(minute>=0)                System.out.println("即將開始");            else {                System.out.println("已結束");            }        } else if (day > 7) {            System.out.println("預備中");        } else {            System.out.println("已結束");        }     }    /**     * 時間比較     * <p>     * 如果第一個時間大於第二時間返回1<br/>     * 等於返回0<br/>     * 小於返回-1     * </p>     * @param firstTime     * @param secondTime     * @return     */    public static int compare(Date firstTime,Date secondTime){        long diff =firstTime.getTime()- secondTime.getTime() ;        if(diff>0){            return 1;        }else if(diff==0){            return 0;        }else{            return -1;        }    }    /**     * 是否值過大     * @param firstTime     * @return     */    public static boolean isTooLarge(Date firstTime){        Calendar calendar=Calendar.getInstance();        calendar.setTime(new Date());        calendar.add(Calendar.YEAR, 1000);        Date bigDate=calendar.getTime();        if(compare(firstTime, bigDate)>0){            return true;        }else{            return false;        }    }}


本文出自 “關注java” 部落格,請務必保留此出處http://ijiuwen.blog.51cto.com/6165238/1621969

java時間比較工具類分享

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.