How to Convert String into Calendar Date in Java

Hello today i am telling you how to convert string into calendar date and it can be use with
Check : How to get future date from current date in java
and here if we fetch date from database and then we want future date then what we do. How can we convert the date(String ) into calendar date.
How to Convert String into Calendar Date in Java, Convert String into Calendar Date in Java,String into Calendar Date in Java,into Calendar Date in Java,Calendar Date in Java,Date in Java,

Here is the solution :
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;



public class Testing {

  public static void main(String[] args) {
   
   try
   {
   DateFormat formatter = new SimpleDateFormat("MM/dd/yyyy");
   Date date = formatter.parse("01/29/2002");
   Calendar calendar = Calendar.getInstance();
   calendar.setTime(date);
   
   System.out.println("Changes date : " + (calendar.get(Calendar.MONTH) + 1) + "-"
          + calendar.get(Calendar.DATE) + "-" + calendar.get(Calendar.YEAR));

    
   }
   catch(Exception e)
   {
    System.out.println(e);
   }
    
    
    
  }
}
Thank you....

How to make seo friendly url in JSP

Today i am telling you how to convert your ugly url into seo friendly url in JSP. If you've any query about :

How to make seo friendly url in JSP,make seo friendly url in JSP,seo friendly url in JSP,friendly url in JSP,url in JSP,url rewriting in jsp,htaccess in jsp,url redirect in jsp,url beautifier in jsp

then this post is for you.
First of all you need to add urlrewritefilter-4.0.3.jar to your WEB-INF/lib
Download this jar file :  urlrewritefilter-4.0.3.jar 
How to make seo friendly url in JSP,make seo friendly url in JSP,seo friendly url in JSP,friendly url in JSP,url in JSP,url rewriting in jsp,htaccess in jsp,url redirect in jsp,url beautifier in jsp

To WEB-INF/web.xml add (near the top above any servlet mappings)
<filter>
    <filter-name>UrlRewriteFilter</filter-name>
    <filter-class>org.tuckey.web.filters.urlrewrite.UrlRewriteFilter</filter-class>
</filter>
<filter-mapping>
    <filter-name>UrlRewriteFilter</filter-name>
    <url-pattern>/*</url-pattern>
    <dispatcher>REQUEST</dispatcher>
    <dispatcher>FORWARD</dispatcher>
</filter-mapping>
Add urlrewrite.xml in WEB-INF
You've done all the steps correctly.

Download PDF of this post : click here
Example :
Suppose if your url is
http://sawan-pc:8080/NewEMS/JSON_user_chats.jsp?userid=GeekOnJavaand if i want
http://sawan-pc:8080/NewEMS/json/GeekOnJava
You just add below code in urlrewrite.xml
<rule>
            <from>/json/([^-]+?)/([^-]+?)</from>
            <to>/JSON_attedencein.jsp?user=$1&amp;pass=$2</to>
        </rule>
/////

How to get future date from current date in java

Hello friends sometimes we need to find out the 2 month or 6 month or any future date from current date in our project in java. Then its very easy solution for you which i told you.
How to get future date from current date  in java,get future date from current date  in java,future date from current date  in java,date from current date  in java,from current date  in java,current date  in java,date  in java,date after particular month in java

First of all we need to import Calendar
import java.util.Calendar;
Now we need to calculate current date and add into a String.
Calendar now = Calendar.getInstance();
    System.out.println("Current date : " + (now.get(Calendar.MONTH) + 1) + "-"
        + now.get(Calendar.DATE) + "-" + now.get(Calendar.YEAR));
At last if we need to find out 6 month after date from current date.
now.add(Calendar.MONTH, 2);

    System.out.println("date after one month : " + (now.get(Calendar.MONTH) ) + "-"
        + now.get(Calendar.DATE) + "-" + now.get(Calendar.YEAR));
Here is the final and full code :
import java.util.Calendar;

public class Testing {

  public static void main(String[] args) {
    Calendar now = Calendar.getInstance();
    System.out.println("Current date : " + (now.get(Calendar.MONTH) + 1) + "-"
        + now.get(Calendar.DATE) + "-" + now.get(Calendar.YEAR));

    // add days to current date using Calendar.add method
    now.add(Calendar.MONTH, 6);

    System.out.println("date after six month : " + (now.get(Calendar.MONTH) ) + "-"
        + now.get(Calendar.DATE) + "-" + now.get(Calendar.YEAR));
    
  
    
    
    
  }
}
Thank you for visiting....

How to get data from JSON url in java : Easy Steps

Hello friends, i am coming with a new trick which you need in your daily coding life. Today i am telling you how to get data from url which is in JSON form.
But before getting json data i recommend you for :
Create JSON http://geekonjava.blogspot.in/2014/06/how-to-create-json-in-java-easy-step.html
And if you want another step to read and write json. You don't need to go away just check below link :
read and write JSON http://geekonjava.blogspot.in/2014/01/how-to-read-and-write-json-in-java.html
Now come to start our code cooking with the name of  GeekOnJava.
How to get data from JSON url in java : Easy Steps,get data from JSON url in java : Easy Steps,data from JSON url in java : Easy Steps,from JSON url in java : Easy Steps,JSON url in java : Easy Steps,url in java : Easy Steps,java : Easy Steps

But first of all you need to download jar files which  are present in my previous post :
and below full code to give data from json url in java.
package Com.Sanskriti;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.net.URL;
import org.json.JSONException;
import org.json.JSONObject;
import java.nio.charset.Charset;

public class GetJsonData {
 
/////////////////////JSON method//////////////////////////////////////////////
    
private static String readAll(Reader rd) throws IOException {
StringBuilder sb = new StringBuilder();
int cp;
while ((cp = rd.read()) != -1) {
sb.append((char) cp);
}
return sb.toString();
}

public static JSONObject readJsonFromUrl(String url) throws IOException, JSONException {
InputStream is = new URL(url).openStream();
try {
BufferedReader rd = new BufferedReader(new InputStreamReader(is, Charset.forName("UTF-8")));
String jsonText = readAll(rd);
JSONObject json = new JSONObject(jsonText);
return json;
} finally {
is.close();
}
}

/////////////////////////////JSON method End////////////////////////////////////////

 /**
  * @param args
  */
 public static void main(String[] args) {
  
  
  String url="http://sawan-pc:8080/NewEMS/JSON_attedencein.jsp?user=admin&pass=admin123";
  try {
   JSONObject json = readJsonFromUrl(url);
   System.out.println(json.get("message"));
   
  } catch (IOException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  } catch (JSONException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
  // TODO Auto-generated method stub

 }

}

