您的位置:

java计算时间,java计算时间间隔

本文目录一览:

java类里面怎么计算时间?

用Calendar类里面的add(int field, int amount) 方法进行时间的计算。示例代码如下:

import java.text.ParseException;

import java.text.SimpleDateFormat;

import java.util.Calendar;

public class TestCalendar {

public static void main(String[] args){

Calendar c = Calendar.getInstance();

SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

try {

c.setTime(format.parse("2011-08-05 14:49:45"));

System.out.println("原来的时间:"+format.format(c.getTime()));

c.add(Calendar.MINUTE,20);

System.out.println("计算后得到的时间:"+format.format(c.getTime()));

} catch (ParseException e) {

e.printStackTrace();

}

}

}

JAVA 时间计算

如果今天是2011年1月5日,先引入import java.util.Date;import java.text.SimpleDateFormat;

Date time=new Date();

Calendar cal=Calendar.getInstance();

cal.setTime(time);//日历为今天

long tm,tm1;

tm=cal.getTimeInMillis();//得到当前时间与1970年1月1日0点相距的毫秒数

tm1=tm-(24*60*60*1000);//得到昨天与1970年1月1日0点相距的毫秒数

Date time1=new Date(tm1);

SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");

String tm=sdf.format(time1);//tm就是昨天的日期的字符串表示

java整么计算时间

