我该怎么做?
Year 2019, Month July, Date 30
以这种格式并且仅从Calendar(java.util)对象中准确
private String getCurrentDate() {
Calendar calendar = Calendar.getInstance();
SimpleDateFormat mdformat = new SimpleDateFormat("yyyy-MM-dd");
return mdformat.format(calendar.getTime()); }
答案 0 :(得分:1)
如果您只想使用日历:
Calendar c = Calendar.getInstance();
String result = "Year " + c.get(Calendar.YEAR) +
", Month " + new SimpleDateFormat("MMMM").format(c.getTime()) +
", Date " + c.get(DAY_OF_MONTH);
System.out.println(result);
将打印:
Year 2019, Month June, Date 4
如果您甚至不想使用SimpleDateFormat
:
String[] months = {
"January", "February", "March", "April", "May", "June",
"July", "August", "September", "October", "November", "December"
};
Calendar c = Calendar.getInstance();
String result = "Year " + c.get(Calendar.YEAR) +
", Month " + months[c.get(Calendar.MONTH)] +
", Date " + c.get(DAY_OF_MONTH);
System.out.println(result);
还将打印:
Year 2019, Month June, Date 4
答案 1 :(得分:0)
文本可以包含在SDF字符串中,因此请使用您的代码:
SimpleDateFormat mdFormat = new SimpleDateFormat("'Year 'yyyy', Month 'MMMM', Date 'dd");
此答案的目的是表明您可以在日期格式中包含文本; SDF有一些微妙之处(例如LLLL对于语言支持可能更好),此处未解决。