我写了下面的代码
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。
当前回答
如果你只想获取日期,就像这样输入代码
Calendar.getInstance().getTime();
其他回答
这与android无关,因为它是基于java的,所以你可以使用
private String getDateTime() {
DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
Date date = new Date();
return dateFormat.format(date);
}
此方法可用于从系统中获取当前日期。
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);
CharSequence s = DateFormat.getDateInstance().format("MMMM d, yyyy ");
首先需要一个实例
LocalDateTime ldt2 = LocalDateTime.now();
String year = ldt2.getYear()+"";
String month = ldt2.getMonthValue()+"";
String date = ldt2.getDayOfMonth()+"";
String hour = ldt2.getHour()+"";
String minute = ldt2.getMinute()+"";
String secs = ldt2.getSecond()+"";