在Android中获取当前星期几的最简单方法是什么?
在Android中获取一周中最简单的方法是什么?
使用Java Calendar
类。
Calendar calendar = Calendar.getInstance(); int day = calendar.get(Calendar.DAY_OF_WEEK); switch (day) { case Calendar.SUNDAY: // Current day is Sunday case Calendar.MONDAY: // Current day is Monday case Calendar.TUESDAY: // etc. }
Calendar calendar = Calendar.getInstance(); Date date = calendar.getTime(); // 3 letter name form of the day System.out.println(new SimpleDateFormat("EE", Locale.ENGLISH).format(date.getTime())); // full name form of the day System.out.println(new SimpleDateFormat("EEEE", Locale.ENGLISH).format(date.getTime()));
结果(今天):
星期六 星期六
更新: java8
LocalDate date = LocalDate.now(); DayOfWeek dow = date.getDayOfWeek(); System.out.println("Enum = " + dow); String dayName = dow.getDisplayName(TextStyle.FULL, Locale.ENGLISH); System.out.println("FULL = " + dayName); dayName = dow.getDisplayName(TextStyle.FULL_STANDALONE, Locale.ENGLISH); System.out.println("FULL_STANDALONE = " + dayName); dayName = dow.getDisplayName(TextStyle.NARROW, Locale.ENGLISH); System.out.println("NARROW = " + dayName); dayName = dow.getDisplayName(TextStyle.NARROW_STANDALONE, Locale.ENGLISH); System.out.println("NARROW_STANDALONE = " + dayName); dayName = dow.getDisplayName(TextStyle.SHORT, Locale.ENGLISH); System.out.println("SHORT = " + dayName); dayName = dow.getDisplayName(TextStyle.SHORT_STANDALONE, Locale.ENGLISH); System.out.println("SHORT_STANDALONE = " + dayName);
结果(今天):
Enum = SATURDAY FULL = Saturday FULL_STANDALONE = Saturday NARROW = S NARROW_STANDALONE = 6 SHORT = Sat SHORT_STANDALONE = Sat
Calendar.getInstance().get(Calendar.DAY_OF_WEEK)
要么
new GregorianCalendar().get(Calendar.DAY_OF_WEEK);
就像在Java中一样,没有什么特别的Android。
public String weekdays[] = new DateFormatSymbols(Locale.ITALIAN).getWeekdays(); Calendar c = Calendar.getInstance(); Date date = new Date(); c.setTime(date); int dayOfWeek = c.get(Calendar.DAY_OF_WEEK); System.out.println(dayOfWeek); System.out.println(weekdays[dayOfWeek]);
如果你不想使用Calendar类,你可以使用它
String weekday_name = new SimpleDateFormat("EEEE", Locale.ENGLISH).format(System.currentTimeMillis());
即结果是,
"Sunday"
这是我获得当天的简单方法
public String getCurrentDay(){ String daysArray[] = {"Sunday","Monday","Tuesday", "Wednesday","Thursday","Friday", "Saturday"}; Calendar calendar = Calendar.getInstance(); int day = calendar.get(Calendar.DAY_OF_WEEK); return daysArray[day]; }
使用这两种方法,你会发现容易,如果你不会最后七天使用(currentdaynumber + 7-1)%7,(currentdaynumber + 7-2)%7 ….. upto 6
public static String getDayName(int day){ switch(day){ case 0: return "Sunday"; case 1: return "Monday"; case 2: return "Tuesday"; case 3: return "Wednesday"; case 4: return "Thursday"; case 5: return "Friday"; case 6: return "Saturday"; } return "Worng Day"; } public static String getCurrentDay(){ SimpleDateFormat dayFormat = new SimpleDateFormat("EEEE", Locale.US); Calendar calendar = Calendar.getInstance(); return dayFormat.format(calendar.getTime()); }
Java 8的datetime
API
使它变得如此简单:
LocalDate.now().getDayOfWeek().name()
将以String
返回给你一天的名字
输出:星期四
由于GregorianCalender类中的DAY_OF_WEEK是一个静态字段,因此您可以直接以默认方式访问它
int dayOfWeek = GregorianCalender.DAY_OF_WEEK;