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