Java 如何获取日期的工作日?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18600257/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get the weekday of a Date?
提问by Sunny Chhatwal
I want to get the day of week from the Java Date
object when I have an array of Date
in String with me.
Date
当我有一个Date
in String数组时,我想从 Java对象中获取星期几。
SimpleDateFormat sourceDateformat = new SimpleDateFormat("yyyy-MM-dd");
public String[] temp_date;
public Int[] day = new Int[5];
Date[] d1= new Date[5];
Calendar[] cal= new Calendar[5]
try {
d1[i]= sourceDateformat.parse(temp_date[i].toString());
cal[i].setTime(d1[i]); // its not compiling this line..showing error on this line
day[i]= cal[i].get(Calendar.DAY_OF_WEEK);
}
catch (ParseException e) {
e.printStackTrace();
}
Does anyone know the answer to this?
有谁知道这个问题的答案?
采纳答案by Philipp Jahoda
You can get the day-integerlike that:
你可以得到这样的天整数:
Calendar c = Calendar.getInstance();
c.setTime(yourdate); // yourdate is an object of type Date
int dayOfWeek = c.get(Calendar.DAY_OF_WEEK); // this will for example return 3 for tuesday
If you need the output to be "Tue" rather than 3,instead of going through a calendar, just reformat the string: new SimpleDateFormat("EE").format(date)
(EE meaning "day of week, short version")
如果您需要输出为“Tue”而不是 3,而不是通过日历,只需重新格式化字符串:new SimpleDateFormat("EE").format(date)
(EE 表示“星期几,短版”)
Taken from here: How to determine day of week by passing specific date?
取自此处:如何通过传递特定日期来确定星期几?
回答by Dise?o Web Cantabria
// kotlin
val calendar = Calendar.getInstance()
val dateInfo = DateFormat.getDateInstance(DateFormat.FULL).format(calendar.time)
data.text = dateInfo
回答by Ole V.V.
This is one of the many things that have become a lot easier with the advent of java.time, the modern Java date and time API.
这是随着现代 Java 日期和时间 API java.time 的出现而变得更加容易的众多事情之一。
String tempDate = "2020-03-29";
LocalDate date = LocalDate.parse(tempDate);
DayOfWeek day = date.getDayOfWeek();
System.out.println(day);
Output:
输出:
SUNDAY
星期日
Of course we now have got an enum for the days of the week. There's no longer any reason to fiddle with integers and having to remember on what day of week they begin and whether the days are numbered from 0 or 1.
当然,我们现在有一个星期几的枚举。不再有任何理由摆弄整数,也不必记住它们从一周中的哪一天开始,以及这些天是从 0 还是 1 编号。
I am expoiting the fact that your expected input format (yyyy-MM-dd
in your code) is ISO 8601, the default for java.time, so we don't need to specify any formatter explicitly.
我正在利用这样一个事实,即您的预期输入格式(yyyy-MM-dd
在您的代码中)是 ISO 8601,即 java.time 的默认格式,因此我们不需要明确指定任何格式化程序。
Question: Doesn't java.time require Android API level 26?
问题:java.time 不需要 Android API 级别 26 吗?
java.time works nicely on both older and newer Android devices. It just requires at least Java 6.
java.time 在较旧和较新的 Android 设备上都能很好地工作。它只需要至少Java 6。
- In Java 8 and later and on newer Android devices (from API level 26) the modern API comes built-in.
- In non-Android Java 6 and 7 get the ThreeTen Backport, the backport of the modern classes (ThreeTen for JSR 310; see the links at the bottom).
- On (older) Android use the Android edition of ThreeTen Backport. It's called ThreeTenABP. And make sure you import the date and time classes from
org.threeten.bp
with subpackages.
- 在 Java 8 及更高版本和更新的 Android 设备(从 API 级别 26)中,现代 API 是内置的。
- 在非 Android Java 6 和 7 中获得 ThreeTen Backport,现代类的 backport(ThreeTen for JSR 310;请参阅底部的链接)。
- 在(较旧的)Android 上使用 ThreeTen Backport 的 Android 版本。它被称为 ThreeTenABP。并确保从
org.threeten.bp
子包中导入日期和时间类。
Links
链接
- Oracle tutorial: Date Timeexplaining how to use java.time.
- Java Specification Request (JSR) 310, where
java.time
was first described. - ThreeTen Backport project, the backport of
java.time
to Java 6 and 7 (ThreeTen for JSR-310). - ThreeTenABP, Android edition of ThreeTen Backport
- Question: How to use ThreeTenABP in Android Project, with a very thorough explanation.
- Wikipedia article: ISO 8601
- Oracle 教程:解释如何使用 java.time 的日期时间。
- Java 规范请求 (JSR) 310,
java.time
首先描述的地方。 - ThreeTen Backport 项目,
java.time
向 Java 6 和 7 (ThreeTen for JSR-310)的向后移植。 - ThreeTenABP, ThreeTen Backport安卓版
- 问题:如何在Android项目中使用ThreeTenABP,讲解很透彻。
- 维基百科文章:ISO 8601