我写了下面的代码

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

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

28-Dec-2011

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


当前回答

Calendar c = Calendar.getInstance();
int day = c.get(Calendar.DAY_OF_MONTH);
int month = c.get(Calendar.MONTH);
int year = c.get(Calendar.YEAR);
String date = day + "/" + (month + 1) + "/" + year;

Log.i("TAG", "--->" + date);

其他回答

此方法可用于从系统中获取当前日期。

public static String getCurrentDateAndTime(){
    Date c = Calendar.getInstance().getTime();
    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy/MM/dd");
    String formattedDate = simpleDateFormat.format(c);
    return formattedDate;
}
 public String giveDate() {
    Calendar cal = Calendar.getInstance();
    SimpleDateFormat sdf = new SimpleDateFormat("EEE, MMM d, yyyy");
    return sdf.format(cal.getTime());
 }

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

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

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

Calendar cal = Calendar.getInstance();
cal.getTime().toString();
LocalDateTime ldt2 = LocalDateTime.now();
String year = ldt2.getYear()+"";
String month = ldt2.getMonthValue()+"";
String date = ldt2.getDayOfMonth()+"";
String hour = ldt2.getHour()+"";
String minute = ldt2.getMinute()+"";
String secs = ldt2.getSecond()+"";