您的位置:

java时间相减,java时间相加减

本文目录一览:

java 两个时间相减

先转成Date 再获取毫秒数来减

java.text.SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

java.util.Calendar c = Calendar.getInstance();

String date1 = "2014-10-10 00:00:00";

String date2 = "2015-10-10 00:00:00";

c.setTime(sdf.parse(date1));

int y1 = c.get(Calendar.YEAR);

int m1 = c.get(Calendar.MONTH);

c.setTime(sdf.parse(date2));

int y2 = c.get(Calendar.YEAR);

int m2 = c.get(Calendar.MONTH);

int y = Math.abs(y2 - y1);

int m = y * 12 + Math.abs(m1-m2);

long d1 = sdf.parse(date1).getTime();

long d2 = sdf.parse(date2).getTime();

int d = (int) (Math.abs(d2-d1) / (1000 * 60 * 60 * 24));

System.out.println("相差年: " + y);

System.out.println("相差月: " + m);

System.out.println("相差天: " + d);

JAVA时间相减

用下面这种方法吧:

Calendar date = Calendar.getInstance();//today

//int k = 5;//add 5 days

int k = -5;//sub 5 days

date.add( Calendar.DATE, k );

String format = "yyyy-MM-dd HH:mm:ss";

SimpleDateFormat simpleDate = new SimpleDateFormat( format );

String newDate = simpleDate.format( date.getTime() );

System.out.println("newDate is "+newDate);

java实现两个时间相减得到年月

大概判断了一下,比较粗糙,仅供参考。

private static void function17() throws Exception {

        Scanner sc=new Scanner(System.in);

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

        System.out.println("请输入起始时间");

        Date date1=sdf.parse(sc.next());

        System.out.println("请输入截止时间");

        Date date2=sdf.parse(sc.next());

        long time1=date1.getTime();

        long time2=date2.getTime();

        long time=(time2-time1)/1000;

        long year=time/(24*3600*365);

        long month=time%(24*3600*365)/(24*3600*30);

        if(year0){

            System.out.println(year+"年"+month+"月");

        }else{

            System.out.println(month+"月");

        }

    }

java 中 日期如何相加减???

这个东西很简单。

现在是2004-03-26 13:31:40

过去是:2004-01-02 11:30:24

要获得两个日期差,差的形式为:XX天XX小时XX分XX秒

方法一:

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

try

{

Date d1 = df.parse("2004-03-26 13:31:40");

Date d2 = df.parse("2004-01-02 11:30:24");

long diff = d1.getTime() - d2.getTime();

long days = diff / (1000 * 60 * 60 * 24);

}

catch (Exception e)

{

}

方法二:

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

java.util.Date now = df.parse("2004-03-26 13:31:40");

java.util.Date date=df.parse("2004-01-02 11:30:24");

long l=now.getTime()-date.getTime();

long day=l/(24*60*60*1000);

long hour=(l/(60*60*1000)-day*24);

long min=((l/(60*1000))-day*24*60-hour*60);

long s=(l/1000-day*24*60*60-hour*60*60-min*60);

System.out.println(""+day+"天"+hour+"小时"+min+"分"+s+"秒");

方法三:

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

java.util.Date begin=dfs.parse("2004-01-02 11:30:24");

java.util.Date end = dfs.parse("2004-03-26 13:31:40");

long between=(end.getTime()-begin.getTime())/1000;//除以1000是为了转换成秒

long day1=between/(24*3600);

long hour1=between%(24*3600)/3600;

long minute1=between%3600/60;

long second1=between%60/60;

System.out.println(""+day1+"天"+hour1+"小时"+minute1+"分"+second1+"秒");