If your JSON is :
{"message":"Login UnSuccessfull","status":"0"}
then output would be
Login UnSuccessfull

Thank you and enjoy baked cake.

How to Create JSON in Java : Easy Step

Hello friends i am telling you how to create json in java. Yes i know i already post on this article at
Read this : http://geekonjava.blogspot.in/2014/01/how-to-read-and-write-json-in-java.html
But this is a great and easy step which i used in my current project. Hope you'll get the point.
If you want to use this method then you need to add these jar file.
Get Jar Files : https://app.box.com/s/hw38jx263rz7c10mjl9n 
In this zip file you'll get two jar file.
How to Create JSON in Java : Easy Step, Create JSON in Java : Easy Step,JSON in Java : Easy Step,Java : Easy Step,
  1. json-simple-1.1.1
  2. java-json 
After add these jar file you're ready to bake a code.
First of all you need to import jar
import org.json.simple.JSONObject
then after go to coding section.
Suppose you want to make below type of JSON :
{"message":"GeekOnJava","status":"200"}
You need to write only two line.
JSONObject obj=new JSONObject();
obj.put("status", "1");
obj.put("message", "GeekOnJava");
System.out.print(obj);
Full source code of create JSON :
package com.blogspot.geekonjava;
import org.json.simple.JSONObject;
public class MakeJson {

 /**
  * @param args
  */
 public static void main(String[] args) {
  // TODO Auto-generated method stub
   JSONObject obj=new JSONObject();
    obj.put("status", "1");
          obj.put("message", "GeekOnJava");
          System.out.print(obj);

 }

}

Thank you for your appreciation.


How to use Shaking effect in Swing

Sometimes when we want to perform some effect when login unsuccessful and here i am telling you that how to use shaking effect in unsuccessful login with Swing.
The logic behind the idea is to move the direction/position and then back to the original position of JFrame for a certain time to feel the vibrate or shaking mood of JFrame.
How to use Shaking effect in Swing,use Shaking effect in Swing,Shaking effect in Swing,effect in Swing,
Did you made : Make KeyLogger in Java
This can be implemented to log in system to make a twist for your conventional log in system. Try this. Hope you will enjoy.
import java.awt.Point;
import java.awt.event.*;
import java.util.Random;
import javax.swing.*;
 
