How can I get current date in Android?

2019-01-04 18:48发布

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.

21条回答
别忘想泡老子
2楼-- · 2019-01-04 19:31
CharSequence s  = DateFormat.getDateInstance().format("MMMM d, yyyy ");

You need an instance first

查看更多
Emotional °昔
3楼-- · 2019-01-04 19:32

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); 
}
查看更多
太酷不给撩
4楼-- · 2019-01-04 19:32

try this,

SimpleDateFormat timeStampFormat = new SimpleDateFormat("yyyyMMddHHmmssSS");
Date myDate = new Date();
String filename = timeStampFormat.format(myDate);
查看更多
Luminary・发光体
5楼-- · 2019-01-04 19:37

You can use following code to get a date in the format you want.

String date = String.valueOf(android.text.format.DateFormat.format("dd-MM-yyyy", new java.util.Date()));
查看更多
爷、活的狠高调
6楼-- · 2019-01-04 19:39
 String date = new SimpleDateFormat("yyyy-MM-dd").format(new Date());

// import Date class as java.util

查看更多
戒情不戒烟
7楼-- · 2019-01-04 19:39
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();        
查看更多
登录 后发表回答