public static boolean validDateRange(long fromdate, long todate) {
boolean valid = false;
if (todate < valid =" false;" valid =" true;" style="font-weight: bold;">Retrieve Date as String
public static String getDateStringFromDate(java.util.Date date,
String format) {
SimpleDateFormat dateFormatter = new SimpleDateFormat(format);
return dateFormatter.format(date);
}
Retrieve String as Date
public static Date getDateFromDateString(String format, String dateString)
throws ParseException {
SimpleDateFormat dateFormatter = new SimpleDateFormat(format);
Date date = null;
if(dateString != null && !dateString.equalsIgnoreCase("")){
date = new Date(dateFormatter.parse(dateString).getTime());
}
return date;
}
/**
* @param date
* @return returns starting date of the week in which the specified date falls
*/
public static java.util.Date getWeekStartDate(java.util.Date date) {
return getWeekStartDate(date, 0);
}
/**
* @param date
* @param incrWeek
* @return returns starting date of the week. The week is calculated as the week in which the specified date falls + incrWeek.
*/
public static java.util.Date getWeekStartDate(java.util.Date date, int incrWeek) {
if (date == null) {
return null;
}
Calendar cal = Calendar.getInstance();
cal.setTime(date);
if(cal.get(Calendar.MONTH) == 0)
{
if(cal.get(Calendar.WEEK_OF_YEAR)==1)
{
if (cal.get(Calendar.DATE) < 7) { cal.roll(Calendar.YEAR,-1); } } } cal.roll(Calendar.WEEK_OF_YEAR, incrWeek); cal.set(Calendar.DATE, (cal.get(Calendar.DATE) - (cal.get(Calendar.DAY_OF_WEEK) - cal.getFirstDayOfWeek()))); return cal.getTime(); } /** * @param date * @return returns starting date of the week in which the specified date falls */ public static java.util.Date getWeekEndDate(java.util.Date date) { return getWeekEndDate(date, 0); } /** * @param date * @param incrWeek * @return returns starting date of the week. The week is calculated as the week in which the specified date falls + incrWeek. */ public static java.util.Date getWeekEndDate(java.util.Date date, int incrWeek) { if (date == null) { return null; } Calendar cal = Calendar.getInstance(); cal.setTime(date); if(cal.get(Calendar.MONTH) == 0) { if(cal.get(Calendar.WEEK_OF_YEAR)==1) { if (cal.get(Calendar.DATE) < 7) { cal.roll(Calendar.YEAR,-1); } } } cal.roll(Calendar.WEEK_OF_YEAR, incrWeek); cal.set(Calendar.DATE, cal.get(Calendar.DATE) + (Calendar.DAY_OF_WEEK - cal.get(Calendar.DAY_OF_WEEK))); return cal.getTime(); }
/** * Method for retrieving the correct weekstartdate and weekenddate * even when querying for the first week of January. */
public static java.util.Date getWeekStartDateWeekly(java.util.Date date) { return getWeekStartDateWeekly(date, 0); } public static java.util.Date getWeekStartDateWeekly(java.util.Date date, int incrWeek) { if (date == null) { return null; } Calendar cal = Calendar.getInstance(); cal.setTime(date); cal.roll(Calendar.WEEK_OF_YEAR, incrWeek); cal.set(Calendar.DATE, (cal.get(Calendar.DATE) - (cal.get(Calendar.DAY_OF_WEEK) - cal.getFirstDayOfWeek()))); return cal.getTime(); } public static java.util.Date getWeekEndDateWeekly(java.util.Date date) { return getWeekEndDateWeekly(date, 0); } public static java.util.Date getWeekEndDateWeekly(java.util.Date date, int incrWeek) { if (date == null) { return null; } Calendar cal = Calendar.getInstance(); cal.setTime(date); cal.roll(Calendar.WEEK_OF_YEAR, incrWeek); cal.set(Calendar.DATE, cal.get(Calendar.DATE) + (Calendar.DAY_OF_WEEK - cal.get(Calendar.DAY_OF_WEEK))); return cal.getTime(); }
/** * Changes the value of startDate and endDate to next start date and end date respectively, Calculated by adding the difference in days between the two dates to both. * @param startDate * @param endDate */ public static void nextDate(java.util.Date startDate, java.util.Date endDate) { int diff = new Long((endDate.getTime() - startDate.getTime()) / (24 * 60 * 60 * 1000)).intValue() + 1; if (diff > 0) {
Calendar startCal = Calendar.getInstance();
startCal.setTime(startDate);
Calendar endCal = Calendar.getInstance();
endCal.setTime(endDate);
startCal.add(Calendar.DATE, diff);
endCal.add(Calendar.DATE, diff);
startDate.setTime(startCal.getTimeInMillis());
endDate.setTime(endCal.getTimeInMillis());
}
}
/**
* Changes the value of startDate and endDate to next start date and end date respectively, Calculated by substracting the difference in days between the two dates to both.
* @param startDate
* @param endDate
*/
public static void previousDate(java.util.Date startDate,
java.util.Date endDate) {
int diff = new Long((endDate.getTime() - startDate.getTime())
/ (24 * 60 * 60 * 1000)).intValue() + 1;
if (diff > 0) {
Calendar startCal = Calendar.getInstance();
startCal.setTime(startDate);
Calendar endCal = Calendar.getInstance();
endCal.setTime(endDate);
startCal.add(Calendar.DATE, -diff);
endCal.add(Calendar.DATE, -diff);
startDate.setTime(startCal.getTimeInMillis());
endDate.setTime(endCal.getTimeInMillis());
}
}
/**
* @param startDate
* @param endDate
* @param dateFormat
* @return returns an array of String whose 1st element contains next startDate and 2nd element contains next endDate, Calculated by adding the difference in days between the two dates to both.
* @throws ParseException if the startDate and endDate strings are not parsable by the specified dateFormat.
*/
public static String[] getNextDates(String startDate, String endDate, String dateFormat) throws ParseException {
String[] dates= new String[2];
Date startDt = CommonUtils.getDateFromDateString(dateFormat, startDate);
Date endDt = CommonUtils.getDateFromDateString(dateFormat, endDate);
nextDate(startDt, endDt);
dates[0] = CommonUtils.getDateStringFromDate(startDt, dateFormat);
dates[1] = CommonUtils.getDateStringFromDate(endDt, dateFormat);
return dates;
}
/**
* @param startDate
* @param endDate
* @param dateFormat
* @return returns an array of String whose 1st element contains next startDate and 2nd element contains next endDate, Calculated by substracting the difference in days between the two dates to both.
* @throws ParseException if the startDate and endDate strings are not parsable by the specified dateFormat.
*/
public static String[] getPreviousDates(String startDate, String endDate, String dateFormat) throws ParseException {
String[] dates= new String[2];
Date startDt = CommonUtils.getDateFromDateString(dateFormat, startDate);
Date endDt = CommonUtils.getDateFromDateString(dateFormat, endDate);
previousDate(startDt, endDt);
dates[0] = CommonUtils.getDateStringFromDate(startDt, dateFormat);
dates[1] = CommonUtils.getDateStringFromDate(endDt, dateFormat);
return dates;
}
/**
* This method will return true if the String is null or void.
*
* @param testString
* @return
*/
public static boolean validateString(String testString) {
return (testString == null || testString.trim().equals("")) ? true
: false;
}
/**
* This method to trim String values(It will also check null value before
* trim operation).
*
* @param stringToTrim
* @return
*/
public static String trimString(String stringToTrim) {
return stringToTrim != null ? stringToTrim.trim() : null;
}
public static boolean dateBeforeTodays(java.util.Date leaveStartDt) {
boolean flag = false;
Calendar today = Calendar.getInstance();
today.set(Calendar.HOUR,0);
today.set(Calendar.MINUTE,0);
today.set(Calendar.SECOND,0);
today.set(Calendar.MILLISECOND,0);
today.set(Calendar.AM_PM,Calendar.AM);
if(leaveStartDt.before(today.getTime())){
flag = true;
}
return flag;
}
public static boolean dateAfterTodays(java.util.Date leaveStartDt) {
boolean flag = false;
Calendar today = Calendar.getInstance();
today.set(Calendar.HOUR,0);
today.set(Calendar.MINUTE,0);
today.set(Calendar.SECOND,0);
today.set(Calendar.MILLISECOND,0);
today.set(Calendar.AM_PM,Calendar.AM);
if(leaveStartDt.after(today.getTime())){
flag = true;
}
return flag;
}
public static boolean dateAfterTomorrow(java.util.Date leaveStartDt) {
boolean flag = false;
Calendar foo = Calendar.getInstance();
foo.add(Calendar.DAY_OF_MONTH, -1);
foo.set(Calendar.HOUR,0);
foo.set(Calendar.MINUTE,0);
foo.set(Calendar.SECOND,0);
foo.set(Calendar.MILLISECOND,0);
foo.set(Calendar.AM_PM,Calendar.AM);
if(leaveStartDt.after(foo.getTime())){
flag = true;
}
return flag;
}
public static java.util.Date covertToDate(java.util.Date leaveStartDt) throws ParseException{
SimpleDateFormat dateFormatter = new SimpleDateFormat(Constants.DATE_FORMAT);
return dateFormatter.parse(dateFormatter.format(leaveStartDt));
}
/**
* Format value according to specified format string (as tag attribute or
* as string from message resources) or to current user locale.
*
* When a format string is retrieved from the message resources,
*
applyLocalizedPattern is used. For more about localized
* patterns, see
*
* (To obtain the correct value for some characters, you may need to view
* the file in a hex editor and then use the Unicode escape form in the
* property resources file.)
*
* @param value value to process and convert to String
* @param format format in which the value is to be formatted
* @param locale the locale to use
* @exception Exception if an exception has occurred
*/
public static String format(Object value, String format, Locale locale) throws Exception {
Format formatter = null;
if( value == null || format == null){
return null;
}
if (value instanceof java.lang.String) { // Return String object as is.
return (String) value;
} else {
if (value instanceof Number) { // Prepare format object for numeric values.
if (format != null) {
try {
formatter = NumberFormat.getNumberInstance(locale);
((DecimalFormat) formatter).applyLocalizedPattern(format);
} catch (IllegalArgumentException e) {
throw e;
}
}
} else if (value instanceof java.util.Date) { // Prepare format object for date .
if (format != null) {
formatter = new SimpleDateFormat(format, locale);
}
}
}
if (format != null) {
return formatter.format(value);
} else {
return value.toString();
}
}
/**
* Format value according to specified format string (as tag attribute or
* as string from message resources) or to current user locale.
*
* @param value value to process and convert to String
* @param format format in which the value is to be formatted
* @exception Exception if an exception has occurred
*/
public static String format(Object value, String format) throws Exception {
return format(value, format, CommonUtils.getLocale()) ;
}
public static String correctDate(int yy, int mm, int dd){
String year;
String month;
String date;
if(dd/10==0)
date = "0"+dd;
else
date = ""+dd;
if(mm/10==0)
month = "0"+mm;
else
month = ""+mm;
if(yy/10==0)
year = "0"+yy;
else
year = ""+yy;
return year + "-" + month + "-" + date;
}
public static String incrementDate(String incrDate, int counter){
StringTokenizer stringDate;
stringDate = new StringTokenizer(incrDate,"-");
Calendar date=Calendar.getInstance();
date.set(Calendar.YEAR,Integer.parseInt(stringDate.nextToken().trim()));
date.set(Calendar.MONTH,Integer.parseInt(stringDate.nextToken().trim()) - 1);
date.set(Calendar.DATE,Integer.parseInt(stringDate.nextToken().trim()));
date.add(Calendar.DATE,counter);
return correctDate(date.get(Calendar.YEAR),(date.get(Calendar.MONTH)+1),date.get(Calendar.DATE));
}
public static String getCurrentDate(){
Calendar cal = Calendar.getInstance();
return correctDate(cal.get(Calendar.YEAR),cal.get(Calendar.MONTH)+1,cal.get(Calendar.DATE));
}
public static boolean isDateGreaterEqual(String firstDate, String secondDate){
boolean isGreater = false;
int y1=0,m1=0,d1=0;
int y2=0,m2=0,d2=0;
StringTokenizer stringDate;
stringDate = new StringTokenizer(firstDate,"-");
y1 = Integer.parseInt(stringDate.nextToken().trim());
m1 = Integer.parseInt(stringDate.nextToken().trim());
d1 = Integer.parseInt(stringDate.nextToken().trim());
stringDate = new StringTokenizer(secondDate,"-");
y2 = Integer.parseInt(stringDate.nextToken().trim());
m2 = Integer.parseInt(stringDate.nextToken().trim());
d2 = Integer.parseInt(stringDate.nextToken().trim());
if (y1>y2) isGreater = true;
else if (y1==y2) {
if (m1>m2) isGreater = true;
else if (m1==m2)
if(d1>=d2) isGreater = true;
}
return isGreater;
}
/**
*DateDiff -- compute the difference between two dates.
* **/
public static boolean isSingleDay(java.util.Date leaveStartDt, java.util.Date leaveEndDt) {
boolean isSingleDay=true;
try {
long diff = leaveEndDt.getTime()-leaveStartDt.getTime();
if(diff>0)
{
isSingleDay=false;
}
} catch (Exception e) {
e.printStackTrace();
}
return isSingleDay;
}
/**
*DateDiff -- compute the the number of days between two dates.
* **/
public static int getNumberOfDays(java.util.Date leaveStartDt, java.util.Date leaveEndDt) {
int inDays=0;
try {
long diff = leaveEndDt.getTime()-leaveStartDt.getTime();
long inSecs =diff/1000;
long inMins=inSecs/60;
int inhrs=(int) (inMins/60);
inDays=inhrs/24;
} catch (Exception e) {
e.printStackTrace();
}
return inDays;
}
No comments:
Post a Comment