public class ShakingFrame 
{
    //variables
    private JFrame frame;
    public static final int UPDATE_TIME = 2;
    public static final int DURATION = 1000;
 
    private Point primaryLocation;
    private long startTime;
    private Timer time;
 
    public ShakingFrame(JFrame f)
    {
        this.frame = f;
    }//constructor
     
    //used on a 'ShakingFrame' object to shake the jframe
    public void startShake()
    {
        primaryLocation = frame.getLocation();
        startTime = System.currentTimeMillis();
        time= new Timer(UPDATE_TIME,timeListener);
        time.start();
    }
     
    //stops shake/puts back in original place
    public void stopShake()
    {
        //code to stop the screen shaking
        time.stop();
        frame.setLocation(primaryLocation);
        frame.setVisible(true);
        frame.repaint();
    }
     
    private class ActionTime implements ActionListener
    {
         private int xOffset, yOffset;
         //every interval the timer ticks, this is performed
        @Override
         public void actionPerformed(ActionEvent e)
         {
             //get elapsed time(running time)
             long elapsedTime = System.currentTimeMillis() - startTime;
             Random r = new Random();
             int op = r.nextInt(5);
              
             if ( op > 0)
             {
                //change x and y offset then reallocate frame
                xOffset = primaryLocation.x + (r.nextInt(20));
                yOffset = primaryLocation.y + (r.nextInt(20));
                frame.setLocation(xOffset,yOffset);
                frame.setVisible(false);                
                frame.repaint();
             }
             else
             {
                //change x and y offset then reallocate frame
                xOffset = primaryLocation.x - (r.nextInt(20));
                yOffset = primaryLocation.y - (r.nextInt(20));
                frame.setLocation(xOffset,yOffset);
                frame.setVisible(true);
                frame.repaint();
             }
             //elapsedTime exceed  DURATION, so stop now
             if(elapsedTime > DURATION)
             {   
                stopShake();
             }
         }
    }
    //listener/instance of ActionTime
    private ActionTime timeListener = new ActionTime();
}
In your log in frame if log in unsuccessful put the following code
ShakingFrame s = new ShakingFrame(this); 
s.startShake();
Thank You 

How to get list of dates between two dates in Java

Hello friends i am telling you how to get list of dates between two dates in Java. I used this program in my Employee Information project where i got a task to show the all leave of employee on calendar.

How to get list of dates between two dates in Java,get list of dates between two dates in Java,list of dates between two dates in Java,find list of dates between two dates in Java,dates between two dates in Java,between two dates in Java,two dates in Java,

