天天看點

日期格式化為yyyymmdd_小白篇(二十):通過Java生成日期次元表

簡短介紹 大家好,一年一度的雙十一又到了。準備好剁手了嗎?反正我是看透了套路,什麼時候買都差不多(好吧,其實咱窮^_^,求個心理安慰)。今天給大家分享個日期次元表的工具類。

01

為什麼要日期次元表

日期次元在數倉中有着很重要的作用。舉個栗子:我們展示全年每天商品交易量。如果某天某個商品沒有交易量,但是這個日期又得顯示出來。那我們要怎麼處理呢?此時日期次元表就起到作用了,我們隻要用日期次元表做查詢主表,就可以很友善的展示每天是否有交易量的資料了。

02

建立日期次元表

-- dim_dateCREATE TABLE dim_date(  id string COMMENT '自增主鍵id',   day_code string COMMENT '日代碼',   day_long_desc string COMMENT '日完整名稱',   day_medium_desc string COMMENT '日中等長度名',   day_short_desc string COMMENT '日短名',   week_code string COMMENT '周代碼',   week_long_desc string COMMENT '周完整名稱',   week_medium_desc string COMMENT '周中等長度名',   week_short_desc string COMMENT '周短名',   week_name string COMMENT '周名稱',   ten_day_code string COMMENT '旬代碼',   ten_day_long_desc string COMMENT '旬完整名稱',   ten_day_medium_desc string COMMENT '旬中等長度名',   ten_day_short_desc string COMMENT '旬短名',   month_code string COMMENT '月代碼',   month_long_desc string COMMENT '月完整名稱',   month_medium_desc string COMMENT '月中等長度名',   month_short_desc string COMMENT '月短名',   quarter_code string COMMENT '季代碼',   quarter_long_desc string COMMENT '季完整名稱',   quarter_medium_desc string COMMENT '季中等長度名',   quarter_short_desc string COMMENT '季短名',   half_year_code string COMMENT '半年代碼',   half_long_desc string COMMENT '半年完整名稱',   half_medium_desc string COMMENT '半年中等長度名',   half_short_desc string COMMENT '半年短名',   year_code string COMMENT '年代碼',   year_long_desc string COMMENT '年完整名稱',   year_medium_desc string COMMENT '年中等長度名',   year_short_desc string COMMENT '年短名',   all_time_code string COMMENT '全部時間代碼',   all_time_desc string COMMENT '全部時間名稱',   day_timespan string COMMENT '日時間跨天',   day_end_date string COMMENT '結束日期',   week_timespan string COMMENT '周跨天數',   week_end_date string COMMENT '周結束日期',   ten_day_timespan string COMMENT '旬跨天數',   ten_day_end_date string COMMENT '旬結束日期',   month_timespan string COMMENT '月跨天數',   month_end_date string COMMENT '月結束日期',   quarter_timespan string COMMENT '季跨天數',   quarter_end_date string COMMENT '季結束日期',   half_year_timespan string COMMENT '半年跨天數',   half_year_end_date string COMMENT '半年結束日期',   year_timespan string COMMENT '年跨天數',   year_end_date string COMMENT '年結束日期',   week_start_date string COMMENT '周開始日期',   month_start_date string COMMENT '月開始時間',   quarter_start_date string COMMENT '季度開始時間',   workday_flag string COMMENT '是否工作日:1.是 0.否',   weekend_flag string COMMENT '是否周末:1.是 0.否',   holiday_flag string COMMENT '是否為節假日 1:是,0:否',   holiday_name string COMMENT '節假日名稱',   is_workday string COMMENT '是否上班 1:是,0:否',    last_week_day string COMMENT '周的最後一天',   last_month_day string COMMENT '月的最後一天',  load_time string COMMENT '加載時間')COMMENT '日期次元表'ROW FORMAT DELIMITED FIELDS TERMINATED BY '\t' STORED AS TEXTFILE;
           

03

使用Java生成日期次元資料

