我写了下面的代码

Date d = new Date();
CharSequence s  = DateFormat.format("MMMM d, yyyy ", d.getTime());

我想要当前日期的字符串格式,比如

28-Dec-2011

这样我就可以把它设置为TextView。


当前回答

Date c = Calendar.getInstance().getTime();
System.out.println("Current time => " + c);

SimpleDateFormat df = new SimpleDateFormat("dd-MMM-yyyy");
String formattedDate = df.format(c);

这是最好的答案……

其他回答

我尝试过这种方法,对我很有效。

val df = SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ", Locale.getDefault()) // pass the format pattern that you like and done.
println(df.format(Date()))

在当前地区(设备地区!)中获取当前日期的最简单方法:

String currentDate = DateFormat.getDateInstance().format(Calendar.getInstance().getTime());

如果你想拥有不同风格的日期,请使用getDateInstance(int style):

DateFormat.getDateInstance(DateFormat.FULL).format(Calendar.getInstance().getTime());

其他样式:DateFormat。长,DateFormat。DATE_FIELD DateFormat。DAY_OF_YEAR_FIELD等(使用CTRL+空格可以看到所有这些)

如果你也需要时间:

String currentDateTime = DateFormat.getDateTimeInstance(DateFormat.DEFAULT,DateFormat.LONG).format(Calendar.getInstance().getTime());

您可以使用以下代码获得所需格式的日期。

String date = String.valueOf(android.text.format.DateFormat.format("dd-MM-yyyy", new java.util.Date()));

我已经用过这个了:

Date What_Is_Today=Calendar.getInstance().getTime();
SimpleDateFormat Dateformat = new SimpleDateFormat("dd-MM-yyyy");
String Today=Dateformatf.format(What_Is_Today);

Toast.makeText(this,Today,Toast.LENGTH_LONG).show();

首先我得到时间,然后我声明了一个简单的日期格式(以获得日期:19-6-2018) 然后我使用format将日期更改为字符串。

试试这个,

SimpleDateFormat timeStampFormat = new SimpleDateFormat("yyyyMMddHHmmssSS");
Date myDate = new Date();
String filename = timeStampFormat.format(myDate);