当前位置: 技术问答>java相关
如何将一个时间减去一段时间后再得到一个新的时间。
来源: 互联网 发布时间:2015-09-17
本文导语: 比如有一个date1,我使用一个个long型变量 L = date1.UTC(,,,,,); 然后 long M = L - 1000; 问题是如何将 M 再转化为一个date??? 有什么方法可以做到吗?不要告诉我是手算回去的. | public long daysBetw...
比如有一个date1,我使用一个个long型变量 L = date1.UTC(,,,,,);
然后 long M = L - 1000;
问题是如何将 M 再转化为一个date??? 有什么方法可以做到吗?不要告诉我是手算回去的.
然后 long M = L - 1000;
问题是如何将 M 再转化为一个date??? 有什么方法可以做到吗?不要告诉我是手算回去的.
|
public long daysBetween(String sourceDay1, String sourceDay2)
{
long days = -1;
if(sourceDay1 != null && sourceDay2 != null && (isStandardDate(sourceDay1) || isFormatDate(sourceDay1) || isStandardDate(sourceDay2) || isFormatDate(sourceDay2)))
{
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
try
{
if(isFormatDate(sourceDay1))
{
sourceDay1 = toStandardDate(sourceDay1);
}
if(isFormatDate(sourceDay2))
{
sourceDay2 = toStandardDate(sourceDay2);
}
Date day1 = sdf.parse(sourceDay1);
Date day2 = sdf.parse(sourceDay2);
days = (day1.getTime() - day2.getTime()) / (24 * 3600 * 1000);
days = Math.abs(days);
}
catch(ParseException e)
{
if(e != null)
{
e.printStackTrace();
System.out.println("日期转换出错:" + e.getMessage());
}
}
}
return days;
}
public String dateAdjust(String sourceString, int addDays, int format, boolean keepEightBit)
{
String returnString = sourceString;
if(isStandardDate(sourceString) || isFormatDate(sourceString))
{
if(isFormatDate(sourceString))
{
sourceString = toStandardDate(sourceString);
}
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
Date dt = sdf.parse(sourceString, new ParsePosition(0));
Calendar cal = Calendar.getInstance();
cal.setTime(dt);
cal.add(cal.DATE, addDays);
Date dt1 = cal.getTime();
StringBuffer strBuffer = new StringBuffer();
strBuffer.append("");
returnString = sdf.format(dt1, strBuffer, new FieldPosition(0)).toString();
returnString = toFormatDate(returnString, format, keepEightBit);
}
return returnString;
}
{
long days = -1;
if(sourceDay1 != null && sourceDay2 != null && (isStandardDate(sourceDay1) || isFormatDate(sourceDay1) || isStandardDate(sourceDay2) || isFormatDate(sourceDay2)))
{
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
try
{
if(isFormatDate(sourceDay1))
{
sourceDay1 = toStandardDate(sourceDay1);
}
if(isFormatDate(sourceDay2))
{
sourceDay2 = toStandardDate(sourceDay2);
}
Date day1 = sdf.parse(sourceDay1);
Date day2 = sdf.parse(sourceDay2);
days = (day1.getTime() - day2.getTime()) / (24 * 3600 * 1000);
days = Math.abs(days);
}
catch(ParseException e)
{
if(e != null)
{
e.printStackTrace();
System.out.println("日期转换出错:" + e.getMessage());
}
}
}
return days;
}
public String dateAdjust(String sourceString, int addDays, int format, boolean keepEightBit)
{
String returnString = sourceString;
if(isStandardDate(sourceString) || isFormatDate(sourceString))
{
if(isFormatDate(sourceString))
{
sourceString = toStandardDate(sourceString);
}
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
Date dt = sdf.parse(sourceString, new ParsePosition(0));
Calendar cal = Calendar.getInstance();
cal.setTime(dt);
cal.add(cal.DATE, addDays);
Date dt1 = cal.getTime();
StringBuffer strBuffer = new StringBuffer();
strBuffer.append("");
returnString = sdf.format(dt1, strBuffer, new FieldPosition(0)).toString();
returnString = toFormatDate(returnString, format, keepEightBit);
}
return returnString;
}
|
Date(long date)
Allocates a Date object and initializes it to represent the specified number of milliseconds since the standard base time known as "the epoch", namely January 1, 1970, 00:00:00 GMT.
----------用这个方法
Allocates a Date object and initializes it to represent the specified number of milliseconds since the standard base time known as "the epoch", namely January 1, 1970, 00:00:00 GMT.
----------用这个方法
|
不好意思,把上面的第二个方法改一改:
public String dateAdjust(String sourceString, int addDays, )
{
String returnString = sourceString;
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
Date dt = sdf.parse(sourceString, new ParsePosition(0));
Calendar cal = Calendar.getInstance();
cal.setTime(dt);
cal.add(cal.DATE, addDays);
Date dt1 = cal.getTime();
StringBuffer strBuffer = new StringBuffer();
strBuffer.append("");
returnString = sdf.format(dt1, strBuffer, new FieldPosition(0)).toString();
return returnString;
}
public String dateAdjust(String sourceString, int addDays, )
{
String returnString = sourceString;
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
Date dt = sdf.parse(sourceString, new ParsePosition(0));
Calendar cal = Calendar.getInstance();
cal.setTime(dt);
cal.add(cal.DATE, addDays);
Date dt1 = cal.getTime();
StringBuffer strBuffer = new StringBuffer();
strBuffer.append("");
returnString = sdf.format(dt1, strBuffer, new FieldPosition(0)).toString();
return returnString;
}
|
long lBeginTime = new java.util.Date().getTime() ;
long lEndTime = new java.util.Date().getTime() ;
int iDay = (int)((lBeginTime-lEndTime)/86400000);//新的天数
long lEndTime = new java.util.Date().getTime() ;
int iDay = (int)((lBeginTime-lEndTime)/86400000);//新的天数