Tuesday, June 18, 2019

Write a method with any number of arguments

Java we can write a method with any number of arguments using elispe as below

protected boolean areAllStringsSet(String... theStrings){

}

When we write amethod loke this it is always better to validate whether all the arguments are valid, NOT NULL

below is a method to check and validate whether those are NULL.
you can use for any check and modify as you need

/**
* If any of the supplied strings are empty then return false;
*/
protected boolean areAllStringsSet(String... theStrings){
boolean retVal = true;
if(theStrings != null){
for(int i=0;i String str = theStrings[i];
if(str != null){
if(str.trim().length() < 0){
retVal = false;
break;
}
}else{
retVal = false;
break;
}
}
}

return retVal;
}

Thursday, June 13, 2019

Java :: Compare Two dates

If you have tried to compare dates in java , you would have experience trouble checking "equal".
This is because, date include a time component. There is a high chance that time component can be different.
But if we need to compare two dates, only looking at the "Year","Month","Day"
Then Time must be removed from the date.
Below example shows how to do that.

package com.date.util;

import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

public class DateConverter {

public static void main(String[] args) {
Date curDate = new Date();
System.out.println(curDate);
System.out.println(getDateWithoutTime(curDate));
}

/**
* Check whether First date is after or equal to Second date. When
* comparing the dates, the time element is removed before compare, else always
* not equal due to time component.
* Second Date | First Date
*
* @param firstDate
*            First date to be compared.
* @param secondDate
*            Second date to be compared.
* @return True when First date is after or equal Second date and False
*         vice versa.
*/
public final static boolean isFirstDateAfterOrEqualToSecondDate(Date firstDate, Date secondDate) {
if (firstDate != null && secondDate != null) {
Date firstDateWithoutTime = getDateWithoutTime(firstDate);
Date secondDateWithoutTime = getDateWithoutTime(secondDate);
return ((firstDateWithoutTime.after(secondDateWithoutTime))
|| (firstDateWithoutTime.equals(secondDateWithoutTime)));
}
return false;
}

/**
* Check whether First date is after Second date. When comparing the
* dates, the time element is removed before compare.
* Second Date | First Date
*
* @param firstDate
*            First date to be compared.
* @param secondDate
*            Second date to be compared.
* @return True when First date is after Second date and False vice
*         versa.
*/
public final static boolean isFirstDateAfterSecondDate(Date firstDate, Date secondDate) {
if (firstDate != null && secondDate != null) {
return (getDateWithoutTime(firstDate)).after(getDateWithoutTime(secondDate));
}
return false;
}

/**
* Check whether First date is before or equal to Second date. When
* comparing the dates, the time element is removed before compare, else always
* not equal due to time component.
* First Date | Second Date
*
* @param firstDate
*            First date to be compared.
* @param secondDate
*            Second date to be compared.
* @return True when First date is before or equal Second date and False
*         vice versa.
*/
public final static boolean isFirstDateBeforeOrEqualToSecondDate(Date firstDate, Date secondDate) {
if (firstDate != null && secondDate != null) {
Date firstDateWithoutTime = getDateWithoutTime(firstDate);
Date secondDateWithoutTime = getDateWithoutTime(secondDate);
return ((firstDateWithoutTime.before(secondDateWithoutTime))
|| (firstDateWithoutTime.equals(secondDateWithoutTime)));
}
return false;
}

/**
* Check whether First date is before to Second date. When comparing the
* dates, the time element is removed before compare, else always not equal due
* to time component.
* First Date | Second Date
*
* @param firstDate
*            First date to be compared.
* @param secondDate
*            Second date to be compared.
* @return True when First date is before Second date and False vice
*         versa.
*/
public final static boolean isFirstDateBeforeSecondDate(Date firstDate, Date secondDate) {
if (firstDate != null && secondDate != null) {
return (getDateWithoutTime(firstDate)).before(getDateWithoutTime(secondDate));
}
return false;
}

/**
* Check whether date has a time component.
*
* @param aDate
*            valid date object.
* @return True when there is not time value for date and False vice versa.
*/
public final static boolean isTimeEqualToZero(Date aDate) {
if (aDate != null) {
Calendar aCalendarDate = new GregorianCalendar();
aCalendarDate.setTime(aDate);

int theHour = aCalendarDate.get(Calendar.HOUR_OF_DAY);
int theMinute = aCalendarDate.get(Calendar.MINUTE);

return (theHour == 0 && theMinute == 0);
}

return false;
}

/**
* Get the Date without Time. In Other words,get only Year,Month,day component.
*
* @param aDate
*            Valid date object.
* @return Date without Time.
*/
public static final Date getDateWithoutTime(Date aDate) {
Date rtnDate = null;
if (aDate != null) {
Calendar aCalendarDate = new GregorianCalendar();

aCalendarDate.setTime(aDate);
aCalendarDate.set(GregorianCalendar.HOUR, 0);
aCalendarDate.set(GregorianCalendar.MINUTE, 0);
aCalendarDate.set(GregorianCalendar.SECOND, 0);
aCalendarDate.set(GregorianCalendar.MILLISECOND, 0);
aCalendarDate.set(GregorianCalendar.AM_PM, GregorianCalendar.AM);
rtnDate = aCalendarDate.getTime();
}
return rtnDate;
}
}

Wednesday, June 5, 2019

Important Online Payment Links


To Pay Water Bill
National Water Supply & Drainage Board
https://payments.waterboard.lk/payment/index

Telephone Bill Payments - NTB card accessible
Mobitel - go to site - click payment link in top right corner (Pay Bill)

https://online.mobitel.lk/onlinepay/

Dilog also same as Mobitel