日付組立(例:2020年1月1日、二〇二〇年1月1日、1/1/2020)

3035 ワード

package com.yhcookie.basedemo.util;

import org.springframework.util.StringUtils;
import java.time.LocalDate;

/**
 *  
 * @author yhcookie
 * @date 2020/5/2 10:20
 */
public class DateWrapperTest {

    public static void main(String[] args){
        // 2020-01-01
        LocalDate currentDate = LocalDate.now();

        /**
         * ey:currentDateStr = "2020-01-01"
         *     currentYearStr = "2020"
         *     currentMonthStr = "1"
         *     currentDayStr = "1"
         */
        String currentDateStr = currentDate.toString();
        String currentYearStr = String.valueOf(currentDate.getYear());
        String currentMonthStr = String.valueOf(currentDate.getMonth().getValue());
        String currentDayStr = String.valueOf(currentDate.getDayOfMonth());
        
        // 2020 1 1 
        String chineseCurrentDate 
                = currentYearStr + " " + currentMonthStr + " " + currentDayStr + " ";
        //  
        String fullChineseCurrentDate
                = numberStrToChineseStr(currentYearStr) + " " + numberStrToChineseStr(currentMonthStr) + " " + numberStrToChineseStr(currentDayStr) + " ";
        // 1/1/2020
        String enCurrentDateStr 
                = currentDayStr + "/" + currentMonthStr + "/" + currentYearStr;
    }

    /**
     *  
     * @return
     */
    private static String numberStrToChineseStr(String numberStr){
        // "" || null ||  
        if (StringUtils.isEmpty(numberStr) || !numberStr.matches("^[0-9]+")){
            return numberStr;
        }

        String chineseStr = "";
        char[] numberStrCharArray = numberStr.toCharArray();
        for (char c : numberStrCharArray) {
            switch (c){
                case '0':
                    chineseStr += " ";
                    break;
                case '1':
                    chineseStr += " ";
                    break;
                case '2':
                    chineseStr += " ";
                    break;
                case '3':
                    chineseStr += " ";
                    break;
                case '4':
                    chineseStr += " ";
                    break;
                case '5':
                    chineseStr += " ";
                    break;
                case '6':
                    chineseStr += " ";
                    break;
                case '7':
                    chineseStr += " ";
                    break;
                case '8':
                    chineseStr += " ";
                    break;
                case '9':
                    chineseStr += " ";
                    break;
                default:
                    break;
            }
        }
        return chineseStr;
    }
}