我写了下面的代码
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。
当前回答
我用CalendarView写了一个日历应用程序,这是我的代码:
CalendarView cal = (CalendarView) findViewById(R.id.calendar);
cal.setDate(new Date().getTime());
calendar字段是我的CalendarView。 进口:
import android.widget.CalendarView;
import java.util.Date;
我得到了当前的日期,没有错误。
其他回答
Calendar c = Calendar.getInstance();
int day = c.get(Calendar.DAY_OF_MONTH);
int month = c.get(Calendar.MONTH);
int year = c.get(Calendar.YEAR);
String date = day + "/" + (month + 1) + "/" + year;
Log.i("TAG", "--->" + date);
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())
只需一行代码获得简单的日期格式:
SimpleDateFormat.getDateInstance().format(Date())
产出:2020年5月18日
SimpleDateFormat.getDateTimeInstance().format(Date())
上午11:00:39
SimpleDateFormat.getTimeInstance().format(Date())
输出:11:00:39 AM
希望这个答案足以得到这个日期和时间格式…:)
在当前地区(设备地区!)中获取当前日期的最简单方法:
String currentDate = DateFormat.getDateInstance().format(Calendar.getInstance().getTime());
如果你想拥有不同风格的日期,请使用getDateInstance(int style):
DateFormat.getDateInstance(DateFormat.FULL).format(Calendar.getInstance().getTime());
其他样式:DateFormat。长,DateFormat。DATE_FIELD DateFormat。DAY_OF_YEAR_FIELD等(使用CTRL+空格可以看到所有这些)
如果你也需要时间:
String currentDateTime = DateFormat.getDateTimeInstance(DateFormat.DEFAULT,DateFormat.LONG).format(Calendar.getInstance().getTime());
CharSequence s = DateFormat.getDateInstance().format("MMMM d, yyyy ");
首先需要一个实例