Saturday 5 April 2014

Date Formating and Parsing in Java

Parsing of Date from one format to another format is one of the precious thing. In Java, We can able to parse  dates from strings, and format dates to strings, using a Helper Class of java.text package and that is java.text.SimpleDateFormat class.

Some Important Notation of Date Formating and  Parsing:
y   = year   (yy or yyyy)
M   = month  (MM)
d   = day in month (dd)
h   = hour (0-12)  (hh)
H   = hour (0-23)  (HH)
m   = minute in hour (mm)
s   = seconds (ss)
S   = milliseconds (SSS)
z   = time zone  text        (e.g. Pacific Standard Time...)
Z   = time zone, time offset (e.g. -0800)
Some Important Date Pattern that we need to achieve as per our requirement.
yyyy-MM-dd           (2009-12-31)

dd-MM-YYYY           (31-12-2009)
    
yyyy-MM-dd HH:mm:ss  (2009-12-31 23:59:59)

HH:mm:ss.SSS         (23:59.59.999)

yyyy-MM-dd HH:mm:ss.SSS   (2009-12-31 23:59:59.999)

yyyy-MM-dd HH:mm:ss.SSS Z   (2009-12-31 23:59:59.999 +0100)   

Code How can we format the Date Pattern into another Date Pattern:
Simple Code to Format the Date into other Pattern : 

  SimpleDateFormat sdf1 = new SimpleDateFormat("dd-MM-yyyy");
   System.out.println(sdf1.format(new Date()));//06-04-2014
     
   SimpleDateFormat sdf2 = new SimpleDateFormat("dd-MMM-yyyy");
   System.out.println(sdf2.format(new Date()));//06-Apr-2014
    
   SimpleDateFormat sdf3 = new SimpleDateFormat("yyyy-MM-dd");
   System.out.println(sdf3.format(new Date()));//2014-04-06
      
   SimpleDateFormat sdf4 =
                    new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
   System.out.println(sdf4.format(new Date()));
                     //2014-04-06 11:48:36
      
  SimpleDateFormat sdf5 =
               new SimpleDateFormat("yyyy-MM-dd hh:mm:ss:SSS z");
  System.out.println(sdf5.format(new Date()));
                 //2014-04-06 11:49:58:775 IST
    
  SimpleDateFormat sdf6 =
             new SimpleDateFormat("yyyy-MM-dd hh:mm:ss:SSS z Z");
  System.out.println(sdf6.format(new Date()));
              //2014-04-06 11:50:39:297 IST +0530

Simple Code to parse the Date into other Pattern : 

          
  SimpleDateFormat sdf5 = new SimpleDateFormat("yyyy-MM-dd");
   try {
    //Input is : 1991-03-24
    System.out.println(sdf5.parseObject(new Scanner(System.in).next()));
//Sun Mar 24 00:00:00 IST 1991
   } catch (ParseException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   }
Convert java.util.Date to java.sql.Date and  java.sql.Date to java.util.Date : 

java.sql.Date to java.util.Date

 java.sql.Date sd = new java.sql.Date(System.currentTimeMillis());
 java.util.Date ud = new java.util.Date(sd.getTime());
 System.out.println(sd);//2014-04-06

java.util.Date to java.sql.Date:

  java.util.Date ud = new java.util.Date();
  java.sql.Date sd = new java.sql.Date(ud.getTime());
  System.out.println(sd);//2014-04-06

Like that , so many third party utility classes are there where we can convert one pattern to another.My request to all of you, while implementing the third party utility class please make a POC and test it in every aspect.

Thanks

No comments:

Post a Comment