程序员最近都爱上了这个网站  程序员们快来瞅瞅吧!  it98k网:it98k.com

本站消息

站长简介/公众号

  出租广告位,需要合作请联系站长


+关注
已关注

分类  

暂无分类

标签  

暂无标签

日期归档  

暂无数据

如何获取星期几、月份日期、月份和年份?

发布于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.time 通过脱糖

考虑为您的日期工作使用现代 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 等。MonthswitchJANUARYFEBRUARY

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());
    }

Alternative: use maps

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

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.bp with subpackages.

Links



所属网站分类: 技术文章 > 问答

作者:黑洞官方问答小能手

链接:http://www.javaheidong.com/blog/article/502605/56bfc4ab713d4ab89e10/

来源:java黑洞网

任何形式的转载都请注明出处,如有侵权 一经发现 必将追究其法律责任

7 0
收藏该文
已收藏

评论内容:(最多支持255个字符)