[Java] 时间字符串比较的几种方法

最近一直写程序分析软件Trace, 所以Trace中的时间戳要处理一下...
经常遇到的一个问题是比较给定格式的时间字符串的先后顺序,由于字符串包含英文月份, e.g. Aug, Jan, May, etc.

所以需要用SimpleDateFormat格式化: 

new SimpleDateFormat("MMM dd HH:mm:ss yyyy", Locale.US);

其中要解析英文月份的话必须用java.util.Locale自带的属性Locale.US.

用了三种方法(3种comparer)比较时间前后:

public class TimeComparer {

    /**
     * 
     * @param time1 input timestamp No.1
     * @param time2 input timestamp No.2
     * @param timestampFormat the format object for time stamp
     * @return 
     *         if return value is 1 -> time 1 is newer
     *                   value is -1 -> time 2 is newer
     *                   value is 0 -> same timestamps
     */
    int timestampCompare(String time1, String time2, SimpleDateFormat timestampFormat) {
        int comparer = 3 ;
        int rev = 0;
        if(time1.equals(time2)) // same
            return rev;
        
        try {
            Date timestamp1 = timestampFormat.parse(time1);
            Date timestamp2 = timestampFormat.parse(time2);
            
            switch (comparer) {
                
                case 1: //Compare 1 - function getTime()
                    if(timestamp1.getTime() >  timestamp2.getTime()) // time1 has more seconds -> time1 is newer
                        return 1;
                    else return -1;
                    
                case 2: //Compare 2 - function before/after
                    if(timestamp1.after(timestamp2)) // time 1 is newer
                        return 1;
                    else return -1;
                    
                case 3: //Compare 3 - function calendar compareTo
                    Calendar cal1 = Calendar.getInstance();  
                    Calendar cal2 = Calendar.getInstance();  
                    cal1.setTime(timestamp1);  
                    cal2.setTime(timestamp2); 
                    if(cal1.compareTo(cal2) > 0) // cal1 is bigger -> cal1 is newer
                        return 1;
                    else return -1;
                    
                default: 
                    break;
            }

        } catch (ParseException e) {
            e.printStackTrace();
        }
        
        return rev;
    }
}


另外主函数的测试用例为:

public static void main(String[] args) {
        String timestamp1 = "Aug 30 13:12:27 2017";
        String timestamp2 = "Aug 28 21:30:05 2017";
        
        //MMM dd hh:mm:ss yyyy
        SimpleDateFormat timestampFormat = new SimpleDateFormat("MMM dd HH:mm:ss yyyy", Locale.US);
        int rev = new TimeComparer().timestampCompare(timestamp1, timestamp2, timestampFormat);
        if(rev > 0)
            System.out.println(timestamp1 + " is newer!");
        else if(rev < 0)
            System.out.println(timestamp2 + " is newer!");
        else 
            System.out.println("Same timestamps!");
    }


  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值