Thursday, August 16, 2007

Java Comments Generation

To enable javadoc comments in your IDE, go to

project-->properties-->Java Code Style-->Code Templates.



In code templates, set the comment pattern for individual comment type.

For example, you need to set the comment type for Method, then click on edit
and give the pattern

/**

* return Returns the ${return_type}

*/

and save the settings. In your "file.java", on the method do alt+shift+j to get the comment for the method.


Please ensure that we have comments for every variable, method etc in our files.

NOTE:

Kindly use the below pattern

/**

* ${tags} Returns the ${return_type}

*/

As it produces results like

/**

* @param objRequestDTO

* @return Returns the RequestDTO

*/

With the below pattern

/**

* return Returns the ${return_type}

*/

The output will be

/**

* return Returns the RequestDTO

*/

Tuesday, August 07, 2007

blank

Date Utility Methods

Following are different Date utility methods.

1. Code for getting current system time in java.sql.Timestamp format.

public static java.sql.Timestamp todaysSqlDateTime(){
java.util.Date today = new java.util.Date();
java.sql.Timestamp sqlToday = new java.sql.Timestamp(today.getTime());
return sqlToday;
}

2. Code for getting current system time in java.sql.Date format.

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

3. Code for getting current system time in java.util.Date format.

public static Date getCurrentSystemDate() {
Date objDate = new Date();
return objDate;
}

4. Code for getting current system time in java.util.Date format.

public static java.util.Date getCurrentSystemDate2() {
java.util.Date objDate = new java.util.Date(Calendar.getInstance().getTimeInMillis());
return objDate;
}

5. Code to convert util.Date to sql.Date.

public static java.sql.Date utilDateTOSQLDate(java.util.Date utilDate) {
java.sql.Date sqlDate = null;
if(utilDate != null){
sqlDate = new java.sql.Date(utilDate.getTime());
}
return sqlDate;
}

6. Code to convert sql.Date.to util.Date

public static Date convertSQLDateTOUtilDate(java.sql.Date sqlDate) {
java.util.Date utilDate=null;
try{
if(sqlDate != null) {
SimpleDateFormat sdf = new SimpleDateFormat("dd-M-yyyy HH:mm:ss");
utilDate = new Date(sqlDate.getTime());
utilDate = sdf.parse(utilDate.toString());
}
}
catch(Exception ex) {
}
return utilDate;
}

7. Code to convert java.util.Date to String Date

public static String convertUtilDateToStringDate(Date objDate) {

DateFormat dateFormat = DateFormat.getDateInstance(DateFormat.MEDIUM);
String usaFormatDate = dateFormat.format(objDate);

return usaFormatDate ;
}

8. Code to convert String Date to java.util.Date

Example of datepattern
"MM/dd/yyyy"
MM/dd/yyyy HH:mm:ss

public static Date stringDateToUtilDate(String stringDate, String datepattern) {

java.util.Date utilDate = null;

try{
if (null != stringDate && !(stringDate.trim().equals("")))
{
SimpleDateFormat formattedDate = new SimpleDateFormat(datepattern);
utilDate = formattedDate.parse(stringDate);
}
}catch(ParseException exception){exception.printStackTrace();}
catch(Exception exception){exception.printStackTrace();}
return utilDate;
}

9. Date subtraction

public static Double compareDates(Date date1, Date date2) {

double difdays = 0.0;
if(date1!= null & date2!= null) {
long difMillSecs = date1.getTime() - date2.getTime();
double difSecs = (double)difMillSecs / 1000.0;
double difMins = difSecs / 60.0;
double difHours = difMins / 60.0;
difdays = difHours/ 24.0;
}
return difdays ;
}

10. Need to know whether Date1 is (after/before) Date2

We can make use of before()/after() methods that are there in java.util.Date class.

The above classes can be found in the following packages
1. java.util.Date
2. java.sql.Date
3. java.sql.Timestamp
4. java.text.DateFormat
5. java.text.SimpleDateFormat