Veer Suthar Veer Suthar - 1 month ago 7
Android Question

How can I get current date in Android?

I wrote the following code

Date d = new Date();
CharSequence s = DateFormat.format("MMMM d, yyyy ", d.getTime());


But is asking me parameter, I want current date in string format,

like

28-Dec-2011


so that I can set over
TextView
,

explain a bit, if you think something is necessary, I am new to Android Development.

Answer

You can use the SimpleDateFormat class for formatting date in your desired format.

Just check this link where you get idea for your example.

For example:

String dateStr = "04/05/2010"; 

SimpleDateFormat curFormater = new SimpleDateFormat("dd/MM/yyyy"); 
Date dateObj = curFormater.parse(dateStr); 
SimpleDateFormat postFormater = new SimpleDateFormat("MMMM dd, yyyy"); 

String newDateStr = postFormater.format(dateObj); 

Update:

Detailed example is here and here, I would suggest you to go through this example and understand the concept of SimpleDateFormat class.

Final Solution:

Calendar c = Calendar.getInstance();
System.out.println("Current time => " + c.getTime());

SimpleDateFormat df = new SimpleDateFormat("dd-MMM-yyyy");
String formattedDate = df.format(c.getTime());