例如,我有日期:“23/2/2010”(2010年2月23日)。我想把它传递给一个返回星期几的函数。我该怎么做呢?

在这个例子中,函数应该返回String "Tue"。

此外,如果只需要日期顺序,如何检索?


当前回答

对于Java 8或更高版本,Localdate更可取

import java.time.LocalDate;

public static String findDay(int month, int day, int year) {

    LocalDate localDate = LocalDate.of(year, month, day);

    java.time.DayOfWeek dayOfWeek = localDate.getDayOfWeek();
    System.out.println(dayOfWeek);

    return dayOfWeek.toString();
}

注意:如果输入是字符串/用户定义的,那么你应该把它解析成int。

其他回答

java.time

使用java。在Java 8及以后版本中内置了时间框架。

DayOfWeek枚举可以生成一个字符串,其中包含自动本地化为本地语言和文化规范的日期名称。指定一个TextStyle来指示您想要长形式或缩写名称。

import java.time.LocalDate
import java.time.format.DateTimeFormatter
import java.time.format.TextStyle
import java.util.Locale
import java.time.DayOfWeek;

DateTimeFormatter formatter = DateTimeFormatter.ofPattern("d/M/yyyy");
LocalDate date = LocalDate.parse("23/2/2010", formatter); // LocalDate = 2010-02-23
DayOfWeek dow = date.getDayOfWeek();  // Extracts a `DayOfWeek` enum object.
String output = dow.getDisplayName(TextStyle.SHORT, Locale.US); // String = Tue
...
import java.time.LocalDate;
...
//String month = in.next();
int mm = in.nextInt();
//String day = in.next();
int dd = in.nextInt();
//String year = in.next();
int yy = in.nextInt();
in.close();
LocalDate dt = LocalDate.of(yy, mm, dd);
System.out.print(dt.getDayOfWeek());

是的。根据具体情况:

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

添加了另一种完全符合OP要求的方法,而不使用最新的内置方法:

public static String getDay(String inputDate) {
    String dayOfWeek = null;
    String[] days = new String[]{"Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"};

    try {
        SimpleDateFormat format1 = new SimpleDateFormat("dd/MM/yyyy");
        Date dt1 = format1.parse(inputDate);
        dayOfWeek = days[dt1.getDay() - 1];
    } catch(Exception e) {
        System.out.println(e);
    }

    return dayOfWeek;
}

这是正确的…

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对象的值。