Java實現對日期上旬中旬下旬格式化

PS:我數據庫表定義的日期是String類型,要求對讀取的日期進行格式化爲xx年xx月上\中\下旬java

測試代碼以下數據庫

package day1;

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

public class test_3 {
    
    /**
     * 將日期轉化爲上中下旬
     * 
     * @param inDate
     * @return
     */
    public static String getData(String inDate) {
        String returnCode = "月下旬";

        Calendar cal = Calendar.getInstance();//日曆對象
        try {
            cal.setTime(new SimpleDateFormat("yyyy-MM-dd").parse(inDate));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        int day = cal.get(Calendar.DATE);//獲取日
        if (day < 11)
            returnCode = "月上旬";
        else if (day < 21)
            returnCode = "月中旬";
        returnCode = cal.get(Calendar.YEAR) + "年" + String.valueOf(cal.get(Calendar.MONTH) + 1) + returnCode;

        return returnCode;
    }
    
    public static void main(String args[]){
        String s= "2019-05-28 23:49:26";
        System.out.println(getData(s));
    }
}
相關文章
相關標籤/搜索