import java.io.File;import java.io.FileWriter;import java.io.IOException;import java.io.PrintWriter;import java.text.ParseException;import java.text.SimpleDateFormat;import java.util.Calendar;import java.util.Date;public class DateDimUtil {    /**     * 目前日期 + 1天     * 傳回:yyyymmdd     */    public static String getNowDate(String specifiedDay) {        Calendar c = Calendar.getInstance();        Date date = null;        try {            date = new SimpleDateFormat("yyyyMMdd").parse(specifiedDay);        } catch (ParseException e) {            e.printStackTrace();        }        c.setTime(date);        int day = c.get(Calendar.DATE);        c.set(Calendar.DATE, day + 1);        return new SimpleDateFormat("yyyyMMdd").format(c.getTime());    }    /**     * 目前日期,是星期幾     */    public static int getWeekDay(String specifiedDay) {        Date date = null;        try {            date = new SimpleDateFormat("yyyyMMdd").parse(specifiedDay);        } catch (ParseException e) {            e.printStackTrace();        }        Calendar c = Calendar.getInstance();        c.setTime(date);        int week_of_year = c.get(Calendar.DAY_OF_WEEK);        week_of_year = week_of_year - 1;        if (week_of_year == 0) {            week_of_year = 7;        }        return week_of_year;    }    /**     * 目前日期,屬于年的第幾周     */    public static int getWeekofYear(String specifiedDay) {        Calendar c = Calendar.getInstance();        Date date = null;        try {            date = new SimpleDateFormat("yyyyMMdd").parse(specifiedDay);        } catch (ParseException e) {            e.printStackTrace();        }        c.setTime(date);        c.setFirstDayOfWeek(Calendar.MONDAY);        int week = c.get(Calendar.WEEK_OF_YEAR);        return week;    }    /**     * 目前日期,格式化轉換     */    public static String yyyymmdd(String specifiedDay,String patten) throws ParseException{        Date date = new SimpleDateFormat("yyyyMMdd").parse(specifiedDay);        return new SimpleDateFormat(patten).format(date);    }    /**     * 目前日期,所屬月多少天     */    public static int getMDaycnt(String specifiedDay) {        Date date = null;        try {            date = new SimpleDateFormat("yyyyMMdd").parse(specifiedDay);        } catch (ParseException e) {            e.printStackTrace();        }        Calendar c = Calendar.getInstance();        c.setTime(date);        int i = c.get(Calendar.DAY_OF_MONTH);        return i;    }    /**     * 目前日期,所屬月旬數     * 三旬: 上旬1-10日 中旬11-20日 下旬21-31日     * 傳回: 1\2\3     */    public static int getTenDay(String specifiedDay) {        Date date = null;        try {            date = new SimpleDateFormat("yyyyMMdd").parse(specifiedDay);        } catch (ParseException e) {            e.printStackTrace();        }        Calendar c = Calendar.getInstance();        c.setTime(date);        int i = c.get(Calendar.DAY_OF_MONTH);        if (i < 11)            return 1;        else if (i < 21)            return 2;        else            return 3;    }    /**     * 目前日期,所屬月旬數     * 三旬: 上旬1-10日 中旬11-20日 下旬21-31日     * 傳回: 1\2\3     */    public static String getTenDays(String specifiedDay) {        Date date = null;        try {            date = new SimpleDateFormat("yyyyMMdd").parse(specifiedDay);        } catch (ParseException e) {            e.printStackTrace();        }        Calendar c = Calendar.getInstance();        c.setTime(date);        int i = c.get(Calendar.DAY_OF_MONTH);        if (i < 11)            return "上旬";        else if (i < 21)            return "中旬";        else            return "下旬";    }    /**     * 目前日期,所屬月,三旬: 每旬多少天     */    public static int getTenDayscnt(String specifiedDay) {        Date date = null;        try {            date = new SimpleDateFormat("yyyyMMdd").parse(specifiedDay);        } catch (ParseException e) {            e.printStackTrace();        }        Calendar c = Calendar.getInstance();        c.setTime(date);        int i = c.get(Calendar.DAY_OF_MONTH);        if (i < 11){            return 10;        } else if (i < 21) {            return 10;        }else{            return  Integer.parseInt(getMonthEndTime(specifiedDay).substring(6,8)) - 20;        }    }    /**     * 目前日期,所屬季度     */    public static int getQuarter(String specifiedDay) {        Date date = null;        try {            date = new SimpleDateFormat("yyyyMMdd").parse(specifiedDay);        } catch (ParseException e) {            e.printStackTrace();        }        Calendar c = Calendar.getInstance();        c.setTime(date);        int currentMonth = c.get(Calendar.MONTH) + 1;        int dt = 0;        try {            if (currentMonth <= 3)                dt = 1;            else if (currentMonth <= 6)                dt = 2;            else if (currentMonth <= 9)                dt = 3;            else if (currentMonth <= 12)                dt = 4;        } catch (Exception e) {            e.printStackTrace();        }        return dt;    }    /**     * 目前日期,所屬季度天數     */    public static int getQuarterCntday(String specifiedDay) {        int cnt_d = 0;        int q = getQuarter(specifiedDay);        if (q == 1) {            specifiedDay = specifiedDay.substring(0,4);            specifiedDay = getMonthEndTime(specifiedDay + "0201");            cnt_d = getMDaycnt(specifiedDay);            cnt_d = 31 + cnt_d + 31;        }else if (q == 2) {            cnt_d = 30 + 31 + 30;        }else if (q == 3) {            cnt_d = 31 + 31 + 30;        }else if (q == 4) {            cnt_d = 31 + 30 + 31;        }        return cnt_d;    }    /**     * 目前日期,所屬年多少天     */    public static int getYearCntday(String specifiedDay) {        int cnt_d;        specifiedDay = specifiedDay.substring(0,4);        specifiedDay = getMonthEndTime(specifiedDay + "0201");        cnt_d = getMDaycnt(specifiedDay);        cnt_d = 31 + cnt_d + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30 + 31;        return cnt_d;    }    /**     * 目前日期,所屬前半年 或 後半年 天資料     */    public static int getHyearCntday(String specifiedDay) {        int cnt_d = 0;        int q = getHalfYear(specifiedDay);        if (q == 1) {            specifiedDay = specifiedDay.substring(0,4);            specifiedDay = getMonthEndTime(specifiedDay + "0201");            cnt_d = getMDaycnt(specifiedDay);            cnt_d = 31 + cnt_d + 31 + 30 + 31 + 30;        }else if (q == 2) {            cnt_d = 31 + 31 + 30 + 31 + 30 + 31;        }        return cnt_d;    }    /**     * 目前日期,所屬前半年 或 後半年     * 傳回:1\2     */    public static int getHalfYear(String specifiedDay) {        int dt = getQuarter(specifiedDay);        if (dt <= 2) {            dt = 1;        }else {            dt = 2;        }        return dt;    }    /**     * 目前日期,所屬前半年 或 後半年     * 傳回:上半年\下半年     */    public static String getHalfYears(String specifiedDay) {        int dt = getQuarter(specifiedDay);        if (dt<=2) {            return "上半年";        }        return "下半年";    }    /**     * 目前日期,所屬月的開始時間     */    public static String getMonthStartTime(String specifiedDay) {        Date date = null;        try {            date = new SimpleDateFormat("yyyyMMdd").parse(specifiedDay);        } catch (ParseException e) {            e.printStackTrace();        }        Calendar c = Calendar.getInstance();        c.setTime(date);        String dt = "";        try {            c.set(Calendar.DATE, 1);            dt = new SimpleDateFormat("yyyyMMdd").format(c.getTime());        } catch (Exception e) {            e.printStackTrace();        }        return dt;    }    /**     * 目前日期,所屬月的結束時間     */    public static String getMonthEndTime(String specifiedDay) {        Date date = null;        try {            date = new SimpleDateFormat("yyyyMMdd").parse(specifiedDay);        } catch (ParseException e) {            e.printStackTrace();        }        Calendar c = Calendar.getInstance();        c.setTime(date);        String dt = "";        try {            c.set(Calendar.DATE, 1);            c.add(Calendar.MONTH, 1);            c.add(Calendar.DATE, -1);            dt = new SimpleDateFormat("yyyyMMdd").format(c.getTime());        } catch (Exception e) {            e.printStackTrace();        }        return dt;    }    /**     * 目前日期,所屬周的第一天(周一),的日期     */    public static String getWeekStartTime(String specifiedDay,String pattern) {        Date date = null;        try {            date = new SimpleDateFormat("yyyyMMdd").parse(specifiedDay);        } catch (ParseException e) {            e.printStackTrace();        }        Calendar c = Calendar.getInstance();        c.setTime(date);        try {            SimpleDateFormat shortSdf = new SimpleDateFormat("yyyy-MM-dd");            SimpleDateFormat longSdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");            int weekday = c.get(Calendar.DAY_OF_WEEK) - 2;            c.add(Calendar.DATE, -weekday);            c.setTime(longSdf.parse(shortSdf.format(c.getTime()) + " 00:00:00.000"));        } catch (Exception e) {            e.printStackTrace();        }        return new SimpleDateFormat(pattern).format(c.getTime());    }    /**     * 目前日期,所屬周的最後一天(周日),的日期     */    public static String getWeekEndTime(String specifiedDay,String pattern) {        Date date = null;        try {            date = new SimpleDateFormat("yyyyMMdd").parse(specifiedDay);        } catch (ParseException e) {            e.printStackTrace();        }        Calendar c = Calendar.getInstance();        c.setTime(date);        try {            SimpleDateFormat shortSdf = new SimpleDateFormat("yyyy-MM-dd");            SimpleDateFormat longSdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");            int weekday = c.get(Calendar.DAY_OF_WEEK);            c.add(Calendar.DATE, 8 - weekday);            c.setTime(longSdf.parse(shortSdf.format(c.getTime()) + " 23:59:59.999"));        } catch (Exception e) {            e.printStackTrace();        }        return new SimpleDateFormat(pattern).format(c.getTime());    }    /**     * 目前日期,所屬旬結束時間     */    public static String getTenDayEndTime(String specifiedDay) {        Date date = null;        try {            date = new SimpleDateFormat("yyyyMMdd").parse(specifiedDay);        } catch (ParseException e) {            e.printStackTrace();        }        int ten = getTenDay(specifiedDay);        if (ten == 1) {            SimpleDateFormat df = new SimpleDateFormat("yyyyMM10");            return df.format(date);        } else if (ten == 2) {            SimpleDateFormat df = new SimpleDateFormat("yyyyMM20");            return df.format(date);        } else {            return getMonthEndTime(specifiedDay);        }    }    /**     * 目前日期,所屬季度的開始時間     */    public static String getQuarterStartTime(String specifiedDay)  {        Date date = null;        try {            date = new SimpleDateFormat("yyyyMMdd").parse(specifiedDay);        } catch (ParseException e) {            e.printStackTrace();        }        Calendar c = Calendar.getInstance();        c.setTime(date);        int currentMonth = c.get(Calendar.MONTH) + 1;        String dt = "";        try {            if (currentMonth <= 3)                c.set(Calendar.MONTH, 0);            else if (currentMonth <= 6)                c.set(Calendar.MONTH, 3);            else if (currentMonth <= 9)                c.set(Calendar.MONTH, 6);            else if (currentMonth <= 12)                c.set(Calendar.MONTH, 9);            c.set(Calendar.DATE, 1);            dt =  new SimpleDateFormat("yyyyMMdd").format(c.getTime());        } catch (Exception e) {            e.printStackTrace();        }        return dt;    }    /**     * 目前日期,所屬季度的結束時間     */    public static String getQuarterEndTime(String specifiedDay) {        Date date = null;        try {            date = new SimpleDateFormat("yyyyMMdd").parse(specifiedDay);        } catch (ParseException e) {            e.printStackTrace();        }        Calendar c = Calendar.getInstance();        c.setTime(date);        int currentMonth = c.get(Calendar.MONTH) + 1;        String dt = "";        try {            if (currentMonth <= 3) {                c.set(Calendar.MONTH, 2);                c.set(Calendar.DATE, 31);            } else if (currentMonth <= 6) {                c.set(Calendar.MONTH, 5);                c.set(Calendar.DATE, 30);            } else if (currentMonth <= 9) {                c.set(Calendar.MONTH, 8);                c.set(Calendar.DATE, 30);            } else if (currentMonth <= 12) {                c.set(Calendar.MONTH, 11);                c.set(Calendar.DATE, 31);            }            dt = new SimpleDateFormat("yyyyMMdd").format(c.getTime());        } catch (Exception e) {            e.printStackTrace();        }        return dt;    }    /**     * 目前日期,是否節假日     *     */    public static int getJjr(String specifiedDay,String j) {        String[] f = { "0101" , "0405" , "0501" , "1001" };        String sub = specifiedDay.substring(4,8);        if (!"2".equals(j)) {//0工作日、1周末、2節日、3調休            for (String s : f) {                if (s.equals(sub)) {                    return 1;                }            }            return 0;        }else{            return 1;        }    }    /**     * 目前日期,節假日名稱     * http://timor.tech/api/holiday?date=20190505     */    public static String getJjrname(String specifiedDay) {        String[] f = { "0101" , "0405" , "0501" , "1001" };        String[] f1 = { "元旦節" , "清明節" , "勞動節" , "國慶節" };        String sub = specifiedDay.substring(4,8);        for (int k = 0 ; k < f.length ; k++ ) {            if(f[k].equals(sub)){                return f1[k];            }        }        return "-";    }    /**     * 目前日期,是否上班     */    public static int iswork(String specifiedDay,String j)  {        String[] f = { "0101" , "0405" , "0501" , "1001" };        String sub = specifiedDay.substring(4,8);        //0工作日、1周末、2節日、3調休        if ("0".equals(j) || "3".equals(j)) {            for (String s : f) {                if (s.equals(sub)) {                    return 0;                }            }            return 1;        } else {            return 0;        }    }    /**     * 循環計算     */    public static void anyDate(String start_day, String end_day, int i) throws Exception {        while (true) {            start_day = getNowDate(start_day);            if (start_day.equals(end_day)) {                break;            } else {                System.out.println(start_day);                StringBuffer rel = new StringBuffer();                String year_s = start_day.substring(0, 4);                String month_s = start_day.substring(4, 6);                String day_long_desc = yyyymmdd(start_day,"yyyy年MM月dd日");// 日完整名稱 yyyy年MM月dd日                String day_medium_desc =  yyyymmdd(start_day,"dd日");// 日中等長度名 dd日                String day_short_desc =  yyyymmdd(start_day,"yyyy-MM-dd");// 日短名 yyyy-MM-dd                int  ws = getWeekofYear(start_day);                String n_year = year_s;                if (ws == 1 && "12".equals(month_s)){                    n_year = (Integer.parseInt(year_s) + 1) + "";                }                String week_code = n_year + "W" + ws;// 周代碼  2019W02                String week_long_desc = n_year + "年第" + ws + "周";          //周完整名稱String  2019年第02周                String week_medium_desc  = "第" + ws + "周";                  //周中等長度名String  第02周                String week_short_desc = n_year + "-W" + ws ;               //周短名String                String week_name =  yyyymmdd(start_day,"EEEE");                //旬代碼String                String ten_day_code =year_s + month_s + "X" + getTenDay(start_day);                //旬完整名稱String                String ten_day_long_desc = year_s +"年"+ month_s + "月" + getTenDays(start_day);                //旬中等長度名String                String ten_day_medium_desc = getTenDays(start_day);                //旬短名String                String ten_day_short_desc = year_s +"-"+ month_s + "-X" + getTenDay(start_day);                //月代碼String                String month_code = year_s + month_s;                //月完整名稱String                String month_long_desc = year_s + "年" + month_s + "月";                //月中等長度名String                String month_medium_desc = month_s + "月";                //月短名String                String month_short_desc = year_s + "-" + month_s;                //季代碼String                String qu=getQuarter(start_day)+ "";                String quarter_code = year_s + "Q"+ qu;                //季完整名稱String                String quarter_long_desc = year_s + "年第" + qu + "季度";                //季中等長度名String                String quarter_medium_desc = "第" + qu + "季度";                //季短名String                String quarter_short_desc = year_s + "-Q" + qu;                //半年代碼String                String half_year_code =year_s + "H" + getHalfYear(start_day);                //半年完整名稱String                String half_long_desc = year_s + "年" +getHalfYears(start_day);                //半年中等長度名String                String half_medium_desc = getHalfYears(start_day);                //半年短名String                String half_short_desc = year_s + "-H" + getHalfYear(start_day);                //年完整名稱String                String year_long_desc = year_s + "年";                //年中等長度名String                String year_medium_desc = year_s + "年";                //年短名String                //全部時間代碼String                String all_time_code = "ALL";                //全部時間名稱String                String all_time_desc = "ALL_TIME";                //日時間跨天String                String day_timespan = "1";                //周跨天數String                String week_timespan = "7";                //周結束日期String                String week_end_date = getWeekEndTime(start_day,"yyyy-MM-dd");;                //旬跨天數String                String ten_day_timespan = getTenDayscnt(start_day)+"";                //旬結束日期String                String ten_day_end_date = getTenDayEndTime(start_day);                //月跨天數String                String month_timespan = getMonthEndTime(start_day).substring(6,8)+"";                //月結束日期String                String month_end_date = getMonthEndTime(start_day);                //季跨天數String                String quarter_timespan = getQuarterCntday(start_day)+"";                //季結束日期String                String quarter_end_date = getQuarterEndTime(start_day);                //半年跨天數String                String half_year_timespan = getHyearCntday(start_day)+"";                //半年結束日期String                String half_year_end_date = year_s + "0630";                //年跨天數String                String year_timespan = getYearCntday(start_day)+"";                //年結束日期String                String year_end_date = year_s + "1231";                //周開始日期String                String week_start_date = getWeekStartTime(start_day,"yyyyMMdd");                //月開始時間String                String month_start_date = getMonthStartTime(start_day);                //季度開始時間String                String quarter_start_date = getQuarterStartTime(start_day);                //是否工作日:1.是 0.否                String workday_flag = getWeekDay(start_day) <= 5 ? "1" : "0" ;                //是否周末:1.是 0.否                String weekend_flag = getWeekDay(start_day) > 5 ? "1" : "0";                //是否為節假日 1:是,0:否 -: 未知                String holiday_flag = "-";//暫時預設未知,或者調用接口處理                //節假日名稱String                String holiday_name = getJjrname(start_day);                //是否上班 1:是,0:否 -: 未知                String is_workday = "-";//暫時預設未知,或者調用接口處理                String last_week_day = getWeekEndTime(start_day,"yyyy-MM-dd"); //周的最後一天String                String last_month_day = yyyymmdd(getMonthEndTime(start_day),"yyyy-MM-dd"); //月的最後一天String                Date d = new Date();                String load_time = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(d);//加載時間                rel.append(i).append("\t");                rel.append(start_day).append("\t");                rel.append(day_long_desc).append("\t");                rel.append(day_medium_desc).append("\t");                rel.append(day_short_desc).append("\t");                rel.append(week_code).append("\t");                rel.append(week_long_desc).append("\t");                rel.append(week_medium_desc).append("\t");                rel.append(week_short_desc).append("\t");                rel.append(week_name).append("\t");                rel.append(ten_day_code).append("\t");                rel.append(ten_day_long_desc).append("\t");                rel.append(ten_day_medium_desc).append("\t");                rel.append(ten_day_short_desc).append("\t");                rel.append(month_code).append("\t");                rel.append(month_long_desc).append("\t");                rel.append(month_medium_desc).append("\t");                rel.append(month_short_desc).append("\t");                rel.append(quarter_code).append("\t");                rel.append(quarter_long_desc).append("\t");                rel.append(quarter_medium_desc).append("\t");                rel.append(quarter_short_desc).append("\t");                rel.append(half_year_code).append("\t");                rel.append(half_long_desc).append("\t");                rel.append(half_medium_desc).append("\t");                rel.append(half_short_desc).append("\t");                rel.append(year_s).append("\t");                rel.append(year_long_desc).append("\t");                rel.append(year_medium_desc).append("\t");                rel.append(year_s).append("\t");                rel.append(all_time_code).append("\t");                rel.append(all_time_desc).append("\t");                rel.append(day_timespan).append("\t");                rel.append(start_day).append("\t");                rel.append(week_timespan).append("\t");                rel.append(week_end_date).append("\t");                rel.append(ten_day_timespan).append("\t");                rel.append(ten_day_end_date).append("\t");                rel.append(month_timespan).append("\t");                rel.append(month_end_date).append("\t");                rel.append(quarter_timespan).append("\t");                rel.append(quarter_end_date).append("\t");                rel.append(half_year_timespan).append("\t");                rel.append(half_year_end_date).append("\t");                rel.append(year_timespan).append("\t");                rel.append(year_end_date).append("\t");                rel.append(week_start_date).append("\t");                rel.append(month_start_date).append("\t");                rel.append(quarter_start_date).append("\t");                rel.append(workday_flag).append("\t");                rel.append(weekend_flag).append("\t");                rel.append(holiday_flag).append("\t");                rel.append(holiday_name).append("\t");                rel.append(is_workday).append("\t");                rel.append(last_week_day).append("\t");                rel.append(last_month_day).append("\t");                rel.append(load_time);                writeFile(rel);            }            i++ ;        }    }    // 寫入檔案(增量寫入,不會覆寫原有内容)    public static void writeFile(StringBuffer rel) {        // 本地存放路徑        String file_path = "d:/date_dim.txt" ;        FileWriter fw_s = null;        PrintWriter pw_s = null;        try {            File f_hive = new File(file_path);/* 生成路徑 */            fw_s = new FileWriter(f_hive, true);            pw_s = new PrintWriter(fw_s);            pw_s.println(rel);            pw_s.flush();            fw_s.flush();        } catch (IOException e) {            e.printStackTrace();        } finally {            if (pw_s != null)                pw_s.close();            if (fw_s != null) {                try {                    fw_s.close();                } catch (IOException e) {                    e.printStackTrace();                }            }        }    }    // 測試    public static void main(String[] args) throws Exception {        String start_day = "20191031";/*開始日期*/        String end_day = "20201231";/*結束日期*/        anyDate(start_day,end_day,1);//開始時間-結束時間-續跑ID(便于後續增量生成)        System.out.println("success !");    }}
           
日期格式化為yyyymmdd_小白篇(二十):通過Java生成日期次元表

說明:最後将生成的資料檔案導入至建立的hive日期次元表中即可。記得驗證下資料哦^_^

日期格式化為yyyymmdd_小白篇(二十):通過Java生成日期次元表

04

小結

其實生成日期次元表是很簡單的。可以通過很多程式設計語言來實作,sql\python\java...等等都可以。大家可以根據各自的習慣來處理。有好的建議,可以留言告知下哦,互相學習嘛。

如果大家喜歡可關注公衆号,感謝!

日期格式化為yyyymmdd_小白篇(二十):通過Java生成日期次元表