我写了下面的代码

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

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

28-Dec-2011

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


当前回答

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

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

calendar字段是我的CalendarView。 进口:

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

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

其他回答

这与android无关,因为它是基于java的,所以你可以使用

private String getDateTime() { 
   DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
   Date date = new Date(); 
   return dateFormat.format(date); 
}
CharSequence s  = DateFormat.getDateInstance().format("MMMM d, yyyy ");

首先需要一个实例

在Kotlin

https://www.programiz.com/kotlin-programming/examples/current-date-time

fun main(args: Array<String>) {

val current = LocalDateTime.now()

val formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSS")
val formatted = current.format(formatter)

println("Current Date and Time is: $formatted")}
SimpleDateFormat df = new SimpleDateFormat("dd-MMM-yyyy");
String date = df.format(Calendar.getInstance().getTime());

试试这个链接的代码,这是绝对正确的答案,所有的情况下,所有的日期和时间。或根据应用程序的需要和要求自定义日期和时间。

试试这个链接。试试这个链接