我写了下面的代码

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

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

28-Dec-2011

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


当前回答

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

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

其他回答

如果你只想获取日期,就像这样输入代码

Calendar.getInstance().getTime();

我用CalendarView写了一个日历应用程序,这是我的代码:

CalendarView cal = (CalendarView) findViewById(R.id.calendar);
cal.setDate(new Date().getTime());

calendar字段是我的CalendarView。 进口:

import android.widget.CalendarView;
import java.util.Date;

我得到了当前的日期,没有错误。

它是简单的一行代码,用于以yyyy-MM-dd格式获取当前日期 你可以使用你想要的格式:

String date = new SimpleDateFormat("yyyy-MM-dd", Locale.getDefault()).format(new Date());
SimpleDateFormat df = new SimpleDateFormat("dd-MMM-yyyy");
String date = df.format(Calendar.getInstance().getTime());

这是我使用的代码:

             Date date = new Date();  // to get the date
             SimpleDateFormat df = new SimpleDateFormat("dd-MM-yyyy"); // getting date in this format
             String formattedDate = df.format(date.getTime());
             text.setText(formattedDate);