为什么 new java.text.SimpleDateFormat("EEEE").format(new java.util.Date(2015, 6, 9)) return 错误的星期几?
Why does new java.text.SimpleDateFormat("EEEE").format(new java.util.Date(2015, 6, 9)) return the wrong day of the week?
我想从 Java
中的 date
中获取星期几。为什么这个 return Friday when
其实是 Tuesday
?
new java.text.SimpleDateFormat("EEEE").format(new java.util.Date(2015, 6, 9))
PS 我知道 java.util.Date(int year, int month, int day)
已被弃用,也许这与它有关。
该已弃用构造函数中的 month
参数从 0
开始表示一月份,因此如果您谈论的是 6 月 9 日,您可能需要 new Date(115, 5, 9)
(115
是因为 year
参数是“自 1900 年以来”)。
Parameters:
year
- the year minus 1900.
month
- the month between 0-11.
date
- the day of the month between 1-31.
(我的重点。)
你说过你想做单线的。在 Java 8 中,你可以这样做:
String day = LocalDate.of(2015, 6, 9).format(DateTimeFormatter.ofPattern("EEEE"));
使用 java.time.LocalDate
and java.time.format.DateTimeFormatter
.
java.time
旧版日期时间 API(java.util
日期时间类型及其格式 API、SimpleDateFormat
)已过时且容易出错。建议完全停止使用,改用java.time
,modern date-time API*.
使用现代日期时间的解决方案API:
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[]) {
System.out.println(DateTimeFormatter.ofPattern("EEEE", Locale.ENGLISH).format(LocalDate.of(2015, 6, 9)));
// Alternatively
System.out.println(LocalDate.of(2015, 6, 9).getDayOfWeek().getDisplayName(TextStyle.FULL, Locale.ENGLISH));
}
}
输出:
Tuesday
Tuesday
从 Trail: Date Time[=41= 中了解有关 modern date-time API* 的更多信息].
* 无论出于何种原因,如果您必须坚持Java 6 或Java 7,您可以使用ThreeTen-Backport which backports most of the java.time functionality to Java 6 & 7. If you are working for an Android project and your Android API level is still not compliant with Java-8, check Java 8+ APIs available through desugaring and 。
我想从 Java
中的 date
中获取星期几。为什么这个 return Friday when
其实是 Tuesday
?
new java.text.SimpleDateFormat("EEEE").format(new java.util.Date(2015, 6, 9))
PS 我知道 java.util.Date(int year, int month, int day)
已被弃用,也许这与它有关。
该已弃用构造函数中的 month
参数从 0
开始表示一月份,因此如果您谈论的是 6 月 9 日,您可能需要 new Date(115, 5, 9)
(115
是因为 year
参数是“自 1900 年以来”)。
Parameters:
year
- the year minus 1900.
month
- the month between 0-11.
date
- the day of the month between 1-31.
(我的重点。)
你说过你想做单线的。在 Java 8 中,你可以这样做:
String day = LocalDate.of(2015, 6, 9).format(DateTimeFormatter.ofPattern("EEEE"));
使用 java.time.LocalDate
and java.time.format.DateTimeFormatter
.
java.time
旧版日期时间 API(java.util
日期时间类型及其格式 API、SimpleDateFormat
)已过时且容易出错。建议完全停止使用,改用java.time
,modern date-time API*.
使用现代日期时间的解决方案API:
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[]) {
System.out.println(DateTimeFormatter.ofPattern("EEEE", Locale.ENGLISH).format(LocalDate.of(2015, 6, 9)));
// Alternatively
System.out.println(LocalDate.of(2015, 6, 9).getDayOfWeek().getDisplayName(TextStyle.FULL, Locale.ENGLISH));
}
}
输出:
Tuesday
Tuesday
从 Trail: Date Time[=41= 中了解有关 modern date-time API* 的更多信息].
* 无论出于何种原因,如果您必须坚持Java 6 或Java 7,您可以使用ThreeTen-Backport which backports most of the java.time functionality to Java 6 & 7. If you are working for an Android project and your Android API level is still not compliant with Java-8, check Java 8+ APIs available through desugaring and