我想在Java中查找日期的日期27-04-2011
。
我尝试使用它,但它不起作用:
Calendar cal = Calendar.getInstance();
int val = cal.get(Calendar.DAY_OF_WEEK);
它给出整数值,而不是我想要的String输出。我没有得到我想要的正确值。例如,它给了我4
的值28-02-2011
,它应该是2,因为星期日是第一个星期。
答案 0 :(得分:9)
是的,你已经问过一周中的哪一天 - 而且2月28日是Monday, day 2。请注意,在你给出的代码中,你实际上并没有在任何地方设置日期 - 它只是使用当前日期,即星期三,这就是你得到4的原因。如果你可以显示你是如何设置日历到另一个日期(例如2月28日),我们可以找出为什么这不适合你。
如果您希望将其格式化为文本,则可以使用SimpleDateFormat
和“E”说明符。例如(未经测试):
SimpleDateFormat formatter = new SimpleDateFormat("EEE");
String text = formatter.format(cal.getTime());
我个人会尽量避免使用Calendar
- 使用Joda Time,这是一个非常优越的日期和时间API。
答案 1 :(得分:3)
Calendar cal=Calendar.getInstance();
System.out.println(new SimpleDateFormat("EEE").format(cal.getTime()));
<强>输出强>
Wed
另见
答案 2 :(得分:3)
String dayNames[] = new DateFormatSymbols().getWeekdays();
Calendar date1 = Calendar.getInstance();
System.out.println("Today is a "
+ dayNames[date1.get(Calendar.DAY_OF_WEEK)]);
答案 3 :(得分:2)
java.util
日期时间 API 及其格式化 API SimpleDateFormat
已过时且容易出错。建议完全停止使用它们并切换到 modern Date-Time API*。
此外,下面引用的是 Home Page of Joda-Time 处的通知:
<块引用>请注意,从 Java SE 8 开始,要求用户迁移到 java.time (JSR-310) - JDK 的核心部分,取代了该项目。
使用 java.time
(现代日期时间 API)的解决方案:
import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.time.format.TextStyle;
import java.util.Locale;
public class Main {
public static void main(String[] args) {
String input = "27-04-2011";
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("d-M-u", Locale.ENGLISH);
LocalDate date = LocalDate.parse(input, dtf);
DayOfWeek dow = date.getDayOfWeek();
System.out.println(dow);
// String value
String strDay = dow.getDisplayName(TextStyle.FULL, Locale.ENGLISH);
System.out.println(strDay);
strDay = dow.getDisplayName(TextStyle.SHORT, Locale.ENGLISH);
System.out.println(strDay);
// Alternatively
strDay = date.format(DateTimeFormatter.ofPattern("EEEE", Locale.ENGLISH));
System.out.println(strDay);
strDay = date.format(DateTimeFormatter.ofPattern("EEE", Locale.ENGLISH));
System.out.println(strDay);
}
}
输出:
WEDNESDAY
Wednesday
Wed
Wednesday
Wed
从 Trail: Date Time 了解有关现代 Date-Time API 的更多信息。
* 出于任何原因,如果您必须坚持使用 Java 6 或 Java 7,您可以使用 ThreeTen-Backport,它将大部分 java.time 功能向后移植到 Java 6 & 7. 如果您正在为 Android 项目工作并且您的 Android API 级别仍然不符合 Java-8,请检查 Java 8+ APIs available through desugaring 和 How to use ThreeTenABP in Android Project。
答案 4 :(得分:1)
请参阅DAY_OF_WEEK字段的JavaDoc。它指向7个常量SUNDAY..SATURDAY,它们显示了如何解码cal.get(Calendary.DAY_OF_WEEK)的int返回值。你确定吗
Calendar cal = Calendar.getInstance();
cal.set(2011, 02, 28);
cal.get(Calendar.DAY_OF_WEEK);
为您返回错误的值?
答案 5 :(得分:1)
请尝试以下操作:
Calendar cal=Calendar.getInstance();
int val = cal.get(Calendar.DAY_OF_WEEK);
System.out.println(new DateFormatSymbols().getWeekdays()[val]);
或
Calendar cal=Calendar.getInstance();
String dayName = new DateFormatSymbols().getWeekdays()[cal
.get(Calendar.DAY_OF_WEEK)];
System.out.println(dayName);
答案 6 :(得分:1)
Calendar cal=Calendar.getInstance();
cal.set(2011, 2, 28);
int val = cal.get(Calendar.DAY_OF_WEEK);
System.out.println(val);
答案 7 :(得分:0)
查看SimpleDateFormat,并且可以Locale。
答案 8 :(得分:0)
如果您需要月份中的确切日期值,则需要使用 Calendar:DAY_OF_MONTH 它将返回从 1 开始的月份中的确切日期。
//Current date is 07-06-2021 and this will return 7
Calendar cal = Calendar.getInstance();
int val = cal.get(Calendar.DAY_OF_MONTH);
System.out.println("Date in month:"+val);
//If you want the day of the week in text better use the
//SimpleDateFormat, since Calendar API will return the integer value in
// the week if we given Calendar.DAY_OF_WEEK
String dayWeekText = new SimpleDateFormat("EEEE").format(cal.getTime());
System.out.println("Day of week:"+dayWeekText);
正如评论中所建议的,Java Date API 具有所有这些功能。 Java 8 为日期和时间引入了新的 API,以解决旧的 java.util.Date 和 java.util.Calendar 的缺点。
同样可以在 Java 中使用 Date API 来实现
LocalDate localDate = LocalDate.parse("2021-06-08"); //2021 June 08 Tuesday
System.out.println(localDate.dayOfWeek().getAsText()); //Output as : Tuesday
System.out.println(localDate.dayOfWeek().getAsShortText()); //Output as : Tue
System.out.println(localDate.dayOfMonth().get()); //Output as current date