Date Time Java API Series 25 ----- Jdk8 in java.time package of the new date and time API classes, application MonthDay computing zodiac.

  By Java API Date Time Series 24 ----- Jdk8 new date and time API class java.time package, MonthDay class source code and applications, compared to the same month the time of day. MonthDay made of simple instructions and applications. Zodiac constellation which is calculated according to the Gregorian calendar birthday belongs to the class (Aries, Taurus, Gemini, Cancer, Leo, Virgo, Libra, Scorpio, Sagittarius, Capricorn, Aquarius, Pisces).

1. Zodiac Name Enumeration

package com.xkzhangsan.time.enums;

import static com.xkzhangsan.time.constants.Constant.MONTHDAY_FORMAT_PRE;

import java.time.MonthDay;
import java.util.Objects;

/**
 * Constellation name enumeration, include English name, Chinese name, time range
 * 
 * @ClassName: ConstellationNameEnum
 * @Description: ConstellationNameEnum
 * @author xkzhangsan
 * @Date 2020 Nian 02-29
 * / 
Public  enum ConstellationNameEnum {
    
    Aries(1, "白羊座", "03-21", "04-19"),
    Taurus(2, "金牛座", "04-20", "05-20"),
    Gemini(3, "双子座", "05-21", "06-21"),
    Cancer(4, "巨蟹座", "06-22", "07-22"),
    Leo(5, "狮子座", "07-23", "08-22"),
    Virgo(6, "处女座", "08-23", "09-22"),
    Libra(7, "天秤座", "09-23", "10-23"), 
    Scorpio(8, "天蝎座", "10-24", "11-22"),
    Sagittarius(9, "射手座", "11-23", "12-21"),
    Capricorn(10, "摩羯座", "12-22", "01-19"),
    Aquarius(11, "水瓶座", "01-20", "02-18"),
    Pisces ( 12 is "Pisces", "02-19", "03-20" ) ,;
    
    /**
     * No.
     */
    private int code;
    
    /**
     * Chinese name
     */
    private String nameCn;
    
    /**
     * Starting time
     */
    private String startDate;
    
    /**
     * End Time
     */
    private String endDate;

    

    private ConstellationNameEnum(int code, String nameCn, String startDate, String endDate) {
        this.code = code;
        this.nameCn = nameCn;
        this.startDate = startDate;
        this.endDate = endDate;
    }
    
    /**
     * According to the enumeration date constellation query name
     * @Param monthDayStr format of the MM-dd
     * @return
     */
    public static ConstellationNameEnum getConstellationNameEnumByMonthDay(String monthDayStr){
        Objects.requireNonNull(monthDayStr, "monthDayStr");
        MonthDay monthDay = MonthDay.parse(MONTHDAY_FORMAT_PRE + monthDayStr);
        for(ConstellationNameEnum constellationNameEnum : ConstellationNameEnum.values()){
            MonthDay monthDayStart = MonthDay.parse(MONTHDAY_FORMAT_PRE + constellationNameEnum.getStartDate());
            MonthDay monthDayEnd = MonthDay.parse(MONTHDAY_FORMAT_PRE + constellationNameEnum.getEndDate());
            if (monthDay.equals(monthDayStart) || monthDay.equals(monthDayEnd)
                    || (monthDay.isAfter(monthDayStart) && monthDay.isBefore(monthDayEnd))) {
                return constellationNameEnum;
            }
        }
        return null;
    }

    /**
     * The date the query constellation Chinese name
     * @Param monthDayStr format of the MM-dd
     * @return
     */
    public static String getNameCnByMonthDay(String monthDayStr){
        ConstellationNameEnum constellationNameEnum = getConstellationNameEnumByMonthDay(monthDayStr);
        return constellationNameEnum != null ? constellationNameEnum.getNameCn() : null;
    }
    
    /**
     * According to the English name of the constellation query date
     * @Param monthDayStr format of the MM-dd
     * @return
     */
    public static String getNameEnByMonthDay(String monthDayStr){
        ConstellationNameEnum constellationNameEnum = getConstellationNameEnumByMonthDay(monthDayStr);
        return constellationNameEnum != null ? constellationNameEnum.name() : null;
    }
    

    public int getCode() {
        return code;
    }

    public String getNameCn() {
        return nameCn;
    }

    public String getStartDate() {
        return startDate;
    }

    public String getEndDate() {
        return endDate;
    }

}

The main method: public static ConstellationNameEnum getConstellationNameEnumByMonthDay (String monthDayStr), derived by comparing the constellation of start time and day of the enumeration string input in.

Constellation name and range from Baidu Encyclopedia .

2. Applications

    /**
     * The date the query constellation Chinese name
     * @Param monthDayStr format of the MM-dd
     * @return
     */
    public static String getConstellationNameCn(String monthDayStr){
        return ConstellationNameEnum.getNameCnByMonthDay(monthDayStr);
    }
    
    /**
     * The date the query constellation Chinese name
     * @param date
     * @return
     */
    public static String getConstellationNameCn(Date date){
        String monthDayStr = DateTimeFormatterUtil.format(date, DateTimeFormatterUtil.MM_DD_FMT);
        return ConstellationNameEnum.getNameCnByMonthDay(monthDayStr);
    }
    
    /**
     * According to the English name of the constellation query date
     * @Param monthDayStr format of the MM-dd
     * @return
     */
    public static String getConstellationNameEn(String monthDayStr){
        return ConstellationNameEnum.getNameEnByMonthDay(monthDayStr);
    }    

 

Test code:

    /**
     * Constellation computing test
     */
    @Test
    public void constellationTest(){
        System.out.println(DateTimeCalculatorUtil.getConstellationNameCn("02-29"));
        System.out.println(DateTimeCalculatorUtil.getConstellationNameEn("02-29"));
        
        //2020-05-01 的星座
        System.out.println(DateTimeCalculatorUtil.getConstellationNameCn(DateTimeCalculatorUtil.getDate(2020, 5, 1)));
    }

 

Output:

Pisces
fish
Taurus

 

 

Source address: https://github.com/xkzhangsan/xk-time

 

Guess you like

Origin www.cnblogs.com/xkzhangsanx/p/12384296.html