您的位置:首页 > 其它

日期处理方法大全

2009-12-01 15:48 246 查看
package com.camelsys.platform.util;

import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.GregorianCalendar;

import org.ofbiz.core.util.Debug;

public class UtilDateTime {
/**
* This static method returns the java.sql.Timestamp. This will be
* used for getting current Timestamp from Server.
* @Method Name:       GetDateTimeStamp()
* @version            1.0
* @author 	          TCS
* @return             current time on server in Timestamp format
* @Version      Author	 		Date			Change Description
*/
public static Timestamp GetDateTimeStamp()
{

SimpleDateFormat lSimpleDateFormat = new SimpleDateFormat();

lSimpleDateFormat.applyPattern("yyyy-MM-dd HH:mm:ss");

return (java.sql.Timestamp.valueOf(lSimpleDateFormat.format(new java.util.Date())));

} //end GetDateTimeStamp

/**
* This static method returns formatted current system date as per the given pattern
* @Method Name:        GetFormattedSysDate(String asPattern)
* @version            1.0
* @author 	      TCS
* @param              pattern of date
* @return            Server date in String format
* @Version      Author	 		Date			Change Description
*/
public static String GetFormattedSysDate(String asPattern)
{
SimpleDateFormat lSimpleDateFormat = new SimpleDateFormat();

lSimpleDateFormat.applyPattern(asPattern);

return (lSimpleDateFormat.format(new java.util.Date()));

} //end GetFormattedSysDate

public static String GetFormattedSysTimestamp()
{
String asPattern = "yyyy-MM-dd HH:mm:ss";
SimpleDateFormat lSimpleDateFormat = new SimpleDateFormat();

lSimpleDateFormat.applyPattern(asPattern);

return (lSimpleDateFormat.format(new java.util.Date()));

}

/**
* This static method returns the java.sql.Date. This will be used for
*            	getting date from a string
* @Method Name:        GetDate(String asDate,asMask)
* @version            1.0
* @author 	          TCS
* @param              asDate Date from the screen
* @param              asMask Mask to be used
* @return             java.sql.Date
* @Version      Author	 		Date			Change Description
*/
public static java.sql.Date GetDate(String asDate, String asMask)
{
int liYearIndex;
int liYearPos = asMask.indexOf("yyyy", 0);
if (liYearPos == -1)
{
liYearPos = asMask.indexOf("yy", 0);
if (liYearPos == 0)
asMask = "yy" + asMask;
else if (liYearPos == 6)
asMask = asMask + "yy";
else
asMask = asMask.substring(0, 3) + "yyyy" + asMask.substring(5);
}
int liDayPos = asMask.indexOf("dd", 0);
int liMonthPos = asMask.indexOf("MM", 0);

int liMaxVal = MaxVal(liDayPos, liMonthPos, liYearPos);
int liMedVal = MedVal(liDayPos, liMonthPos, liYearPos);
String lsSep = asMask.substring(liMaxVal - 1, liMaxVal);

if (liYearPos == liMaxVal)
liYearIndex = 2;
else if (liYearPos == liMedVal)
liYearIndex = 1;
else
liYearIndex = 0;

if (asDate.length() < 10)
{
asDate = PadDateStr(asDate, lsSep, liYearIndex);
}
String lsDay = asDate.substring(liDayPos, liDayPos + 2);
String lsMonth = asDate.substring(liMonthPos, liMonthPos + 2);
String lsYear = asDate.substring(liYearPos, liYearPos + 4);

Debug.logInfo("DT=" + lsDay + "-" + lsMonth + "-" + lsYear);
return GetSqlDate(
Integer.parseInt(lsDay),
Integer.parseInt(lsMonth),
Integer.parseInt(lsYear));
} //end GetDate

/**
* This static method returns the java.sql.Date. This will be used
* for getting date from  Integer arguments
* @Method Name:        GetSqlDate(int pDay, int pMon, int pYear)
* @version            1.0
* @author 	          TCS
* @param              aiDay Day of date
* @param              aiMon Month of date
* @param              aiYear Year of date
* @exception          ParseException
* @return             java.sql.Date
* @Version      Author	 		Date			Change Description
*/
public static java.sql.Date GetSqlDate(int aiDay, int aiMon, int aiYear)
{
Calendar lvCal = Calendar.getInstance();
java.sql.Date lvSqlDate = null;
lvCal.set(aiYear, aiMon - 1, aiDay, 0, 0, 0);
lvSqlDate = new java.sql.Date((lvCal.getTime().getTime() / 1000) * 1000);
lvCal.clear();
return lvSqlDate;
} //end GetSqlDate

/**
* This static method returns the current java.sql.Date.
* @Method Name:        GetSqlDate()
* @version            1.0
* @author 	          TCS
* @exception          ParseException
* @return             java.sql.Date
* @Version      Author	 		Date			Change Description
*/
public static java.sql.Date GetSqlDate()
{
Calendar lCal = Calendar.getInstance();
int liMonth = lCal.get(Calendar.MONTH);
int liDay = lCal.get(Calendar.DAY_OF_MONTH);
int liYear = lCal.get(Calendar.YEAR);
java.sql.Date ldtSqlDate = null;
lCal.set(liYear, liMonth, liDay, 0, 0, 0);
ldtSqlDate = new java.sql.Date((lCal.getTime().getTime() / 1000) * 1000);
lCal.clear();
return ldtSqlDate;
} //end GetSqlDate

/**
* This static method returns the Date as String in the format requested
* @Method Name:        GetFormattedDate(String asPattern,java.sql.Date adDate)
* @version            1.0
* @author 	          TCS
* @param              asPattern pattern of date
* @param              adDate date
* @return             Date in String format
* @Version      Author	 		Date			Change Description
*/

public static String GetFormattedDate(java.sql.Date adDate, String asPattern)
{
String lstDate = "";
if (adDate != null)
{
SimpleDateFormat lSimpleDateFormat = new SimpleDateFormat();
lSimpleDateFormat.applyPattern(asPattern);
lstDate = lSimpleDateFormat.format(adDate);
} else
{
lstDate = "";
}
return lstDate;
}

public static String GetFormattedDate(java.util.Date aDate, String asPattern)
{
SimpleDateFormat lSimpleDateFormat = new SimpleDateFormat();
lSimpleDateFormat.applyPattern(asPattern);
return lSimpleDateFormat.format(aDate);

}

/**
* This static method returns the Date as String in the format   requested
* @Method Name:        GetFormattedDateForComp(String asCompId,String asLangCd, java.sql.Date adDate)
* @version            1.0
* @author             super
* @param              asCompId Company Id
* @param              asLangCd language Id
* @param              adDate date
* @exception          throws fcstException
* @return             Date in String format
* @Version      Author          Date            Change Description
*/

public static java.util.Date getFormattedDate(String aDate, String asPattern)
{
try
{
if ((aDate != null) && !aDate.equals(""))
{
SimpleDateFormat lFormat = new SimpleDateFormat(asPattern);
return lFormat.parse(aDate);
}
} catch (Exception e)
{
Debug.logError(e);
}
return null;
}

/**
* This static method returns the java.sql.Date after adding the
* specified number of days to the specified date. If date is null,
* number of dyas are added to sysdate.
* @Method Name:       GetNextDate(java.sql.Date dt, int NoOfDays)
* @version            1.0
* @author 	          TCS
* @param              adtDate date
* @param              aiNoOfDays Number of days
* @return             returns Date
* @Version      Author	 		Date			Change Description
*/

public static java.sql.Date GetNextDate(java.sql.Date adtDate, int aiNoOfDays)
{
java.sql.Date ldtNewDate = null;
int liMonth = 0;
int liDay = 0;
int liYear = 0;
GregorianCalendar lGregCalendar = (GregorianCalendar) GregorianCalendar.getInstance();
if (adtDate != null)
lGregCalendar.setTime(adtDate);

lGregCalendar.add(Calendar.DATE, aiNoOfDays);
liDay = lGregCalendar.get(Calendar.DAY_OF_MONTH);
liMonth = lGregCalendar.get(Calendar.MONTH) + 1;
liYear = lGregCalendar.get(Calendar.YEAR);

ldtNewDate = GetSqlDate(liDay, liMonth, liYear);

return ldtNewDate;
}

/**
* Pads the short form of the date on the screen.
* @Method Name:       PadDateStr
* @version            1.0
* @author 	          TCS
* @param              asDate date value on screen
* @param              asSep separator used in date mask
* @param              aiYearIndex position of year in date string
* @return             returns the padded date string
* @Version      Author	 		Date			Change Description
*/
private static String PadDateStr(String asDate, String asSep, int aiYearIndex)
{
String lsVal[] = new String[3];
int liPos = asDate.indexOf(asSep, 0);
lsVal[0] = asDate.substring(0, liPos);
lsVal[1] = asDate.substring(liPos + 1, asDate.indexOf(asSep, liPos + 1));
liPos = asDate.indexOf(asSep, liPos + 1);
lsVal[2] = asDate.substring(liPos + 1, asDate.length());

for (int i = 0; i < 3; i++)
{
if (i == aiYearIndex)
{

if (lsVal[i].length() == 2)
{
int liVal = (Integer.valueOf(lsVal[i])).intValue();
if (liVal > 69 && liVal < 100)
lsVal[i] = "19" + lsVal[i];
else
lsVal[i] = "20" + lsVal[i];
} else
{
lsVal[i] = lsVal[i];
}
} else
{
if (lsVal[i].length() == 1)
lsVal[i] = "0" + lsVal[i];
else
lsVal[i] = lsVal[i];
}
}
return (lsVal[0] + asSep + lsVal[1] + asSep + lsVal[2]);
}

/**
* This static method returns the number of days between two specified dates.
* If one of the dates in null, the difference is between system date and other date
* @Method Name:       DaysBetween(java.sql.Date dt1, java.sql.Date dt2)
* @version            1.0
* @author 	          TCS
* @param              adtDate1 Date 1
* @param              adtDate2 Date 2
* @return             returns number of days between Date 1 and Date 2
* @Version      Author	 		Date			Change Description
*/
public static int DaysBetween(java.sql.Date adtDate1, java.sql.Date adtDate2)
{
int liNumberOfDays = 0;
int liMonth1 = 0;
int liDay1 = 0;
int liYear1 = 0;
int liMonth2 = 0;
int liDay2 = 0;
int liYear2 = 0;

GregorianCalendar lGregCalendar1 = (GregorianCalendar) GregorianCalendar.getInstance();
if (adtDate1 != null)
lGregCalendar1.setTime(adtDate1);

GregorianCalendar lGregCalendar2 = (GregorianCalendar) GregorianCalendar.getInstance();
if (adtDate2 != null)
lGregCalendar2.setTime(adtDate2);

liDay1 = lGregCalendar1.get(Calendar.DAY_OF_MONTH);
liMonth1 = lGregCalendar1.get(Calendar.MONTH) + 1;
liYear1 = lGregCalendar1.get(Calendar.YEAR);

liDay2 = lGregCalendar2.get(Calendar.DAY_OF_MONTH);
liMonth2 = lGregCalendar2.get(Calendar.MONTH) + 1;
liYear2 = lGregCalendar2.get(Calendar.YEAR);

liNumberOfDays = ToJulian(liDay1, liMonth1, liYear1) - ToJulian(liDay2, liMonth2, liYear2);
liNumberOfDays = Math.abs(liNumberOfDays);

return liNumberOfDays;
}

//	the String data format must be yyyy-mm-dd hh:mm:ss.fffffffff
public static Timestamp convertStringToTimeStamp(String asDate, String asPattern)
{
java.sql.Timestamp lStamp = null;
if (asDate == null || asDate.length() == 0)
lStamp = new java.sql.Timestamp(System.currentTimeMillis());
else
{
if (asPattern == null || asPattern.length() == 0)
{
try
{
lStamp = java.sql.Timestamp.valueOf(asDate);
} catch (Exception e)
{
lStamp = new java.sql.Timestamp(System.currentTimeMillis());
}
} else
{
try
{
SimpleDateFormat lFormat = new SimpleDateFormat(asPattern);
lStamp = new java.sql.Timestamp(lFormat.parse(asDate).getTime());
} catch (Exception e)
{
lStamp = new java.sql.Timestamp(System.currentTimeMillis());
}
}
}
return lStamp;
} //end GetDateTimeStamp

public static String convertTimeStampToString(java.util.Date asDate, String asPattern)
{
if (asDate == null)
return "";
SimpleDateFormat lFormat = new SimpleDateFormat();
if ((asPattern != null) && (asPattern.length() > 0))
lFormat.applyPattern(asPattern);
return lFormat.format(asDate);
}

/**
* convert Timestamp to string "yyyy-mm-dd"
* @param ts
* @return
*/
static public String getDateString(java.sql.Timestamp ts)
{
java.util.Date dt = null;
if (null == ts)
return "";
java.util.Calendar calendar = Calendar.getInstance();
calendar.setTime(ts);
String strMonth = String.valueOf(calendar.get(Calendar.MONTH) + 1);
if (strMonth.length() == 1)
{
strMonth = "0" + strMonth;
}
String strDay = String.valueOf(calendar.get(Calendar.DATE));
if (strDay.length() == 1)
{
strDay = "0" + strDay;
}
return calendar.get(Calendar.YEAR) + "-" + strMonth + "-" + strDay;
}

//add by super end

/**
* @author akang 2005.1.17
* convert Timestamp to string "yyMMdd"
* @param ts
* @return
*/
static public String getDateStringEx(java.sql.Timestamp ts)
{
java.util.Date dt = null;
if (null == ts)
return "";
java.util.Calendar calendar = Calendar.getInstance();
calendar.setTime(ts);
String strYear = String.valueOf(calendar.get(Calendar.YEAR));
strYear = strYear.substring(2, 4);
String strMonth = String.valueOf(calendar.get(Calendar.MONTH) + 1);
if (strMonth.length() == 1)
{
strMonth = "0" + strMonth;
}
String strDay = String.valueOf(calendar.get(Calendar.DATE));
if (strDay.length() == 1)
{
strDay = "0" + strDay;
}
return strYear + strMonth + strDay;
}
/**
* convert Timestamp to string "yyyy-mm-dd hh:mm:ss.fffffffff"
* @param ts
* @return
*/
static public String getDateTimeString(java.sql.Timestamp ts)
{
if (null == ts)
return "";
java.util.Calendar calendar = Calendar.getInstance();
calendar.setTime(ts);
return calendar.get(Calendar.YEAR)
+ "-"
+ (calendar.get(Calendar.MONTH) + 1)
+ "-"
+ calendar.get(Calendar.DATE)
+ " "
+ calendar.get(Calendar.HOUR_OF_DAY)
+ ":"
+ calendar.get(Calendar.MINUTE)
+ ":"
+ calendar.get(Calendar.SECOND);
//return ts.toString();
}

public static String getNextDateString(String asBefore, int years, String asDateMask)
{
String lsNextDate = "";
java.sql.Date ldtDate = GetDate(asBefore, asDateMask);
Calendar lCalendar = Calendar.getInstance();
lCalendar.setTime(ldtDate);
//lCalendar.add();
lCalendar.add(Calendar.YEAR, years);
lsNextDate = GetFormattedDate(lCalendar.getTime(), asDateMask);
return lsNextDate;
}

public static boolean isAfter(java.sql.Date cdtBefore, java.sql.Date cdtEnd)
{
boolean lbCheck = true;
if (cdtBefore == null || cdtEnd == null)
return false;
Calendar lcalBefore = Calendar.getInstance();
lcalBefore.setTime(cdtBefore);
Calendar lcalAfter = Calendar.getInstance();
lcalAfter.setTime(cdtEnd);
if (lcalAfter.before(lcalBefore))
lbCheck = true;
else
lbCheck = false;
return lbCheck;
}

public static boolean isAfter(java.sql.Timestamp cdtBefore, java.sql.Timestamp cdtEnd)
{
boolean lbCheck = true;
if (cdtBefore == null || cdtEnd == null)
return false;
Calendar lcalBefore = Calendar.getInstance();
lcalBefore.setTime(cdtBefore);
Calendar lcalAfter = Calendar.getInstance();
lcalAfter.setTime(cdtEnd);
if (lcalAfter.before(lcalBefore))
lbCheck = true;
else
lbCheck = false;
return lbCheck;
}

public static int getYear(java.sql.Date dates){
return  dates.getYear();
}
public static int getMonth(java.sql.Date dates){
return  dates.getMonth();
}
public static int getDate(java.sql.Date dates){
return  dates.getDate();
}
public static int getHour(java.sql.Date dates){
return  dates.getHours();
}
public static int getMinute(java.sql.Date dates){
return  dates.getMinutes();
}
public static int getSecond(java.sql.Date dates){
return  dates.getSeconds();
}
public static int getYear(java.util.Date dates){
return  dates.getYear();
}
public static int getMonth(java.util.Date dates){
return  dates.getMonth();
}
public static int getDate(java.util.Date dates){
return  dates.getDate();
}
public static int getHour(java.util.Date dates){
return  dates.getHours();
}
public static int getMinute(java.util.Date dates){
return  dates.getMinutes();
}
public static int getSecond(java.util.Date dates){
return  dates.getSeconds();
}
public static int getDate(Timestamp dates){
return  dates.getDate();
}
private static int ToJulian(int aiDay, int aiMonth, int aiYear)
{
int liJulianYear = aiYear;
if (aiYear < 0)
liJulianYear++;
int liJulianMonth = aiMonth;
if (aiMonth > 2)
liJulianMonth++;
else
{
liJulianYear--;
liJulianMonth += 13;
}
int liJulian =
(int) (java.lang.Math.floor(365.25 * liJulianYear)
+ java.lang.Math.floor(30.6001 * liJulianMonth)
+ aiDay
+ 1720995.0);

int IGREG = 15 + 31 * (10 + 12 * 1582);
// Gregorian Calendar adopted Oct. 15, 1582

if (aiDay + 31 * (aiMonth + 12 * aiYear) >= IGREG)
// change over to Gregorian calendar
{
int ja = (int) (0.01 * liJulianYear);
liJulian += 2 - ja + (int) (0.25 * ja);
}
return liJulian;
}

/**
* Returns the maximum value out of three parameters.
* @Method Name:       MaxVal
* @version            1.0
* @author 	          TCS
* @param              aix value 1
* @param              aiy value 2
* @param              aiz value 3
* @return             returns the maximum value
* @Version      Author	 		Date			Change Description
*/
private static int MaxVal(int aix, int aiy, int aiz)
{
int liVal1;
int liMaxVal;
if (aix > aiy)
{
liVal1 = aix;
} else
{
liVal1 = aiy;
}

if (liVal1 > aiz)
{
liMaxVal = liVal1;
} else
{
liMaxVal = aiz;
}
return liMaxVal;
}

/**
* Returns the medium value out of three parameters.
* @Method Name:       MedVal
* @version            1.0
* @author 	          TCS
* @param              aix value 1
* @param              aiy value 2
* @param              aiz value 3
* @return             returns the medium value
* @Version      Author	 		Date			Change Description
*/
private static int MedVal(int aix, int aiy, int aiz)
{
int liMedVal = 0;
if (aix > aiy && aix > aiz)
{
if (aiy > aiz)
liMedVal = aiy;
else
liMedVal = aiz;
} else if (aix > aiy && aix < aiz)
{
liMedVal = aix;
} else if (aix < aiy && aix < aiz)
{
if (aiy > aiz)
liMedVal = aiz;
else
liMedVal = aiy;
} else if (aix < aiy && aix > aiz)
{
liMedVal = aix;
}
return liMedVal;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: