Write a program to sort string date with time in java

Here we used the Collections class to sort the list and wrote a comparator to compare string dates to impose an ordering on a collection of string dates.

Code:
 
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

public class SortDates {
 
 public static void main(String[] args) {
  
  List strDts1 =  new ArrayList();
  strDts1.add("25/01/2018 $ 02:17 PM");
  strDts1.add("23/02/2018 $ 03:32 PM");
  strDts1.add("23/02/2018 $ 11:47 AM");
  strDts1.add("25/01/2018 $ 09:34 PM");
  strDts1.add("23/02/2018 $ 07:18 AM");
  strDts1.add("23/02/2018 $ 04:45 AM");
  strDts1.add("25/01/2018 $ 08:29 PM");
  strDts1.add("25/01/2018 $ 07:55 AM");
  
  Collections.sort(strDts1, new Comparator() {
         DateFormat df = new SimpleDateFormat("dd/MM/yyyy '$' hh:mm a");
         @Override
         public int compare(String s1, String s2) {
             try {
                 return df.parse(s1).compareTo(df.parse(s2));
             } catch (ParseException e) {
                 throw new IllegalArgumentException(e);
             }
           }
         });
  
  System.out.println(strDts1);

  
  List strDts2 =  new ArrayList();
  strDts2.add("25/01/2018 # 02:17 PM");
  strDts2.add("23/02/2018 # 03:32 PM");
  strDts2.add("23/02/2018 # 11:47 AM");
  strDts2.add("25/01/2018 # 09:34 PM");
  strDts2.add("23/02/2018 # 07:18 AM");
  strDts2.add("23/02/2018 # 04:45 AM");
  strDts2.add("25/01/2018 # 08:29 PM");
  strDts2.add("25/01/2018 # 07:55 AM");
  
    
  //Java 8
  DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd/MM/yyyy '#' hh:mm a");
         Collections.sort(strDts2, (s1, s2) -> LocalDateTime.parse(s1, formatter).
              compareTo(LocalDateTime.parse(s2, formatter)));
     
         System.out.println(strDts2);
 }

}
Output:
[25/01/2018 $ 07:55 AM, 25/01/2018 $ 02:17 PM, 25/01/2018 $ 08:29 PM, 25/01/2018 $ 09:34 PM, 23/02/2018 $ 04:45 AM, 23/02/2018 $ 07:18 AM, 23/02/2018 $ 11:47 AM, 23/02/2018 $ 03:32 PM]
[25/01/2018 # 07:55 AM, 25/01/2018 # 02:17 PM, 25/01/2018 # 08:29 PM, 25/01/2018 # 09:34 PM, 23/02/2018 # 04:45 AM, 23/02/2018 # 07:18 AM, 23/02/2018 # 11:47 AM, 23/02/2018 # 03:32 PM]

Write a program of Fibonacci series using Recursion in Java

Code:
 import java.util.Scanner;  
 public class FibonacciRecursion {  
      public static void main(String args[]) {  
           Scanner scanner = new Scanner(System.in);  
           System.out.println("Enter the number for Fibonacci series:");  
           int num = scanner.nextInt();  
           System.out.println("Fibonacci Series:");  
           for (int i=0;i<num;i++) {  
           System.out.print(fibonacci(i) + ",");  
           }  
           scanner.close();  
      }  
      public static int fibonacci(int num) {  
           if (num == 0) {  
                return 0;  
           } else if (num == 1) {  
                return 1;  
           } else {  
                return fibonacci(num-2) + fibonacci(num-1);  
           }  
      }  
 }  
Output :
Enter the number for Fibonacci series:
8
Fibonacci series:
0,1,1,2,3,5,8,13,

JSP declaration tag

A JSP declaration is used to declare variables and methods in a page’s scripting language.

The syntax for a declaration is as follows:
<%! scripting-language-declaration %>

Example for declaring Variable:
index.jsp
 <html>   
 <body>   
 <%! int a=5; %>   
 <%= "Value of Integer a is "+ (++a) %>   
 </body>   
 </html>   

Example for declaring method : 
index.jsp
 <HTML>  
 <BODY>  
 <%!  
   void getName()  
   {  
     System.out.println( "LoveJavaBytes" );  
   }  
 %>  
 Name is : <%= getName() %>  
 </BODY>  
 </HTML>  

JSP expression tag

A JSP expression is used to insert the value of a scripting language expression, converted into a string, into the data stream returned to the client using response object.

The syntax for an expression is as follows:
<%= scripting-language-expression %>

Example of JSP expression Tag :
On index.jsp, user enters name that send as request to user.jsp which prints username as output.

index.jsp
   <html>   
   <body>   
      <form action="user.jsp">   
        <input type="text" name="username">   
        <input type="submit" value="Submit"><br/>   
      </form>   
   </body>   
   </html>   

user.jsp
   <html>   
   <body>   
       <% ="Welcome! "+ request.getParameter("username") %>     
   </body>   
   </html>