数字1-7转换成星期一到星期天

2023-01-02 00:00:00 数字

代码如下:

package studydemo;

import java.util.Calendar;
import java.util.HashMap;

public class WeekDays {
    public static void main(String[] args) {
        Calendar now = Calendar.getInstance();

        // 一周第一天是否为星期天
        boolean isFirstSunday = (now.getFirstDayOfWeek() == Calendar.SUNDAY);

        //获取周几
        int weekday = now.get(Calendar.DAY_OF_WEEK);
        System.out.println(weekday);

        //第三种方法
        System.out.println("星期"+"日一二三四五六".charAt(weekday-1));
        System.out.println("星期"+"一二三四五六日".charAt(weekday-2));
        
        //若一周第一天为星期天,则-1
        if (isFirstSunday) {
            weekday -= 1;
            if (weekday == 0) {
                weekday = 7;
            }

        }

        //第一种方法
        String day = replace(weekday);
        System.out.println("今天周"+day);

        //第二种方法
        HashMap<Integer, String> map = new HashMap<Integer,String>();
        map.put(1, "一");
        map.put(2, "二");
        map.put(3, "三");
        map.put(4, "四");
        map.put(5, "五");
        map.put(6, "六");
        map.put(7, "日");

        //        String days = map.get(1);
        //        System.out.println(days);
        String dayofweek = map.get(weekday);
        System.out.println("今天周"+dayofweek);

       

    }

    public static String replace(int weekday) {
        String day = null;
        switch (weekday) {
        case 1: day = "一"; break;
        case 2: day = "二"; break;
        case 3: day = "三"; break;
        case 4: day = "四"; break;
        case 5: day = "五"; break;
        case 6: day = "六"; break;
        case 7: day = "七"; break;
        default:break;
        }
        return day;
    }
    public String toChineseDate(int date){

        return "星期"+"日一二三四五六".charAt(date-1);
    }

}

控制台输入内容如下:
《数字1-7转换成星期一到星期天》

    原文作者:风轻云浓远山横
    原文地址: https://blog.csdn.net/qq_44865979/article/details/105248278
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。

相关文章