08-07
28

java中处理时间相加问题

方法一:
package com.blur.bean.time;
public class sortTime {
    public static void main(String[] args) {
    String time1 = "12:45";
    String time2 = "0:1";
    String time3 = "";
    try {
        time3 = testAddTime(time1, time2);
    } catch (NumberFormatException ex) {
        System.out.println("time   format   Error!");
        return;
    }
    System.out.println(time3);
    }

    static String testAddTime(String time1, String time2)
        throws NumberFormatException {
    if (time1.indexOf(":") < 0 || time2.indexOf(":") < 0) {
        throw new NumberFormatException("Time   Format   Error!");
    }
    String[] time1s = time1.split(":");
    String[] time2s = time2.split(":");
    String result = "";
    try {
        result = Integer.toString(Integer.parseInt(time1s[0])
            + Integer.parseInt(time2s[0])
            + (Integer.parseInt(time1s[1]) + Integer
                .parseInt(time2s[1])) / 60);
        result += ":"
            + Integer.toString((Integer.parseInt(time1s[1]) + Integer
                .parseInt(time2s[1])) % 60);
    } catch (NumberFormatException exx) {
        throw new NumberFormatException("Time Format Error!");
    }
    return result;
    }

}



方法二:
Date nextTime=new Date(now.getTime()+(7*24*60*60));



方法三:
最好用java.util.Calendar。        

Calendar cl=Calendar.getInstance();

Long clTemp=cl.getTimeInMillis()+7*24*60*60*1000;

cl.setTimeInMillis(clTemp);

/*date即为现在时间加上七天后的时间*/
Date date=cl.getTime();


[本日志由 blurxx 于 2008-07-28 10:34 AM 编辑]
文章来自: 本站原创
引用通告: 查看所有引用 | 我要引用此文章
Tags: 时间 相减 相加
相关日志:
评论: 0 | 引用: 0 | 查看次数: -
发表评论
昵 称:
密 码: 游客发言不需要密码.
内 容:
验证码: 验证码
选 项:
虽然发表评论不用注册,但是为了保护您的发言权,建议您注册帐号.