请教各位高手一个简单问题,急

wuyan55 2003-02-11 03:43:37
date型中小时是从0-23 现在我要返回一个28:34:45的date型数据,请问应该怎么做,请给个例子
...全文
77 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
hayai 2003-02-11
  • 打赏
  • 举报
回复
写一个Interval Class.Interval stands for a time interval between two Dates. we have one, like the following.
import java.util.*;

/**
* <p>Description: </p>
* Represents an interval between two time points on the time line.<br>
* One of the two major tasks fulfilled by this Class is to calculate the time
* escaped between two time points in milisecond, second, minute, and hour.
* The other calculation this Class does is to figure out the years, months,
* and days between two Dates according to java.util.Calendar.
* Even though the values are, but the intepretation is ambiguous.
* For instance, if we have an Inteval says there are 1 year, 2 months and
* 20 days diferrent, how many days is that year, and how many days are those
* two months excatly?
* The answers are unknown.
* <p>Copyright: Copyright (c) 2003</p>
* @version 2.0
*/

public class Interval
{
////////////
/* Fields */
////////////

/**
* Long representation of two time points in <b>ms</b>.<br>
*/
private long m_lngMilliSecond = 0;

// Must be positive
protected int m_intDayDiff, m_intMonthDiff, m_intYearDiff;

//////////////////
/* Constructors */
//////////////////

/**
* <b>Note:</b>
* If an Interval Object is constructed from this Constructor,
* getDayDiff(), getMonthDiff(), getYearDiff() will NOT be able to invoke.
*
* @throws IllegalArgumentException if intInterval is negative.
*/
public Interval(long intInterval)
{
if (intInterval < 0)
{
throw new IllegalArgumentException("Interval can NOT be negative.");
}
m_lngMilliSecond = intInterval;
m_intDayDiff = -1;
}

/**
* @throws IllegalArgumentException if the begin Date is NOT before the
* end Date.
*/
public Interval(Date begin, Date end)
{
m_lngMilliSecond = end.getTime() - begin.getTime();
if (m_lngMilliSecond < 0)
throw new IllegalArgumentException("Begin Date is NOT before the End Date.");

DateDealer dd = DateDealer.getInstance();

m_intDayDiff = dd.getDay(end) - dd.getDay(begin);
m_intMonthDiff = dd.getMonth(end) - dd.getMonth(begin);
m_intYearDiff = dd.getYear(end) - dd.getYear(begin);

if (m_intDayDiff < 0)
{
m_intDayDiff += dd.getDaysOfMonth(begin);
m_intMonthDiff--;
}

if (m_intMonthDiff < 0)
{
m_intMonthDiff += 12;
m_intYearDiff--;
}
}

/**
* @throws IllegalArgumentException if the begin Date is NOT before the
* end Date.
*/
public Interval(Calendar begin, Calendar end)
{
this(end.getTime(), begin.getTime());
}

//////////////////////////
//plus and minus methods//
//////////////////////////

/**
* Adds a certain time interval to this one.
* This method will effect the value of this Interval Object instance.<br>
* <b>Note:</b>
* If this method is invoked,
* getDayDiff(), getMonthDiff(), getYearDiff() will NOT be able to invoke.
*/
public Interval plus(Interval src)
{
m_lngMilliSecond += src.m_lngMilliSecond;
return this;
}

/**
* Substracts a certain time interval to this one.
* The method will effect the value of this Interval Object instance.<br>
* <b>Note:</b>
* If this method is invoked,
* getDayDiff(), getMonthDiff(), getYearDiff() will NOT be able to invoke.
*/
public Interval minus(Interval src)
{
m_lngMilliSecond -= src.m_lngMilliSecond;
return this;
}

/**
* This method will NOT effect the value of this Interval Object instance.
*/
public Date plus(Date src)
{
return new Date(src.getTime() + m_lngMilliSecond);
}

/**
* This method will NOT effect the value of this Interval Object instance.
*/
public Date minus(Date src)
{
return new Date(src.getTime() - m_lngMilliSecond);
}

///////////////
//Get Mothods//
///////////////

public long getMillis()
{
return m_lngMilliSecond;
}

public int getSecond()
{
return (int)(m_lngMilliSecond / 1000);
}

public int getMinute()
{
return (int)(m_lngMilliSecond / 1000 / 60);
}

public int getHour()
{
return (int)(m_lngMilliSecond / 1000 / 60 / 60);
}

/**
* <b>Note:</b>
* It makes more sense to use this method with getMonthDiff() and
* getYearDiff().
* @throws UnsupportedOperationException if the answer is not clear.
* Such situation will happen in two cases:<br>
* 1. The Interval Object instance is constructed from Interval(long).<br>
* 2. The plus(Interval) and/or minus(Interval) has been invoked.<br>
*/
public int getDayDiff()
{
if (m_intDayDiff < 0)
throw new UnsupportedOperationException("Unclear answer.");
return m_intDayDiff;
}

/**
* <b>Note:</b>
* It makes more sense to use this method with getYearDiff().
* @throws UnsupportedOperationException if the answer is not clear.
* Such situation will happen in two cases:<br>
* 1. The Interval Object instance is constructed from Interval(long).<br>
* 2. The plus(Interval) and/or minus(Interval) has been invoked.<br>
*/
public int getMonthDiff()
{
if (m_intDayDiff < 0)
throw new UnsupportedOperationException("Unclear answer.");
return m_intMonthDiff;
}

/**
* @throws UnsupportedOperationException if the answer is not clear.
* Such situation will happen in two cases:<br>
* 1. The Interval Object instance is constructed from Interval(long).<br>
* 2. The plus(Interval) and/or minus(Interval) has been invoked.<br>
*/
public int getYearDiff()
{
if (m_intDayDiff < 0)
throw new UnsupportedOperationException("Unclear answer.");
return m_intYearDiff;
}
}
wuyan55 2003-02-11
  • 打赏
  • 举报
回复
而且要求一定返回一个date型数据
wuyan55 2003-02-11
  • 打赏
  • 举报
回复
因为要算加班的问题,一个加班日是有28:23:34的
希偌 2003-02-11
  • 打赏
  • 举报
回复
不行
String s="2003-2-21 28:22:22";
java.text.SimpleDateFormat df=new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
try{java.util.Date d=df.parse(s);System.out.println(df.format(d));}catch(Exception e){System.out.println(e.toString());}
上面这段得到的结果为
2003-2-22 04:22:22
系统自动进位,日期自动加上1,而小时数减少24
iamqqmyheart 2003-02-11
  • 打赏
  • 举报
回复
非法的date


可用String代替


可我不明白你的目的.
hayai 2003-02-11
  • 打赏
  • 举报
回复
我今天28时34分45秒回答你。

62,614

社区成员

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

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