Thursday, 10 January 2013

Date Manipulations using Calendar class in Java

Date Manipulations using Calendar class in Java



Date using Calender class in Java

The Calendar class is an abstract class that provides methods for converting between a specific instant in time and a set of calendar fields such as YEAR, MONTH, DAY_OF_MONTH, HOUR, and so on, and for manipulating the calendar fields, such as getting the date of the next week.

In this post I'm going to explain you how to get the first day of current week, last day of current week, first day of previous week, last day of previous week, and similarly for month and year.

Calendar's getInstance method returns a Calendar object whose calendar fields have been initialized with the current date and time. Using this Instance we are going to set  the calender fields accordingly to get the specific day of the year.

 java.util.Calendar cal = java.util.Calendar.getInstance(); //Calendar instance set to current Date and Time

Let's take a simple date formatter to get the date in our desired format.

 DateFormat dateFormat = new SimpleDateFormat("dd-mm-yyyy"); // Give date in dd-mm-yyyy format.
  
First & Last Date of Current Week
cal.set(java.util.Calendar.DAY_OF_WEEK,1);  // will reset to sunday, first day of week
String currentWeekFirstDay=dateFormat.format(cal.getTime());
              
cal.set(java.util.Calendar.DAY_OF_WEEK,7); // will reset to saturday, last day of week
String currentWeekLastDay=dateFormat.format(cal.getTime());

              
First & Last Date of Previous Week

cal.add(java.util.Calendar.WEEK_OF_YEAR,-1); // Will reduce one week time
              
cal.set(java.util.Calendar.DAY_OF_WEEK,1);  // will reset to sunday, first day of week
String previousWeekFirstDay=dateFormat.format(cal.getTime());
              
cal.set(java.util.Calendar.DAY_OF_WEEK,7);  // will reset to saturday, last day of week
String previousWeekLastDay=dateFormat.format(cal.getTime());

              
First & last Date of current Month
cal.set(java.util.Calendar.DAY_OF_MONTH,1);  // will reset to first day of current month
String currentMonthFirstDay=dateFormat.format(cal.getTime());

cal.set(java.util.Calendar.DAY_OF_MONTH,cal.getActualMaximum(java.util.Calendar.DAY_OF_MONTH));

String currentMonthLastDay=dateFormat.format(cal.getTime());
  
First & Last Date of previous Month

cal.add(java.util.Calendar.MONTH,-1); // will reduce one month time
              
cal.set(java.util.Calendar.DAY_OF_MONTH,1);  // will reset to first day of previous month
String previousMonthFirstDay=dateFormat.format(cal.getTime());

cal.add(java.util.Calendar.DAY_OF_MONTH,cal.getActualMaximum(java.util.Calendar.DAY_OF_MONTH)-1);
String previousMonthLastDay=dateFormat.format(cal.getTime());

  
First and Last Date of Year

cal.set(java.util.Calendar.DAY_OF_YEAR, 1);  //will reset to first day of year
String yearFirstDay=dateFormat.format(cal.getTime());

cal.set(java.util.Calendar.DAY_OF_YEAR, cal.getActualMaximum(java.util.Calendar.DAY_OF_YEAR));
String yearLastDay=dateFormat.format(cal.getTime());

  
  
Sample Program

DateSample.java

package com;

//Author: Chandrasekhar Atina
import java.text.DateFormat;
import java.text.SimpleDateFormat;

