例如,我有日期:“23/2/2010”(2010年2月23日)。我想把它传递给一个返回星期几的函数。我该怎么做呢?
在这个例子中,函数应该返回String "Tue"。
此外,如果只需要日期顺序,如何检索?
例如,我有日期:“23/2/2010”(2010年2月23日)。我想把它传递给一个返回星期几的函数。我该怎么做呢?
在这个例子中,函数应该返回String "Tue"。
此外,如果只需要日期顺序,如何检索?
当前回答
import java.time.*;
public static String findDay(int month, int day, int year) {
LocalDate localDate = LocalDate.of(year, month, day);
DayOfWeek dayOfWeek = localDate.getDayOfWeek();
return dayOfWeek.name();
}
其他回答
是的。根据具体情况:
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.util.scanner包给用户输入日期、月和年来查找星期几的程序:
import java.util.Scanner;
public class Calender {
public static String getDay(String day, String month, String year) {
int ym, yp, d, ay, a = 0;
int by = 20;
int[] y = new int[]{6, 4, 2, 0};
int[] m = new int []{0, 3, 3, 6, 1, 4, 6, 2, 5, 0, 3, 5};
String[] wd = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
int gd = Integer.parseInt(day);
int gm = Integer.parseInt(month);
int gy = Integer.parseInt(year);
ym = gy % 100;
yp = ym / 4;
ay = gy / 100;
while (ay != by) {
by = by + 1;
a = a + 1;
if(a == 4) {
a = 0;
}
}
if ((ym % 4 == 0) && (gm == 2)) {
d = (gd + m[gm - 1] + ym + yp + y[a] - 1) % 7;
} else
d = (gd + m[gm - 1] + ym + yp + y[a]) % 7;
return wd[d];
}
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
String day = in.next();
String month = in.next();
String year = in.next();
System.out.println(getDay(day, month, year));
}
}
简单地使用SimpleDateFormat。
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy", java.util.Locale.ENGLISH);
Date myDate = sdf.parse("28/12/2013");
sdf.applyPattern("EEE, d MMM yyyy");
String sMyDate = sdf.format(myDate);
结果是:2013年12月28日星期六
默认构造函数接受“默认”区域设置,因此在需要特定模式时要小心使用它。
public SimpleDateFormat(String pattern) {
this(pattern, Locale.getDefault(Locale.Category.FORMAT));
}
方法下面检索七天,并返回短名称的天在列表数组在Kotlin,你可以重新格式化然后在Java格式,只是提出想法日历可以返回短名称
private fun getDayDisplayName():List<String>{
val calendar = Calendar.getInstance()
val dates= mutableListOf<String>()
dates.clear()
val s= calendar.getDisplayName(DAY_OF_WEEK, SHORT, Locale.US)
dates.add(s)
for(i in 0..5){
calendar.roll( Calendar.DATE, -1)
dates.add(calendar.getDisplayName(DAY_OF_WEEK, SHORT, Locale.US))
}
return dates.toList()
}
结果是这样的
I/System.out: Wed
Tue
Mon
Sun
I/System.out: Sat
Fri
Thu
import java.time.*;
public static String findDay(int month, int day, int year) {
LocalDate localDate = LocalDate.of(year, month, day);
DayOfWeek dayOfWeek = localDate.getDayOfWeek();
return dayOfWeek.name();
}