码迷,mamicode.com
首页 > 编程语言 > 详细

java时间比较工具类分享

时间:2015-03-19 06:39:58      阅读:197      评论:0      收藏:0      [点我收藏+]

标签:public   java   import   开发   

开发中经常需要比较时间,写了一个简易的工具类,分享一下:


?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
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时间比较工具类分享

标签:public   java   import   开发   

原文地址:http://ijiuwen.blog.51cto.com/6165238/1621969

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!