public class DateSample {

public static void main(String[] args) {

String currentWeekFirstDay;
String currentWeekLastDay;
String previousWeekFirstDay;
String previousWeekLastDay;
String currentMonthFirstDay;
String currentMonthLastDay;
String previousMonthFirstDay;
String previousMonthLastDay;
String yearFirstDay;
String yearLastDay;
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
java.util.Calendar cal = java.util.Calendar.getInstance();

cal.set(java.util.Calendar.DAY_OF_WEEK,1);
currentWeekFirstDay=dateFormat.format(cal.getTime());
cal.set(java.util.Calendar.DAY_OF_WEEK,7);
currentWeekLastDay=dateFormat.format(cal.getTime());
cal.add(java.util.Calendar.WEEK_OF_YEAR,-1);
cal.set(java.util.Calendar.DAY_OF_WEEK,1);
previousWeekFirstDay=dateFormat.format(cal.getTime());
cal.set(java.util.Calendar.DAY_OF_WEEK,7);
previousWeekLastDay=dateFormat.format(cal.getTime());
          
cal = java.util.Calendar.getInstance(); // resetting the calendar instance
          
cal.set(java.util.Calendar.DAY_OF_MONTH,1);
currentMonthFirstDay=dateFormat.format(cal.getTime());
cal.add(java.util.Calendar.DAY_OF_MONTH,cal.getActualMaximum(java.util.Calendar.DAY_OF_MONTH)-1);
currentMonthLastDay=dateFormat.format(cal.getTime());
cal.add(java.util.Calendar.MONTH,-1);
cal.set(java.util.Calendar.DAY_OF_MONTH,1);
previousMonthFirstDay=dateFormat.format(cal.getTime());
cal.add(java.util.Calendar.DAY_OF_MONTH,cal.getActualMaximum(java.util.Calendar.DAY_OF_MONTH)-1);
previousMonthLastDay=dateFormat.format(cal.getTime());

cal = java.util.Calendar.getInstance(); // resetting the calendar instance

cal.set(java.util.Calendar.DAY_OF_YEAR, 1);
yearFirstDay=dateFormat.format(cal.getTime());
cal.set(java.util.Calendar.DAY_OF_YEAR, cal.getActualMaximum(java.util.Calendar.DAY_OF_YEAR));
yearLastDay=dateFormat.format(cal.getTime());
      
System.out.println("First Day of Current Week  : "+currentWeekFirstDay);
System.out.println("Last Day of Current Week   : "+currentWeekLastDay);
System.out.println("First Day of Previous Week : "+previousWeekFirstDay);
System.out.println("Last Day of Previous Week  : "+previousWeekLastDay);
System.out.println("First Day of Current Month : "+currentMonthFirstDay);
System.out.println("Last Day of Current Month  : "+currentMonthLastDay);
System.out.println("First Day of Previous Month: "+previousMonthFirstDay);
System.out.println("Last Day of Previous Month : "+previousMonthLastDay);
System.out.println("First Day of Current Year  : "+yearFirstDay);
System.out.println("Last Day of Current Year   : "+yearLastDay);
}
}

 
Output

Friday, 4 January 2013

How to use Google Maps in our Application

How to use Google Maps in our Application
We can use Google Maps JavaScript API v3 for using Google Maps in our Web Applications.

Visit APIs Console at https://code.google.com/apis/console and log in with your Google Account and activate the Google Maps API v3 service in order to use the Google Maps Services.

Generate API key from the API Access page for loading Maps in your Web Application.

Note: Use can use Google Maps for free if your daily usage is limited.If Maps API usage exceeds the Usage Limits, you must load the Maps API using an API key in order to purchase additional quota.

Please take a look at the sample Map centered on Sydney, New South Wales, Australia.



Reference URL : Google Maps JavaScript API v3



This post is posted on Mr.Praveen Kumar RV's request. Thanks for the request, Happy Coding :)

Thursday, 3 January 2013

Dynamic Deployment (Hot Deployment) of Web Application on Tomcat

Dynamic Deployment (Hot Deployment) of Web Application on Tomcat

Dynamic Deployment of Web Application on Tomcat

 

In order to deploy an application on Tomcat server we need to build a WAR file, upload it and deploy the web application. When it comes to development environment, it becomes a tedious process to build a WAR file and deploy it again and again as we make changes to our application. In this perspective, the Dynamic Deployment of Web Application is very helpful in development environments for Developers.

Instead of deploying a WAR file, the web application deployment can also be done using a Context File. The following are the steps to be followed for accomplishing it.

Step 1: Create a context file and drop it inside {CATALINA_HOME}\ conf\Catalina\localhost folder.

** {CATALINA_HOME} represents the Tomcat Home Directory

A Simple Context File   

struts.xml

<?xml version="1.0" encoding="UTF-8"?>

<Context  docBase="D:/WS/STRUTS2/WebContent"  path="/struts"  />

Here  docBase represents the base directory of web application having the Tomcat Web Application Directory Structure and path represents the Context Path.

Tomcat Web Application Directory Structure

If you have any JNDI resources configured in Tomcat Server, we need to add them inside the context file as follows

Interflora.xml

<?xml version="1.0" encoding="UTF-8"?>

<Context

  docBase="D:\WS\sample

  path="/sample"  >

 

<Resource name="jdbc/sampleDS"

   auth="Container"

   type="oracle.jdbc.pool.OracleDataSource"

   driverClassName="oracle.jdbc.driver.OracleDriver"

   factory="oracle.jdbc.pool.OracleDataSourceFactory"

   url="jdbcURL"

   user="username"

   password="password"

   maxActive="20"

   maxIdle="10"

   maxWait="-1" />

