我正在开发一个需要设备当前日期的应用程序,但它只需要星期几,月份和日期,例如Friday October 14
我在日历中尝试过这段代码。如何将Date转换为String?这可能以这种格式获取日期吗?
Calendar c = Calendar.getInstance(TimeZone.getTimeZone("GMT"));
System.out.println("Date" + c.getTime());
and my output : `Fri Oct 14 16:17:03 Asia/Calcutta 2011`
答案 0 :(得分:2)
您可以使用SimpleDateFormat
:
SimpleDateFormat fmt = new SimpleDateFormat("EEEE MMMM dd");
Calendar c = Calendar.getInstance(TimeZone.getTimeZone("GMT"));
System.out.print("Date ");
System.out.println(fmt.format(c.getTime()));
如果你需要不同字符串中的那些值,它会变得更复杂一些(使用DateFormatSymbols
来获取月/工作日名称):
Calendar c = Calendar.getInstance(TimeZone.getTimeZone("GMT"));
DateFormatSymbols dateSymbols = new DateFormatSymbols();
String[] monthsText = dateSymbols.getMonths();
String[] weekdaysText = dateSymbols.getWeekdays();
String day = String.valueOf(c.get(Calendar.DAY_OF_MONTH));
String month = monthsText[c.get(Calendar.MONTH)];
String weekday = weekdaysText[c.get(Calendar.DAY_OF_WEEK)];
System.out.format("day: %s, month: %s, weekday: %s", day, month, weekday);