我写了下面的代码
Date d = new Date();
CharSequence s = DateFormat.format("MMMM d, yyyy ", d.getTime());
我想要当前日期的字符串格式,比如
28-Dec-2011
这样我就可以把它设置为TextView。
我写了下面的代码
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 = 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);
它是简单的一行代码,用于以yyyy-MM-dd格式获取当前日期 你可以使用你想要的格式:
String date = new SimpleDateFormat("yyyy-MM-dd", Locale.getDefault()).format(new Date());
CharSequence s = DateFormat.getDateInstance().format("MMMM d, yyyy ");
首先需要一个实例
In Kotlin you can use this code : -
Simple only need to change date format to this "dd-MM-yyyy"
val d = Date()
val str: CharSequence = DateFormat.format("dd-MM-yyyy", d.getTime())
Log.e("", str.toString())
In Java You use this code: -
Date date = new Date();
CharSequence str = DateFormat.format("dd-MM-yyyy", date.getTime());
Log.e("Date", str.toString())
您可以使用以下代码获得所需格式的日期。
String date = String.valueOf(android.text.format.DateFormat.format("dd-MM-yyyy", new java.util.Date()));