Here i need all the dates between my starting leave date and last leave date.
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class Testing {

 /**
  * @param args
  */
 public static void main(String[] args) {
  // TODO Auto-generated method stub
  List<Date> dates = new ArrayList<Date>();

  String str_date ="27/08/2010";
  String end_date ="02/09/2010";

  DateFormat formatter ; 

  formatter = new SimpleDateFormat("dd/MM/yyyy");
  Date startDate = null;
  try {
   startDate = (Date)formatter.parse(str_date);
  } catch (ParseException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  } 
  Date endDate = null;
  try {
   endDate = (Date)formatter.parse(end_date);
  } catch (ParseException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
  long interval = 24*1000 * 60 * 60; // 1 hour in millis
  long endTime =endDate.getTime() ; // create your endtime here, possibly using Calendar or Date
  long curTime = startDate.getTime();
  while (curTime <= endTime) {
      dates.add(new Date(curTime));
      curTime += interval;
  }
  for(int i=0;i<dates.size();i++){
      Date lDate =(Date)dates.get(i);
      String ds = formatter.format(lDate);    
      System.out.println(" Date is ..." + ds);
  }

 }

}

Output :
Date is ...27/08/2010
Date is ...28/08/2010
Date is ...29/08/2010
Date is ...30/08/2010
Date is ...31/08/2010
Date is ...01/09/2010
Date is ...02/09/2010

Tiny Calculator in JSF

After making Hello World program in my previous tutorial. Now i am telling you how to make a simple calculator using JSF framework.
Now open the index.xhtml file and paste below code :
<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://java.sun.com/jsf/html">
    <h:head>
        <title>Tiny calculator</title>
    </h:head>
    <h:body>
        <h1>Tiny calculator</h1>
        <h:form>
            <h:outputLabel value="Param 1:"/>
            <h:inputText id="param1"/>
            <br/>
            <h:outputLabel value="Param 2:"/>
            <h:inputText id="param2"/>
            <br/>
            <h:commandButton value="add"/>
            <h:commandButton value="substract"/>
            <h:commandButton value="multiply"/>
            <h:commandButton value="divide"/>
            <br/>
            <h:outputLabel value="Result:"/>
            <h:outputText id="result"/>
        </h:form>
    </h:body>
</html>
Now you'll  run this program it show as below image :

Now you need to make Manage Bean right click on project (TinyCalculator) and select New and Other.
You'll see like

Select JavaServer Faces then JSF Managed Bean and Next.
As class name use "CalculatorBean". Further down in the form you’ll find it again as name, with lower char starting.

For "Package" choose an appropriate name. e.g."calculator". An interesting configuration may be done at "Scope". With this, you’ll decide whether this bean will be used as an object for a single request, a session or the whole application. Just keep "request" and finish the wizard by [Finish]. NetBeans now creates a new class and some entries in the configuraion files. Alternativly you can create a pure Java class and edit the configuration manually.
Project directory look like :
Tiny Calculator in JSF,JSF framework,jsf tutorials,jsf first program,jsf demo example,jsf demo project

CalculatorBean.java
package calculator;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;

/**
 *
 * @author Mukesh
 */
@ManagedBean
@RequestScoped
public class CalculatorBean {

    private int _param1;
    private int _param2;
    private String _result;
    
    /**
     * Creates a new instance of CalculatorBean
     */
    public CalculatorBean() {
    }
    
    public int getParam1() {
        return _param1;
    }

    public void setParam1(int param1) {
        _param1 = param1;
    }

    public int getParam2() {
        return _param2;
    }

    public void setParam2(int param2) {
        _param2 = param2;
    }

    public String getResult() {
        return _result;
    }
    // 

    public String add(){
        _result = Integer.toString(_param1 + _param2);
        return "";
    }

    public String substract(){
        _result = Integer.toString(_param1 - _param2);
        return "";
    }

    public String multiply(){
        _result = Integer.toString(_param1 * _param2);
        return "";
    }

    public String divide(){
        _result = _param2 == 0 ? "n/a" : Double.toString(_param1 / (double)_param2);
        return "";
    }
}

Now some changes in index.xhtml page.
<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://java.sun.com/jsf/html">
    <h:head >
        <title>Tiny calculator</title>
    </h:head>
    <h:body>
        <h1>Tiny calculator</h1>
        <h:form id="calculator">
          <h:outputLabel for="param1" value="Param 1:"/>
          <h:inputText id="param1" value="#{calculatorBean.param1}"/>
          <br/>
          <h:outputLabel for="param2" value="Param 2:"/>
          <h:inputText id="param2" value="#{calculatorBean.param2}"/>
          <br/>
          <h:commandButton action="#{calculatorBean.add}" value="add"/>
          <h:commandButton action="#{calculatorBean.substract}" value="substract"/>
          <h:commandButton action="#{calculatorBean.multiply}" value="multiply"/>
          <h:commandButton action="#{calculatorBean.divide}" value="divide"/>
          <br/>
          <h:outputLabel for="result" value="Result:"/>
          <h:outputText id="result" value="#{calculatorBean.result}"/>
        </h:form>
    </h:body>
</html>
Now run your program it work well and your first jsf web application is completed

Hello World in JSF 2

Hello friends i am giving you a very basic tutorial on JSF which will give you some basic idea about JSF framework.

How its work and make program using that. I prefer to use Netbeans but you can use any IDE for this.
First of all make a new project of JSF framework by doing below steps:
  • After starting NetBeans, choose a new project (menu file, new project or simply be pressing {Ctrl}+{Shift}+{n}). The dialog shown below appears. 
  • Please choose "Java Web" and then"Web Application". 
  • Now click onto [Next]. In the following screen, please choose an appropriate project name. Here I used "TinyCalculator". If you like to store your project in a different directory than default, change the "Project Location". Then click [Next].
  • Select your server i use Tomcat server.
  • Now select framework in checkbox (JavaServer Faces).
  • Now click next and finish.
  • Your HelloWorld JSF program make. It'll show after run.
JSF tutorials,Hello World in JSF 2,first example of jsf,first tutorial on jsf,jsf example




Now its time to go ahead and do more with JSF
Check this : Tiny Calculator in JSF