Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 577
Chatar Veer Suthar Avatar asked Dec 28 '11 10:12

Chatar Veer Suthar


People also ask

How can I get current date code in Android?

Date c = Calendar. getInstance(). getTime(); System. out.

How do I display current date and time?

The Clock. systemUTC(). instant() method returns the current date and time both.

Which view is used to display the date and time in Android application?

text. DateFormat. getDateTimeInstance(). format(new Date()); // textView is the TextView view that should display it textView.


2 Answers

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);  

Update:

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

Final Solution:

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); 
like image 66
Paresh Mayani Avatar answered Oct 06 '22 01:10

Paresh Mayani


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()); 
like image 45
Pratik Butani Avatar answered Oct 06 '22 02:10

Pratik Butani