Code.GeekInterview.com
 
 

Java Code Base

 
Code Samples Java
 

Date using Regular Expression


Code ResourceAuthor: srinivasaraobora  

Difficulty Level: Expert

Published: 1st Oct 2010   Read: 4210 times  

Filed in: Java
Add Comment


 

 

Sponsored Links


 

 
This Program Demonstrates use of regular expression with Date Formate.
 


Sample Code
  1. /*
  2.  * File: DateMatcher.java
  3.  * Author : Srinivas Bora
  4.  * Created Date:17/09/2010
  5.  * Description: Program for Date using Regular Expression.
  6.  *
  7. */
  8. import java.util.regex.Matcher;
  9. import java.util.regex.Pattern;
  10. import java.text.DateFormat;
  11. import java.text.SimpleDateFormat;
  12. import java.util.Date;
  13. public class DateMatcher
  14. {
  15.   public DateMatcher()
  16.   {
  17.    
  18.   }
  19.  
  20.   public static void main(String[] args)
  21.   {
  22.     DateFormat df = new SimpleDateFormat("MM/dd/yyyy");
  23.        Date d=new Date();      
  24.        String reportDate = df.format(d);
  25.        System.out.println(reportDate);
  26.    
  27.     Pattern datePattern = Pattern.compile("(d{2})/(d{2})/(d{4})");
  28.     Matcher dateMatcher = datePattern.matcher(reportDate);
  29.     if (dateMatcher.find())
  30.     {
  31.       System.out.println("Month is: " + dateMatcher.group(1));
  32.       System.out.println("Day is:   " + dateMatcher.group(2));
  33.       System.out.println("Year is:  " + dateMatcher.group(3));
  34.     }
  35.   }
  36. }
  37.  
Copyright GeekInterview.com


Next Article: Unmarshalling


 

Latest Code Samples

 

Popular Code Samples

 

Related Code Samples

 

Post Your Comment:

Members Please Login
Your Name:*
e-mail ID:(required for notification)*
Image Verification: 
 
 Subscribe    



Popular Coders

# Coder NameHits
1. srinivasaraobora141329
2. iamdvr103090
3. venki_madesh35438
4. Raju20218
5. parmod kumar duhan16431
6. Kiran.jakkaraju15174
7. chowsys7800
8. ashish.cns6903
9. Venkateswara Rao6813
10. venkat_kiran5932

Active Coders

Refined Tags