提供一个时间操作类给你,你可以调用里面的dateDiff方法,dateDiff(new Date(), getDateFromString("明天的字符串")表示的就是现在到明天0点之前的差距天数。

你要分钟数的话就自己改一下dateDiff,不要除以60和24就是分钟了

以下是工具类代码

import org.apache.log4j.Logger;

import java.text.ParseException;

import java.text.SimpleDateFormat;

import java.util.Calendar;

import java.util.Date;

public class AdDateUtil {

    private static Logger logger = Logger.getLogger(AdDateUtil.class);

    static public String getNowStr(String format) {

        SimpleDateFormat sdf = new SimpleDateFormat(format);

        String now = sdf.format(new Date());

        return now;

    }

    static public Date getFormatDate(String date, String format) {

        SimpleDateFormat sdf = new SimpleDateFormat(format);

        Date d = new Date();

        try {

            d = sdf.parse(date);

        } catch (ParseException e) {

            logger.error(e);

        }

        return d;

    }

    static public String getDateStr(Date date, String format) {

        SimpleDateFormat sdf = new SimpleDateFormat(format);

        String d = sdf.format(date);

        return d;

    }

    static public String getPadZeroString(String s, int size) {

        StringBuffer sb = new StringBuffer();

        for (int i = 0; i  (size - s.length()); i++) {

            sb.append("0");

        }

        sb.append(s);

        return sb.toString();

    }

    /**

    * 得到某月的天数

    *

    * @param year

    * @param month

    * @return

    */

    static public int getDayCountOfMonth(String year, String month) {

        Calendar cal = Calendar.getInstance();

        // 年

        cal.set(Calendar.YEAR, Integer.parseInt(year));

        // 月,因为Calendar里的月是从0开始,所以要-1

        cal.set(Calendar.MONTH, Integer.parseInt(month) - 1);

        return cal.getActualMaximum(Calendar.DAY_OF_MONTH);

    }

    static public String getYesterday(String format) {

        SimpleDateFormat df = new SimpleDateFormat(format);

        Calendar now = Calendar.getInstance();

        now.roll(Calendar.DAY_OF_YEAR, -1); //昨天

        return df.format(now.getTime());

    }

    /**

    * 获取和今天附近的某天

    * @param format

    * @param diff

    * @return

    */

    static public String getADay(String format, int diff) {

        SimpleDateFormat df = new SimpleDateFormat(format);

        Calendar now = Calendar.getInstance();

        int beforeM = now.get(Calendar.MONTH);

        now.roll(Calendar.DAY_OF_YEAR, diff); //

        int nowM = now.get(Calendar.MONTH);

        //必须进行日期处理,否则2009-01-04日前七天是2009-12-28

        if (nowM  beforeM) {

            now.roll(Calendar.YEAR, -1);

        }

        return df.format(now.getTime());

    }

    static public String getTomorrow(String format) {

        SimpleDateFormat df = new SimpleDateFormat(format);

        Calendar now = Calendar.getInstance();

        now.roll(Calendar.DAY_OF_YEAR, 1); //明天

        return df.format(now.getTime());

    }

    /**

    * 得到最近num天的全部日期

    * 说明:

    * 1.日期是从昨天开始算的.

    * 2.如果num=2 , 日期是2008-03-14 ,则返回的结果为 2008-03-12、2008-03-13

    * @param num

    * @return

    */

    public static String[] getDaysByNum(int num, String date) {

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

        String[] result = {  };

        Calendar cal = Calendar.getInstance();

        cal.setTime(getDateFromString(date, "yyyy-MM-dd"));

        //最近一周

        result = new String[num];

        for (int i = num; i  0; i--) {

            cal.add(Calendar.DAY_OF_YEAR, -1);

            result[i - 1] = sdf.format(new Date(cal.getTimeInMillis()));

        }

        return result;

    }

    public static Date getDateFromString(String dateStr, String format) {

        if ((dateStr == null) || (format == null)) {

            try {

                throw new Exception("数据类型异常" + dateStr + "|" + format);

            } catch (Exception e) {

                logger.error("数据类型异常:" + e);

            }

        }

        SimpleDateFormat df = new SimpleDateFormat(format);

        Date date;

        try {

            date = df.parse(dateStr);

            return date;

        } catch (Exception ex) {

            logger.error(ex);

            return new Date();

        }

    }

    static public int getNowYear() {

        Calendar cal = Calendar.getInstance();

        return cal.get(Calendar.YEAR);

    }

    static public int getNowMonth() {

        Calendar cal = Calendar.getInstance();

        return cal.get(Calendar.MONTH) + 1;

    }

    public static String[] getMonthRang(String year, String month) {

        String beginDate = year + "-" + month + "-01";

        String endDate = year + "-" + month + "-" +

            getDayCountOfMonth(year, month);

        return getDaysByRang(beginDate, endDate);

    }

    public static String[] getDaysByRang(String beginDate, String endDate) {

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

        //得到两个日期间相差多少天

        int num = dateDiff(beginDate, endDate);

        if (num  0) {

            //颠倒一下日期

            String tmp = beginDate;

            beginDate = endDate;

            endDate = tmp;

            num = 0 - num;

        }

        String[] result = {  };

        Calendar cal = Calendar.getInstance();

        try {

            cal.setTime(sdf.parse(beginDate));

        } catch (ParseException e) {

            e.printStackTrace();

        }

        num = num + 1; //把开始和结束日期都包含进去

        result = new String[num];

        for (int i = 0; i  num; i++) {

            if (i  0) {

                cal.add(Calendar.DAY_OF_YEAR, 1);

            }

            result[i] = sdf.format(new Date(cal.getTimeInMillis()));

        }

        return result;

    }

    public static int dateDiff(String beginDate, String endDate) {

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

        Date date = null;

        try {

            date = sdf.parse(endDate);

        } catch (ParseException e) {

            date = new Date();

            e.printStackTrace();

        }

        long end = date.getTime();

        try {

            date = sdf.parse(beginDate);

        } catch (ParseException e) {

            date = new Date();

            e.printStackTrace();

        }

        long begin = date.getTime();

        long day = (end - begin) / (1000 * 3600 * 24); //除1000是把毫秒变成秒

        return Integer.parseInt(Long.toString(day));

    }

    public static void main(String[] args) {

        System.out.println(AdDateUtil.getADay("yyyy-MM-dd", -7));

    }

}

java 时间计算

自己写的一个,使用了两种方法实现了你的需求,希望可以帮到你

import java.text.SimpleDateFormat;

import java.util.Calendar;

import java.util.Date;

public class TimeTest {

/**

* @param args

*/

public static void main(String[] args) {

TimeTest t = new TimeTest();

SimpleDateFormat formater = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");

String currentTime = formater.format(new Date());

System.out.println("当前日期:"+currentTime);

System.out.println("昨日日期:"+t.getYtime(currentTime));

System.out.println("上月日期:"+t.getLtime(currentTime));

}

// 昨天

public String getYtime(String today){

String ytime = "";

Date date = new Date(today);

date = new Date(date.getTime() - 1000 * 60 * 60 * 24);

ytime = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss").format(date);

return ytime;

}

// 上月

public String getLtime(String today){

String ltime = "";

Date date = new Date(today);

Calendar cal= Calendar.getInstance();

cal.setTime(date);

cal.add(Calendar.DATE, -1);

cal.add(Calendar.MONTH, -1);

date = cal.getTime();

ltime = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss").format(date);

return ltime;

}

}

计算Java程序执行时间的几种方法

最简单的就是直接获取开始时间、结束时间,计算时间差

public class Demo {

    public static void main(String[] args) {

        long startTime = System.currentTimeMillis();    //获取开始时间

        doSomething();    //测试的代码段

        long endTime = System.currentTimeMillis();    //获取结束时间

        System.out.println("程序运行时间:" + (endTime - startTime) + "ms");    //输出程序运行时间

    }

}

java计算时间

哎。发晚了,虽然不能被采纳了,不过还是贴出来吧,实现方式大同小异,希望对你有所帮助,

// 实例化日期格式

SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");

// 取得Date型日期

Date date = format.parse("1970-01-01 00:00:00");

// 实例化日历类

Calendar calendar = Calendar.getInstance();

// 设置日期为1970-01-01 00:00:00

calendar.setTime(date);

// 毫秒加算方法(1970-01-01 00:00:00的基础上加1336030629000毫秒)

calendar.setTimeInMillis(calendar.getTimeInMillis() + 1336030629000L);

// 利用实例化好的SimpleDateFormat将日期格式化成yyyy-MM-dd hh:mm:ss形式并输出。

System.out.println(format.format(calendar.getTime()));

结果和上面那位回答的是一样的。