Java实现对日期上旬中旬下旬格式化

PS:我数据库表定义的日期是String类型,要求对读取的日期进行格式化为xx年xx月上\中\下旬

测试代码如下

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));
    }
}

 

posted @ 2019-07-17 13:34  曾聪聪  阅读(1055)  评论(0编辑  收藏  举报