例如,我有日期:“23/2/2010”(2010年2月23日)。我想把它传递给一个返回星期几的函数。我该怎么做呢?
在这个例子中,函数应该返回String "Tue"。
此外,如果只需要日期顺序,如何检索?
例如,我有日期:“23/2/2010”(2010年2月23日)。我想把它传递给一个返回星期几的函数。我该怎么做呢?
在这个例子中,函数应该返回String "Tue"。
此外,如果只需要日期顺序,如何检索?
当前回答
是的。根据具体情况:
You can use java.util.Calendar: Calendar c = Calendar.getInstance(); c.setTime(yourDate); int dayOfWeek = c.get(Calendar.DAY_OF_WEEK); if you need the output to be Tue rather than 3 (Days of week are indexed starting at 1 for Sunday, see Calendar.SUNDAY), instead of going through a calendar, just reformat the string: new SimpleDateFormat("EE").format(date) (EE meaning "day of week, short version") if you have your input as string, rather than Date, you should use SimpleDateFormat to parse it: new SimpleDateFormat("dd/M/yyyy").parse(dateString) you can use joda-time's DateTime and call dateTime.dayOfWeek() and/or DateTimeFormat. edit: since Java 8 you can now use java.time package instead of joda-time
其他回答
private String getDay(Date date){
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("EEEE");
//System.out.println("DAY "+simpleDateFormat.format(date).toUpperCase());
return simpleDateFormat.format(date).toUpperCase();
}
private String getDay(String dateStr){
//dateStr must be in DD-MM-YYYY Formate
Date date = null;
String day=null;
try {
date = new SimpleDateFormat("DD-MM-YYYY").parse(dateStr);
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("EEEE");
//System.out.println("DAY "+simpleDateFormat.format(date).toUpperCase());
day = simpleDateFormat.format(date).toUpperCase();
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return day;
}
是的。根据具体情况:
You can use java.util.Calendar: Calendar c = Calendar.getInstance(); c.setTime(yourDate); int dayOfWeek = c.get(Calendar.DAY_OF_WEEK); if you need the output to be Tue rather than 3 (Days of week are indexed starting at 1 for Sunday, see Calendar.SUNDAY), instead of going through a calendar, just reformat the string: new SimpleDateFormat("EE").format(date) (EE meaning "day of week, short version") if you have your input as string, rather than Date, you should use SimpleDateFormat to parse it: new SimpleDateFormat("dd/M/yyyy").parse(dateString) you can use joda-time's DateTime and call dateTime.dayOfWeek() and/or DateTimeFormat. edit: since Java 8 you can now use java.time package instead of joda-time
这是正确的…
java.time.LocalDate; //package related to time and date
它提供了内置方法getDayOfWeek()来获取特定星期的日期:
int t;
Scanner s = new Scanner(System.in);
t = s.nextInt();
s.nextLine();
while(t-->0) {
int d, m, y;
String ss = s.nextLine();
String []sss = ss.split(" ");
d=Integer.parseInt(sss[0]);
m = Integer.parseInt(sss[1]);
y = Integer.parseInt(sss[2]);
LocalDate l = LocalDate.of(y, m, d); //method to get the localdate instance
System.out.println(l.getDayOfWeek()); //this returns the enum DayOfWeek
要将枚举l.getDayOfWeek()的值赋给一个字符串,您可能会使用enum中名为name()的方法,该方法返回enum对象的值。
日历类有内置的displayName功能:
Calendar.getInstance().getDisplayName(Calendar.DAY_OF_WEEK, Calendar.SHORT, Locale.getDefault()); // Thu
Calendar.SHORT -> Thu
Calendar.LONG_FORMAT -> Thursday
Java 1.6以来可用。参见Oracle文档
LocalDate date=LocalDate.now();
System.out.println(date.getDayOfWeek());//prints THURSDAY
System.out.println(date.getDayOfWeek().getDisplayName(TextStyle.SHORT, Locale.US) ); //prints Thu
java.time.DayOfWeek is a enum which returns the singleton instance for the day-of-week of the weekday of the date.