下面的代碼是檢查星期幾,現在我想檢查日期,月份,年份......如何修改?
import java.util.Calendar
ImageView imageView;
Calendar cal = Calendar.getInstance();
cal.setTime(now_date);
imageView = (ImageView)findViewById(R.id.imageView);
if(cal.get(Calendar.DAY_OF_WEEK) == Calendar.MONDAY) {
imageView.setImageResource(R.drawable.IMAGE1);
} else if(cal.get(Calendar.DAY_OF_WEEK) == Calendar.TUESDAY) {
imageView.setImageResource(R.drawable.IMAGE2);
}
uj5u.com熱心網友回復:
時間
該java.util日期時間API及API格式,SimpleDateFormat都已經過時,而且容易出錯。建議完全停止使用它們并切換到現代 Date-Time API *。
使用java.time現代日期時間 API 的解決方案:ZonedDateTime與適用的一起使用ZoneId并從中檢索所需的值。
演示:
import java.time.DayOfWeek;
import java.time.Month;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.TextStyle;
import java.util.Locale;
public class Main {
public static void main(String[] args) {
// Specify the applicable ZoneId e.g.
// ZonedDateTime.now(ZoneId.of("Asia/Kolkata"))
// to get the current date-time in that timezone
ZonedDateTime now = ZonedDateTime.now();
DayOfWeek dow = now.getDayOfWeek();
System.out.println(dow);
int weekDayNum = dow.getValue();
System.out.println(weekDayNum);
String weekDayName = dow.getDisplayName(TextStyle.FULL, Locale.ENGLISH);
System.out.println(weekDayName);
int year = now.getYear();
System.out.println(year);
Month month = now.getMonth();
System.out.println(month);
int monthValue = month.getValue();
System.out.println(monthValue);
int dayOfMonth = now.getDayOfMonth();
System.out.println(dayOfMonth);
int dayOfYear = now.getDayOfYear();
System.out.println(dayOfYear);
}
}
輸出:
MONDAY
1
Monday
2021
NOVEMBER
11
1
305
ONLINE DEMO
從Trail: Date Time 中了解有關現代日期時間 API 的更多資訊。檢查此答案和此答案以了解如何將java.timeAPI 與 JDBC結合使用。
* 如果您正在為 Android 專案作業并且您的 Android API 級別仍然不符合 Java-8,請檢查通過 desugaring 可用的 Java 8 API。請注意,Android 8.0 Oreo 已提供對java.time.
uj5u.com熱心網友回復:
java.time 通過脫糖
考慮使用 java.time,現代 Java 日期和時間 API,用于您的日期作業。讓我們首先看看如何檢查星期幾:
LocalDate today = LocalDate.now(ZoneId.systemDefault());
switch (today.getDayOfWeek()) {
case MONDAY:
System.out.println("Set image to Monday’s image here");
break;
case TUESDAY:
System.out.println("Set image to Tuesday’s image here");
break;
default:
throw new AssertionError("Unsupported day of week: " today.getDayOfWeek());
}
我相信您自己填寫一周中剩余的五天,并按照您的問題設定影像視圖的影像資源。就我的代碼而言,當我今天(11 月 1 日星期一)運行它時,輸出是:
在此處將影像設定為星期一的影像
它的作業原理是getDayOfWeek()回傳DayOfWeek列舉的一個實體,而 Java 允許我們打開列舉。
對于一個月中的某一天:
switch (today.getDayOfMonth()) {
case 1:
System.out.println("Set image to image for 1st day of month here");
break;
case 2:
System.out.println("Set image to image for 2nd day of month here");
break;
default:
throw new AssertionError("Unsupported day of month: " today.getDayOfWeek());
}
在此處將影像設定為每月第一天的影像
再次自己填寫最多 31 個。一周中的一天是一個列舉,而一個月中的一天是一個普通的int.
月份的情況類似于星期幾,因為我們更喜歡使用列舉。getMonth()回傳的實體Month列舉,并在switch宣告中,你將有案件JANUARY,FEBRUARY等等
最后getYear()再次回傳一個int,因此年份情況將類似于月份中的日期,情況為 2021、2022 等。
編輯:你問:
……你能給出代碼來驗證單個 switch 案例中的日期和月份嗎?......只有一種情況它足夠“日期”和“月份”......休息我會做的
如果您對switch陳述句感到滿意,也可以將它們相互嵌套:
switch (today.getMonth()) {
case JANUARY:
switch (today.getDayOfMonth()) {
case 1:
System.out.println("Set image to image for 1st day of January here");
break;
// …
default:
throw new AssertionError("Unsupported day of January: " today.getDayOfWeek());
}
break;
// …
default:
throw new AssertionError("Unsupported month: " today.getMonth());
}
替代方案:使用地圖
對于稍微先進但也非常優雅的解決方案,而不是冗長的switchor if-else陳述句定義一周中每一天使用EnumMap<DayOfWeek, String>的影像(如果影像參考R.drawable.IMAGE1是 a String,抱歉,我不知道 Android 所以不能告訴)。對于一個月中的某一天,您可以使用HashMap<Integer, String>字串陣列或字串數??組。
Edit: I think the map approaches becomes particularly appealing when it comes to combining month and day of month. I am using Java 9 syntax here and hope it works with desugaring, it’s not something I know:
private static final Map<MonthDay, String> IMAGES_PER_DAY
= Map.ofEntries(
Map.entry(MonthDay.of(Month.JANUARY, 1), "Image for Jan 1"),
Map.entry(MonthDay.of(Month.JANUARY, 2), "Image for Jan 2"),
// …
Map.entry(MonthDay.of(Month.NOVEMBER, 1), "Image for Nov 1"),
// …
Map.entry(MonthDay.of(Month.DECEMBER, 31), "Image for Dec 31"));
Now picking the right image for today’s date is pretty simple:
String imageReference = IMAGES_PER_DAY.get(MonthDay.from(today));
System.out.println("Set image to " imageReference);
Set image to Image for Nov 1
Question: Doesn’t java.time require Android API level 26?
java.time works nicely on both older and newer Android devices. It just requires at least 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 either use desugaring or the Android edition of ThreeTen Backport. It’s called ThreeTenABP. In the latter case make sure you import the date and time classes from
org.threeten.bpwith subpackages.
Links
- Oracle tutorial: Date Time explaining how to use java.time.
- Java Specification Request (JSR) 310, where
java.timewas first described. - ThreeTen Backport project, the backport of
java.timeto Java 6 and 7 (ThreeTen for JSR-310). - Java 8 APIs available through desugaring
- ThreeTenABP, Android edition of ThreeTen Backport
- Question: How to use ThreeTenABP in Android Project, with a very thorough explanation.
uj5u.com熱心網友回復:
與 DAY_OF_WEEK 類似,您可以從 Calendar 實體中獲取其他欄位。
請查看 Java 檔案以獲取更多詳細資訊:https : //docs.oracle.com/javase/7/docs/api/java/util/Calendar.html
uj5u.com熱心網友回復:
參考檔案 https://docs.oracle.com/javase/7/docs/api/java/util/Calendar.html 你可以通過這種方式獲得周、月和年
Date today = new Date();
cal.setTime(today);
int dayOfWeek = cal.get(Calendar.DAY_OF_WEEK);
int dayOfMonth = cal.get(Calendar.DAY_OF_MONTH);
int dayOfYear = cal.get(Calendar.DAY_OF_YEAR);
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/344387.html
上一篇:如何在GoogleScripts中回傳一周的開始(開始日=星期日)?
下一篇:向電子郵件添加按鈕
