Java Time Comparison tool class sharing

Source: Internet
Author: User
Tags diff

Development often need to compare time, write a simple tool class, share:


?

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465 666768697071727374757677787980818283848586878889909192 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;        }    }}


This article is from the "Follow the Java" blog, be sure to keep this source http://ijiuwen.blog.51cto.com/6165238/1621969

Java Time Comparison tool class sharing

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.