Skip to main content

Current date and Time in different formats Using Calendar

Current date and time in different formats using Calendar function

       
            Calendar c=Calendar.getInstance();
         
            //for current date

            SimpleDateFormat sdf1=new SimpleDateFormat("dd-MM-yyyy");
            String date1 = sdf1.format(c.getTime());
            //value of date1 is 27-04-2012


            SimpleDateFormat sdf_db=new SimpleDateFormat("yyyy-MM-dd");
            String date_db=sdf_db.format(c.getTime());
            //value of date_db is 2012-04-27
      
            SimpleDateFormat sdf2=new SimpleDateFormat("yyyyMMdd");
            String date2=sdf2.format(c.getTime());
            //value of date2 is 20120427


            //for current time

            SimpleDateFormat stf1=new SimpleDateFormat("hh:mm a");
            String time1 = stf1.format(c.getTime());
            //value of time1 is 04:30 pm


            SimpleDateFormat stf2=new SimpleDateFormat("HH:mm:ss");
            string time2=stf2.format(c.getTime());
            //value of time2 is 16:30:10


http://sourcecodeandroid.blogspot.in by T.s.Mathavan

Comments

Unknown said…
thanks .. pls sent some examples... if u free ... i am searching for one day for this... thanks...Good Day...

Popular posts from this blog

Email pattern validation

//Email pattern validation In this example the email is validated and returns True or False package com.***; import java.util.regex.Matcher; import java.util.regex.Pattern; import android.util.Log; public class EmailValidator {            private Pattern pattern;            private Matcher matcher;            private static final String EMAIL_PATTERN = "^[_A-Za-z0-9-]+(\\.[_A-Za-z0-9-]+)*@[A-Za-z0-9]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$";                public Boolean validate(String email)            {                          pattern = Pattern.compile(EMAIL_PATTERN);                          matcher = pattern.matcher(email);         ...