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.
Date c = Calendar. getInstance(). getTime(); System. out.
The Clock. systemUTC(). instant() method returns the current date and time both.
text. DateFormat. getDateTimeInstance(). format(new Date()); // textView is the TextView view that should display it textView.
You can use the SimpleDateFormat
class for formatting date in your desired format.
Just check this link where you get an 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);
The detailed example is here, I would suggest you go through this example and understand the concept of SimpleDateFormat class.
Date c = Calendar.getInstance().getTime(); System.out.println("Current time => " + c); SimpleDateFormat df = new SimpleDateFormat("dd-MMM-yyyy", Locale.getDefault()); String formattedDate = df.format(c);
Its simple one line code for get current Date in this yyyy-MM-dd format you can use your format that you want :
String date = new SimpleDateFormat("yyyy-MM-dd", Locale.getDefault()).format(new Date());
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With