我写了下面的代码

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

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

28-Dec-2011

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


当前回答

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

//导入Date类为java.util

其他回答

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

//导入Date类为java.util

CharSequence s  = DateFormat.getDateInstance().format("MMMM d, yyyy ");

首先需要一个实例

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

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

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

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

calendar字段是我的CalendarView。 进口:

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

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

这是我使用的代码:

             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);