I wrote the following code
Date d = new Date();
CharSequence s = DateFormat.format(\"MMMM d, yyyy \", d.getTime());
But is asking me para
A simple tweak to Paresh's solution:
Date date = Calendar.getInstance().getTime();
SimpleDateFormat df = new SimpleDateFormat("dd-MMM-yyyy");
String formattedDate = df.format(date);
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);
}
just one line code to get simple Date format :
SimpleDateFormat.getDateInstance().format(Date())
output : 18-May-2020
SimpleDateFormat.getDateTimeInstance().format(Date())
output : 18-May-2020 11:00:39 AM
SimpleDateFormat.getTimeInstance().format(Date())
output : 11:00:39 AM
Hope this answer is enough to get this Date and Time Format ... :)
try this,
SimpleDateFormat timeStampFormat = new SimpleDateFormat("yyyyMMddHHmmssSS");
Date myDate = new Date();
String filename = timeStampFormat.format(myDate);
Calendar cal = Calendar.getInstance();
Calendar dt = Calendar.getInstance();
dt.clear();
dt.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH),cal.get(Calendar.DATE));
return dt.getTime();
The below code displays the both time and date
Calendar cal = Calendar.getInstance();
cal.getTime().toString();