问一个日期方面的问题?

Sylix 2003-09-12 04:06:00
怎么得到一个Date,为当前时间+2小时?
...全文
43 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
netcobol 2003-09-12
  • 打赏
  • 举报
回复
在《java2核心技术》书中提供Date,可以实现追加功能
netcobol 2003-09-12
  • 打赏
  • 举报
回复
在java2核心技术中有sun公司提供的Date 并且提供追加功能
zez 2003-09-12
  • 打赏
  • 举报
回复
Date date = new Date(new Date().getTime()+1000*3600*2);

------------------------------------------------------
我们还年轻牛奶会有的奶牛也会有的
可天天在 csdn 混这些会有吗 ??
ChDw 2003-09-12
  • 打赏
  • 举报
回复
System.currentTimeMillis()
ChDw 2003-09-12
  • 打赏
  • 举报
回复
Date nextTowHour d = new Date(System.currentMillseconds() + 1000 * 3600 * 2);
Sylix 2003-09-12
  • 打赏
  • 举报
回复
to dmhorse(dmhorse),你这个怎么调用?
行么?
junyi2003 2003-09-12
  • 打赏
  • 举报
回复
我记得Date的hour取值范围(0-11)
你set差1个小时,再get就又差了1个小时。

而且Date(String s) 、parse(String s) 这2函数为Deprecated.最好不要在程序中使用。
dmhorse 2003-09-12
  • 打赏
  • 举报
回复
/**
* Get the seconds by the specified HH:mm:ss
*
* @param time the specified format time
*
* @return the total seconds.
*/
public static int getSecondByTime(String time)
{
StringTokenizer tok = new StringTokenizer(time, ":");

String HH = "";
if(tok.hasMoreTokens())
{
HH = tok.nextToken();
}

String mm = "";
if(tok.hasMoreTokens())
{
mm = tok.nextToken();
}

String ss = "";
if(tok.hasMoreTokens())
{
ss = tok.nextToken();
}

int iHH = 0;
try{iHH = Integer.parseInt(HH);}
catch(Exception e){}

int imm = 0;
try
{
imm = Integer.parseInt(mm);
}catch(Exception e){}

int iss = 0;
try
{
iss = Integer.parseInt(ss);
}catch(Exception e){}

int seconds = iHH * 3600 + imm * 60 + iss;
return seconds;
}
dmhorse 2003-09-12
  • 打赏
  • 举报
回复
/**
* Get the interval date by specified start date and time
* @param d the start date
* @param time the interval time
* @param isAfter count the after date,otherwise cout before date
* @return
*/
public static Date getIntervalTime(Date d,String time,boolean isAfter)
{
try
{
Calendar c = java.util.Calendar.getInstance();

SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
Date dCount = sdf.parse(time);
c.setTime(d);
if(isAfter)
c.add(Calendar.SECOND,getSecondByTime(time));
else
c.add(Calendar.SECOND,0-getSecondByTime(time));
return c.getTime();
//System.out.println(dCount.getHours());
//System.out.println(dCount.getSeconds());
//System.out.println(dCount.getMinutes());


}catch(java.text.ParseException e)
{
throw new MFUtilException("Translate date time faield:" + time);
}
}

62,612

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