我写了下面的代码

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

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

28-Dec-2011

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


当前回答

这是我使用的代码:

final Calendar c = Calendar.getInstance();
year = c.get(Calendar.YEAR);
month = c.get(Calendar.MONTH);
day = c.get(Calendar.DAY_OF_MONTH);

// Set current date into textview
tvDisplayDate.setText(new StringBuilder()
    .append(month + 1).append("-") // Month is 0 based, add 1
    .append(day).append("-")
    .append(year).append("   Today is :" + thursday ) );

// Set current date into datepicker
dpResult.init(year, month, day, null);

其他回答

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

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

下面的代码显示了时间和日期

Calendar cal = Calendar.getInstance();
cal.getTime().toString();

工作就像一个魅力和转换为字符串作为奖励;)

SimpleDateFormat currentDate = new SimpleDateFormat("dd/MM/yyyy");
      Date todayDate = new Date();
    String thisDate = currentDate.format(todayDate);

只需一行代码获得简单的日期格式:

SimpleDateFormat.getDateInstance().format(Date())

产出:2020年5月18日

SimpleDateFormat.getDateTimeInstance().format(Date())

上午11:00:39

SimpleDateFormat.getTimeInstance().format(Date())

输出:11:00:39 AM

希望这个答案足以得到这个日期和时间格式…:)

 String date = new SimpleDateFormat("yyyy-MM-dd").format(new Date());

//导入Date类为java.util