【发布时间】:2015-08-03 09:52:33
【问题描述】:
假设我们有一个日期显示为。
2015-08-03 12:00:00
如何将其转换为一天的名称,例如 Tuesday ?我不想要03 Tue 之类的东西。只需要完整的days 名称。我环顾四周,但对此我有点困惑。
【问题讨论】:
-
这可能对你有帮助 :>) stackoverflow.com/questions/20907809/…
假设我们有一个日期显示为。
2015-08-03 12:00:00
如何将其转换为一天的名称,例如 Tuesday ?我不想要03 Tue 之类的东西。只需要完整的days 名称。我环顾四周,但对此我有点困惑。
【问题讨论】:
首先,将该日期解析为 java.util.Date 对象。
DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date yourDate = formatter.parse("2015-08-03 12:00:00");
然后,使用此日期填充 Calendar:
Calendar c = Calendar.getInstance();
c.setTime(yourDate);
int dayOfWeek = c.get(Calendar.DAY_OF_WEEK);
现在你有你的星期几dayOfWeek(例如,1 是星期天)。
【讨论】:
SimpleDateFormat simpleDateformat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date now = simpleDateformat.parse("2015-08-03 12:00:00");
simpleDateformat = new SimpleDateFormat("EEEE"); // the day of the week spelled out completely
System.out.println(simpleDateformat.format(now));
【讨论】:
这是我想出的解决方案:
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd
HH:mm:ss");
Date weekDay = null;
try {
weekDay = formatter.parse("2015-08-03 12:00:00");
} catch (ParseException e) {
e.printStackTrace();
}
SimpleDateFormat outFormat = new SimpleDateFormat("EEEE");
String day = outFormat.format(weekDay);
【讨论】: