发布于2022-09-16 11:08 阅读(1489) 评论(0) 点赞(7) 收藏(1)
下面的代码是检查星期几,现在我想检查日期,月份,年份......如何修改它?
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);
}
考虑为您的日期工作使用现代 Java 日期和时间 API java.time。让我们先看看如何检查星期几:
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()
的一个实例,在您的语句中,您将有 case 、等。Month
switch
JANUARY
FEBRUARY
Finally getYear()
again returns an int
, so the year case will be similar to the day of the month with cases 2021, 2022, etc.
Edit: you asked:
… can you give code to validate both date and month in single switch case? … only for one case its enough for "DATE" and "MONTH"...rest i will do it
If you’re comfortable with switch
statements, you may also nest them inside each other:
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());
}
For a slightly advanced but also very elegant solution, instead of the longish switch
or if
-else
statement define the images to use for each day of week in an EnumMap<DayOfWeek, String>
(if the image reference in R.drawable.IMAGE1
is a String
, sorry, I don’t know Android so can’t tell). For the day of the month you may either use a HashMap<Integer, String>
or an arrays of strings.
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
java.time works nicely on both older and newer Android devices. It just requires at least Java 6.
org.threeten.bp
with subpackages.java.time
was first described.java.time
to Java 6 and 7 (ThreeTen for JSR-310).作者:黑洞官方问答小能手
链接:http://www.javaheidong.com/blog/article/502605/56bfc4ab713d4ab89e10/
来源:java黑洞网
任何形式的转载都请注明出处,如有侵权 一经发现 必将追究其法律责任
昵称:
评论内容:(最多支持255个字符)
---无人问津也好,技不如人也罢,你都要试着安静下来,去做自己该做的事,而不是让内心的烦躁、焦虑,坏掉你本来就不多的热情和定力
Copyright © 2018-2021 java黑洞网 All Rights Reserved 版权所有,并保留所有权利。京ICP备18063182号-2
投诉与举报,广告合作请联系vgs_info@163.com或QQ3083709327
免责声明:网站文章均由用户上传,仅供读者学习交流使用,禁止用做商业用途。若文章涉及色情,反动,侵权等违法信息,请向我们举报,一经核实我们会立即删除!