</Context>

Here Database Connection details are configured inside Tomcat as JNDI resource.

Drop the Context file inside {CATALINA_HOME}\ conf\Catalina\localhost folder as follows and restart the Tomcat Server.

Whenever you make any changes in the application just run the ANT/Maven build, which in turn reflects the changes in the Tomcat Server as well.

 

Note: You need to restart the server if you make any changes to xml/properties files and optionally for java files as well.

The below screen shot shows that the application deployed on server using context file rather than a WAR file

Wednesday, 2 January 2013

Calling a Stored Procedure from Jasper Reports

Calling Oracle Stored Procedure in JasperReports

We can execute a Stored Procedure in Jasper as PLSQL is supported in Jasper.
Prerequisites 
  •  The procedure should have an out parameter of Cursor type.
  • Any inputs to procedure should be sent as in parameters.
  • We need to define field names in iReport Designer manually that exactly match the field names that are returned by cursor
Step1:  Create a blank page in iReport Designer.
 https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEix4zFkHL-OFrhYM80NT1gKzcxPJYA40lXvGjasOjNLiWDu-P6OGEXC9IWk1W9ksv0MbpP5jCEy3NiFvfDk7xsrW9zkStbwWQX8ao-lrEaI2a47FyGr0spH4b9A7SJygDpbdXiOu_8L3jyt/s640/1.jpg
Step2:  We need to add PLSQL Executor in order to execute PLSQL blocks.  In Latest versions of ireport Designer, it will be already added. But if you are using earlier version, you need to add the following properties.
Go to Tools>>Options>>Query Executers 
Language =plsql
Factory Class =com.jaspersoft.jrx.query.PlSqlQueryExecuterFactory
Fields Provider Class =com.jaspersoft.ireport.designer.data.fieldsproviders.SQLFieldsProvider
https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgFwL7BMe6KImH73_Pmfpt2VwO9zq__DJCoxyIRX6suGtoKxYggVlWrNAaOnZFCu0-vdqUtcAhiW4S07ALpIgnPKRM0kHCNXWdQlcMn58X3_t6lTctHJXkFV3XOhaRbdhZPnlojSnTJBlz0/s640/2.jpg
Make sure that you add jasperreports-extensions-3.5.3.jar  and ojdbc14.jar files in classpath
https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiBqz4xqK1_y8Nk-U0yZMs89YGddGwmGh2jO5KJBxYY4mgs3J4q0EO8K2rLOGhOjAqUrXtgCGfQM1zL003RuGS-hYcm5Ca9E9dYm4UpoIhSxjs958i5DuUlJF1ngV3BO6GgF8ZuBdrtQ0dy/s640/3.jpg
Step 3:  Open  report Query  and select Query Language = “plsql”. If you observe the available parameters on the right hand side, you will find ORACLE_REF_CURSOR parameter which is going to be the out parameter for your procedure. Now write your PLSQL code inside curly braces {}.
{call PKG_REPORTS_API.get_shop_details_report ($P{memberNumber},$P{orderNumber}, $P{ORACLE_REF_CURSOR})}

https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgVNgw9p_vmrZfo0xNgax5AlIfh_auc9jjOEbnlDR41WwZ64385ZkcUmo5yyp14-11cMpzqQN9s0cMnO1g59lzPlK0J8mC77MuUfmVgVYPQ6XCtOPT8pRSfbtmKS6l5aPAiMHDPIKFylWli/s640/4.jpg
If you are using old versions you can create your own parameter name “cursor” with datatype as “java.sql.ResultSet” and also make sure “use for prompt” is not selected.
Step 4:  Now use the fields in your report and execute the report to see the results.
https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhJwE5dulznO4Qptji6p4DUAa2f6CliO4Fkk4sg2e5Z5mSXj-_oozvKBRPhMQ-x6hiR5LpIqR3UYn55wFWc0D1cKauJYVv9rt_183GoBMphSeOBwiR7bvAR-WY6TfZEEvF2n1xkVFxGyi74/s640/5.jpg
Results Screen
https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjfn73TOyh56bkMeU7siUQ86NKlzz-cM7AO1cyGm64SxeKyW9cHD0wDf_6d_IyWMykvhrgAHMp6zPRZI4Ddmob9dIhZLC0xBLX_sj9ekNlwoChP5K3LrApVG8KLqLgVIIZMcRuwNN_qyn3B/s640/6.jpg

sample Page

Hi,
Please post a comment for any sought of java solutions. I'm ready to help you.
Lot more updates to come shortly.... Stay Tuned.