Saturday 7 September 2013

Working with Date class in java,Converting String to Date


import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

//This examples are used to take date in string format and stored in date object
public class DateExamples {


public static void main(String[] args) {

System.out.println("+++++++++++++++++++++++++++++++++");
//Just displaying today date in required format
DateFormat df = new SimpleDateFormat("dd/yy/MM");
String formattedDate = df.format(new Date());
System.out.println("TO DAY DATE IS:"+formattedDate);
System.out.println("+++++++++++++++++++++++++++++++++");
System.out.println("+++++++++++++++++++++++++++++++++");
//In this format only we have to give the date
SimpleDateFormat formatter = new SimpleDateFormat
("dd-MMM-yyyy");
String dateInString = "7-Jun-2013";

try {

Date date = formatter.parse(dateInString);
System.out.println(date);
System.out.println(formatter.format(date));

} catch (ParseException e) {
e.printStackTrace();
}
System.out.println("+++++++++++++++++++++++++++++++++");

System.out.println("+++++++++++++++++++++++++++++++++");
//In this format only we have to give the date
SimpleDateFormat formatter1 = new SimpleDateFormat
("dd/MM/yyyy");
String dateInString1 = "07/06/2013";

try {

Date date = formatter1.parse(dateInString1);
System.out.println(date);
System.out.println(formatter1.format(date));

} catch (ParseException e) {
e.printStackTrace();
}
System.out.println("+++++++++++++++++++++++++++++++++");

System.out.println("+++++++++++++++++++++++++++++++++");
//In this format only we have to give the date
SimpleDateFormat formatter2 = new SimpleDateFormat
("MMM dd, yyyy");
String dateInString2 = "Jun 7, 2013";

try {

Date date = formatter2.parse(dateInString2);
System.out.println(date);
System.out.println(formatter2.format(date));

} catch (ParseException e) {
e.printStackTrace();
}
System.out.println("+++++++++++++++++++++++++++++++++");

System.out.println("+++++++++++++++++++++++++++++++++");
//In this format only we have to give the date
SimpleDateFormat formatter3 = new SimpleDateFormat
("E, MMM dd yyyy");
String dateInString3 = "Fri, June 7 2013";

try {

Date date = formatter3.parse(dateInString3);
System.out.println(date);
System.out.println(formatter3.format(date));

} catch (ParseException e) {
e.printStackTrace();
}
System.out.println("+++++++++++++++++++++++++++++++++");


System.out.println("+++++++++++++++++++++++++++++++++");
//In this format only we have to give the date
SimpleDateFormat formatter4 = new SimpleDateFormat
("EEEE, MMM dd, yyyy HH:mm:ss a");
String dateInString4 = "Friday, Jun 7, 2013 12:10:56 PM";

try {

Date date = formatter4.parse(dateInString4);
System.out.println(date);
System.out.println(formatter4.format(date));

} catch (ParseException e) {
e.printStackTrace();
}
System.out.println("+++++++++++++++++++++++++++++++++");

System.out.println("+++++++++++++++++++++++++++++++++");
//In this format only we have to give the time
SimpleDateFormat formatter5 = new SimpleDateFormat
("HH:mm:ss a");
String dateInString5 = "12:10:56 PM";

try {

Date date = formatter5.parse(dateInString5);
//System.out.println(date);
System.out.println(formatter5.format(date));

} catch (ParseException e) {
e.printStackTrace();
}
System.out.println("+++++++++++++++++++++++++++++++++");

System.out.println("+++++++++++++++++++++++++++++++++");
//It just displaying current date and time in required format
SimpleDateFormat formatter6 = new SimpleDateFormat
("yyyy/MM/dd EEE,hh:mm::ss a");
System.out.println(formatter6.format(new Date()));

System.out.println("+++++++++++++++++++++++++++++++++");

System.out.println("+++++++++++++++++++++++++++++++++");
//It just displaying current date and time in required format
SimpleDateFormat formatter7 = new SimpleDateFormat
("yyyy/MMMM/dd EEE,hh:mm::ss a");
System.out.println(formatter7.format(new Date()));

System.out.println("+++++++++++++++++++++++++++++++++");


}

}
OUTPUT:
+++++++++++++++++++++++++++++++++
TO DAY DATE IS:07/13/09
+++++++++++++++++++++++++++++++++
+++++++++++++++++++++++++++++++++
Fri Jun 07 00:00:00 IST 2013
07-Jun-2013
+++++++++++++++++++++++++++++++++
+++++++++++++++++++++++++++++++++
Fri Jun 07 00:00:00 IST 2013
07/06/2013
+++++++++++++++++++++++++++++++++
+++++++++++++++++++++++++++++++++
Fri Jun 07 00:00:00 IST 2013
Jun 07, 2013
+++++++++++++++++++++++++++++++++
+++++++++++++++++++++++++++++++++
Fri Jun 07 00:00:00 IST 2013
Fri, Jun 07 2013
+++++++++++++++++++++++++++++++++
+++++++++++++++++++++++++++++++++
Fri Jun 07 12:10:56 IST 2013
Friday, Jun 07, 2013 12:10:56 PM
+++++++++++++++++++++++++++++++++
+++++++++++++++++++++++++++++++++
12:10:56 PM
++++++++++++++++++++++++++++++++++
++++++++++++++++++++++++++++++++++
2013/09/07 Sat,03:33::32 PM
+++++++++++++++++++++++++++++++++
+++++++++++++++++++++++++++++++++
2013/September/07 Sat,03:33::32 PM
+++++++++++++++++++++++++++++++++

Spring MVC working Flow

  • By using URL/From jsp we are sending request

  • This request will come to web.xml file and execute DispatcherServlet

  • It is the responsibility of DispatcherServlet to identify perticular HandlerMappings

  • By using HandlerMapping DispathcerServlet will search for requested url path matching in spring configuration file/Spring Configuration class

  • If it is available,DispatcherServlet will call methods in our Controller

  • Controller process the request,get the data form form,store into pojo class/javabean,if required perform validations

  • And then Controller returns ModelAndView object with data/errors and jsp name

  • DispatcherServlet take the ModleAndView object and stored in scoped variables

  • Based on the jsp name returned by Controller,DispatcherServlet will find out jsp by using ViewResolver(InternalResourceViewResolver)

  • Finally loginpage/successpage/failure page send to clietnt/jsp/other

  • By using scoped variables we can display the data/errors