写一个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; } }
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
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;
}
}