How can I get current date in Android?
public String giveDate() {
Calendar cal = Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat("EEE, MMM d, yyyy");
return sdf.format(cal.getTime());
}
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());
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);
This is nothing to do with android as it is java based so you could use
private String getDateTime() {
DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
Date date = new Date();
return dateFormat.format(date);
}