java 计算当前时间离月底有多少天
答案:2 悬赏:60 手机版
解决时间 2021-02-04 13:55
- 提问者网友:沉默的哀伤
- 2021-02-04 02:25
比如今天10-28号,离月底10月31号,还有3天
最佳答案
- 五星知识达人网友:洒脱疯子
- 2021-02-04 03:57
import java.util.Calendar;
==========
Calendar c = Calendar.getInstance();
int d = c.getActualMaximum(Calendar.DAY_OF_MONTH);
int now = c.get(Calendar.DAY_OF_MONTH);
System.out.println(d - now);
==========
Calendar c = Calendar.getInstance();
int d = c.getActualMaximum(Calendar.DAY_OF_MONTH);
int now = c.get(Calendar.DAY_OF_MONTH);
System.out.println(d - now);
全部回答
- 1楼网友:duile
- 2021-02-04 04:48
现在是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+"秒"); ==================================================== java 比较时间大小 string s1="2008-01-25 09:12:09"; string s2="2008-01-29 09:12:11"; java.text.dateformat df=new java.text.simpledateformat("yyyy-mm-dd hh:mm:ss"); java.util.calendar c1=java.util.calendar.getinstance(); java.util.calendar c2=java.util.calendar.getinstance(); try { c1.settime(df.parse(s1)); c2.settime(df.parse(s2)); }catch(java.text.parseexception e){ system.err.println("格式不正确"); } int result=c1.compareto(c2); if(result==0) system.out.println("c1相等c2"); else if(result<0) system.out.println("c1小于c2"); else system.out.println("c1大于c2");
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