Tuesday, March 24, 2009

Exists - Keyword

The EXISTS condition is considered "to be met" if the subquery returns at least one row.

This link helps us to understand EXISTS better.

Tuesday, August 19, 2008

Regex - Tutorial

This link helps us to understand better about the Regex.

X{n} – means exactly n occurrences of X.
X{n,} – means at least n occurrences of X and there is no limit for maximum occurrence.
X{n,m} – means at least n and at most m occurrences of X.
X+ - means one or more occurrences of X.
x*- means zero or more occurrences of X.

Pattern pattern = Pattern.compile("He*llo");
CharSequence inputStr = "Hallo Hello Heello Hzllo2 H_llo Hillo1";
Matcher matcher = pattern.matcher(inputStr);
while(matcher.find())
{
int start = matcher.start();
int end = matcher.end();
System.out.println("Pattern found: " + inputStr.subSequence(start, end).toString());
}

Monday, August 04, 2008

How to add days (Say 10 or 30 days) to a existing date (Say current date) and return the new date?

/*
How to add days (Say 10 or 30 days) to a existing date (Say current date) and return the new date?
Usage: To find the expiry date of user id. Example: Whenever user logins to a system, his lastlogondate is updated with current date.If in a system user does not login for 90 days, then the user id should be locked. For suchkind of checking we can use this logic
*/

import java.util.Date;
public class DateTest1 {

/*
If the new date send by addCalcDate is after todays current date then we disable the user from the system.
*/

public static void main(String args[]) {
if(todaysSqlDate().after(addCalcDate(objGTEMUser.getLastLogonDate(),90))) {
System.out.println("Disable the user: ");
}
}

/*
We pass the last logon date and add 90 days to it and send the new date.
*/

public static Date addCalcDate(Date baseDate,int calc) {
java.util.GregorianCalendar cal = new java.util.GregorianCalendar();
cal.setTime(baseDate);
cal.add(java.util.GregorianCalendar.DATE, calc);
return cal.getTime();
}


public static java.sql.Date todaysSqlDate(){
return new java.sql.Date(Calendar.getInstance().getTimeInMillis());
}
}

Sunday, July 06, 2008

Preventing the process from running between 12 am to 7 am

// Here is the implementation that helps to set a particular process not to run between 12 am to 7 am

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class Test {
/**
* @param args
*/
public static void main(String[] args) {
if(!checkTimeFrame()) {
System.out.println("Do not run the process - since time is between 12 am and 7 am");
}
}
public static boolean checkTimeFrame(){
Date currentDateTime =todaysDateTime();
System.out.println("currentDateTime: " + currentDateTime);
Date t=stringDateToUtilDate(todaysDate());
System.out.println("Before adding 7 hours t: " + t);
t.setTime(t.getTime()+25200000); // 25200000 = 7 X 60 X 60 X 1000 = 7 hours
System.out.println("After adding 7 hours t: " + t);
System.out.println("Is current time later than 7 AM: " + currentDateTime.after(t));
return currentDateTime.after(t);
}
public static Date todaysDateTime() {
return new Date() ;
}
// Since we are formatting the date, time is lost. Only the date component is retained.
public static String todaysDate() {
Date currentSystemdate = new Date();
SimpleDateFormat formater = new SimpleDateFormat("MM/dd/yyyy");
String stringDate = formater.format(currentSystemdate);
System.out.println("Formatted (\"MM/dd/yyyy\") date: " + stringDate);
return stringDate ;
}
public static Date stringDateToUtilDate(String stringDate) {
java.util.Date utilDate = null;
try{
if (null != stringDate && !(stringDate.trim().equals("")))
{
SimpleDateFormat formattedDate = new SimpleDateFormat("MM/dd/yyyy");
utilDate = formattedDate.parse(stringDate);
System.out.println("String to util date: " + utilDate);
}
}catch(ParseException exception){exception.printStackTrace();}
catch(Exception exception){exception.printStackTrace();}
return utilDate;
}
}

Saturday, March 29, 2008

Does Java pass by reference or pass by value?

Thursday, March 27, 2008

How to find out duplicates in a List

List objStringList = new ArrayList();
objStringList.add("a");
objStringList.add("b");
objStringList.add("c");
objStringList.add("d");
objStringList।add("c");

Approach - I
Iterator objIterator = objStringList.iterator();
while(objIterator.hasNext()) {
String value = objIterator.next();
if(objStringList.indexOf(value) != objStringList.lastIndexOf(value)) {
System.out.println("\n Duplicates detected: " + value);
}
}

for(String objString: objStringList) {
if(objStringList.indexOf(objString) != objStringList.lastIndexOf(objString)) {
System.out.println("\n Duplicates detected: " + objString);
}
}

Approach - II

Set objStringSet = new HashSet();
for(String objString: objStringList) {
boolean result = objStringSet.add(objString);
if (result == false) {
System.out.println("\n Duplicates detected: " + objString);
}
}

Approach - III

List list = new ArrayList();
Set set = new HashSet();
list.add("1");
list.add("2");
list.add("3");
list.add("4");
list.add("5");
set.addAll(list);
if(set.size() < list.size()) {
System.out.println("list has duplicates");
}

Thursday, January 03, 2008

FAQ

Tutorial - one