java 日期范围
答案:2 悬赏:60 手机版
解决时间 2021-02-20 11:48
- 提问者网友:心牵心
- 2021-02-20 01:45
也就是说要实现我给2个参数,比如20080531,20080930 ,然后java可以自动实现将其拆成20080531-20080630......之类的安每月为周期组成的时间段
最佳答案
- 五星知识达人网友:末日狂欢
- 2021-02-20 02:57
import java.util.Date;
public class TimeSpan
{
public final static TimeSpan ZERO = new TimeSpan(0);
private long _totalMilliSeconds = 0;
public TimeSpan(long totalMilliSeconds)
{
_totalMilliSeconds = totalMilliSeconds;
}
public TimeSpan(Date afterDate, Date beforeDate)
{
this(afterDate.getTime() - beforeDate.getTime());
}
public long getMilliSeconds()
{
return _totalMilliSeconds;
}
public long getSeconds()
{
return Math.round(_totalMilliSeconds/1000);
}
public long getMinutes()
{
return Math.round(_totalMilliSeconds/(1000*60));
}
public long getHours()
{
return Math.round(_totalMilliSeconds/(1000*60*60));
}
}
public class TimeSpan
{
public final static TimeSpan ZERO = new TimeSpan(0);
private long _totalMilliSeconds = 0;
public TimeSpan(long totalMilliSeconds)
{
_totalMilliSeconds = totalMilliSeconds;
}
public TimeSpan(Date afterDate, Date beforeDate)
{
this(afterDate.getTime() - beforeDate.getTime());
}
public long getMilliSeconds()
{
return _totalMilliSeconds;
}
public long getSeconds()
{
return Math.round(_totalMilliSeconds/1000);
}
public long getMinutes()
{
return Math.round(_totalMilliSeconds/(1000*60));
}
public long getHours()
{
return Math.round(_totalMilliSeconds/(1000*60*60));
}
}
全部回答
- 1楼网友:时间的尘埃
- 2021-02-20 03:18
setdate()函数写的有问题,这个函数最多只会等到当前日期,而且上下两个函数没有关系,string birthday = df.format(date);这句话只是定义了一个局部变量,类里边的示例变量没有赋值,而你在showmess()里又使用了(this.birthday),所以就会输出空。试着这样改:
public class maindate{
public static void main(string[] args){
testdate td=new testdate();
td.setdate("2008-08-08");
td.showmess();//可正常输出
td.setdate("2008 08 08");
td.showmess();//输出“出生日期未设置或为空”
}
}
class testdate{
private string birthday;
public void setdate(string strdate)
{
dateformat df = new simpledateformat("yyyy-mm-dd");
try{
date tmpdate = df.format(strdate);//如果出异常,则参数strdate格式不对,不执行下句
this.birthday=strdate;
}catch(parseexception pe){
//...
}
}
public void showmess()
{
//...
if(this.birthday!=null||!"".equals(this.birthday)){
system.out.println("出生日期:"+this.birthday);
}else{
system.out.println("出生日期未设置或为空");
}
}
}
以上程序请自行测试
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