java获取日期对应的月份、java获取月份对应的季度、java获取月份对应季节

Java源码网 4月前 ⋅ 371 阅读 0 下载

java获取日期对应的月份、java获取月份对应的季度、java获取月份对应季节

package org.ezplatform.dj.utils.common;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * @Author zhaoxinguo
 * @Date 2022-07-18 10:02
 * @ClassName PartyDateUtils
 * @Description 党务平台日期时间工具类
 */
public class PartyDateUtils {

    public static SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd");
    public static SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd HH:mm");
    public static SimpleDateFormat sdf3 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    public static Date sdf1Parse(String str) {
        Date date = null;
        try {
            date = PartyDateUtils.sdf1.parse(str);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return date;
    }

    public static Date sdf2Parse(String str) {
        Date date = null;
        try {
            date = PartyDateUtils.sdf2.parse(str);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return date;
    }

    public static Date sdf3Parse(String str) {
        Date date = null;
        try {
            date = PartyDateUtils.sdf3.parse(str);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return date;
    }

    /**
     * 字符串转日期
     * @param sdf
     * @param str
     * @return
     */
    public static Date strToDate(SimpleDateFormat sdf, String str) {
        Date date = null;
        try {
            date = sdf.parse(str);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return date;
    }

    /**
     * 获取日期对应的月份
     *
     * @param date 日期
     * @return 月份
     */
    public static int getMM(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        return calendar.get(Calendar.MONTH) + 1;
    }

    /**
     * 获取月份对应的季度
     *
     * @param month 月份
     * @return 季度
     */
    public static int getQuarter(int month) {
        return (month + 2) / 3;
    }

    /**
     * 获取月份对应的季节
     * 3,4,5,月为春天,6,7,8,月为夏天,9,10,11月为秋天,12,1,2月为冬天
     * @param month 月份
     * @return 季节
     */
    public static String getSeason(int month) {
        String season = "春天";
        switch (month / 3) {
            case 1:
                season = "春天";
                break;
            case 2:
                season = "夏天";
                break;
            case 3:
                season = "秋天";
                break;
            default:
                season = "冬天";
        }
        return season;
    }

    public static void main(String [] args) {
        String str = "2022-07-18";
        // 获取日期对应的月份
        System.out.println(getMM(strToDate(PartyDateUtils.sdf1, str)));
        // 获取月份对应的季度
        System.out.println(getQuarter(getMM(new Date())));
        // 获取月份对应的季节
        System.out.println(getSeason(getMM(new Date())));
    }
}

运行结果:

微信截图_20220718101951.png


全部评论: 0

    我有话说: