我写了下面的代码

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

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

28-Dec-2011

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


当前回答

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

public static String getCurrentDateAndTime(){
    Date c = Calendar.getInstance().getTime();
    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy/MM/dd");
    String formattedDate = simpleDateFormat.format(c);
    return formattedDate;
}

其他回答

对帕雷什的解决方案做一个简单的调整:

Date date = Calendar.getInstance().getTime();
SimpleDateFormat df = new SimpleDateFormat("dd-MMM-yyyy");
String formattedDate = df.format(date);
Date c = Calendar.getInstance().getTime();
System.out.println("Current time => " + c);

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

这是最好的答案……

 public static String getcurrentDateAndTime(){

        Date c = Calendar.getInstance().getTime();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy/MM/dd");
        String formattedDate = simpleDateFormat.format(c);
        return formattedDate;
    }

// String currentdate=  getcurrentDateAndTime();

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

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

calendar字段是我的CalendarView。 进口:

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

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

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

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