import cn.hutool.core.date.DateField;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.fawvw.ms.po.DimDatePo;
import java.util.List;
import java.util.stream.Collectors;
public class DateGenerator {
private static final String DATE_STD_FORMAT = "yyyy-MM-dd";
private static final String DATE_TRIM_FORMAT = "yyyyMMdd";
private static final String DATE_CN_FORMAT = "yyyy年M月d日";
private static final String MONTH_STD_FORMAT = "yyyy-MM";
private static final String MONTH_TRIM_FORMAT = "yyyyMM";
private static final String MONTH_CN_FORMAT = "yyyy年M月";
private static final String YEAR_STD_FORMAT = "yyyy";
private static final String YEAR_CN_FORMAT = "yyyy年";
public static List<DimDatePo> generateDateDim() {
List<DateTime> dateTimes = DateUtil.rangeToList(DateUtil.date(-1577952000000L), DateUtil.date(), DateField.DAY_OF_MONTH);
return dateTimes.stream().map(dateTime -> {
//日
String dateStd = DateUtil.format(dateTime, DATE_STD_FORMAT);
String dateTrim = DateUtil.format(dateTime, DATE_TRIM_FORMAT);
String dateCn = DateUtil.format(dateTime, DATE_CN_FORMAT);
String beginOfDay = DateUtil.beginOfDay(dateTime).toString();
String endOfDay = DateUtil.endOfDay(dateTime).toString();
int age = DateUtil.ageOfNow(dateTime);
// Console.log("日: {} {} {} {} {} {}\n",
// dateStd, dateTrim, dateCn, age, beginOfDay, endOfDay);
//周
int weekOfMonth = DateUtil.weekOfMonth(dateTime);
int weekOfYear = DateUtil.weekOfYear(dateTime);
int dayOfWeek = DateUtil.dayOfWeek(dateTime);
String week = DateUtil.dayOfWeekEnum(dateTime).toString();
String beginDateOfWeek = DateUtil.format(DateUtil.beginOfWeek(dateTime), DATE_STD_FORMAT);
String endDateOfWeek = DateUtil.format(DateUtil.endOfWeek(dateTime), DATE_STD_FORMAT);
// Console.log("周: {} {} {} {} {} {}\n", weekOfMonth, weekOfYear, dayOfWeek, week, beginDateOfWeek, endDateOfWeek);
//月
String monthStd = DateUtil.format(dateTime, MONTH_STD_FORMAT);
String monthTrim = DateUtil.format(dateTime, MONTH_TRIM_FORMAT);
String monthCn = DateUtil.format(dateTime, MONTH_CN_FORMAT);
String monthEn = DateUtil.monthEnum(dateTime).toString();
String beginDateOfMonth = DateUtil.format(DateUtil.beginOfMonth(dateTime), DATE_STD_FORMAT);
String endDateOfMonth = DateUtil.format(DateUtil.endOfMonth(dateTime), DATE_STD_FORMAT);
// Console.log("月: {} {} {} {} {} {}\n", monthStd, monthTrim, monthCn, monthEn, beginDateOfMonth, endDateOfMonth);
//季度
String quarterId = DateUtil.yearAndQuarter(dateTime);
int quarterNum = DateUtil.quarter(dateTime);
String quarterEn = DateUtil.quarterEnum(dateTime).toString();
String quarterCn;
String quarterYearCn = quarterId.substring(0, 4) + "年第" + quarterId.substring(4, 5) + "季度";
String quarterYearEn = quarterId.substring(0, 4) + " " + quarterEn;
switch (quarterEn) {
case "Q1":
quarterCn = "第一季度";
break;
case "Q2":
quarterCn = "第二季度";
break;
case "Q3":
quarterCn = "第三季度";
break;
default:
quarterCn = "第四季度";
break;
}
String beginDateOfQuarter = DateUtil.format(DateUtil.beginOfQuarter(dateTime), DATE_STD_FORMAT);
String endDateOfQuarter = DateUtil.format(DateUtil.endOfQuarter(dateTime), DATE_STD_FORMAT);
// Console.log("季度: {} {} {} {} {} {} {} {}\n",
// quarterId, quarterNum, quarterEn, quarterCn, quarterYearCn, quarterYearEn, beginDateOfQuarter, endDateOfQuarter);
//年
String yearStd = DateUtil.format(dateTime, YEAR_STD_FORMAT);
String yearCn = DateUtil.format(dateTime, YEAR_CN_FORMAT);
String chineseZodiac = DateUtil.getChineseZodiac(Integer.parseInt(yearStd));
String beginDateOfYear = DateUtil.format(DateUtil.beginOfYear(dateTime), DATE_STD_FORMAT);
String endDateOfYear = DateUtil.format(DateUtil.endOfYear(dateTime), DATE_STD_FORMAT);
// Console.log("年: {} {} {} {} {}\n", yearStd, yearCn, chineseZodiac, beginDateOfYear, endDateOfYear);
DimDatePo po = DimDatePo.builder().date_std(dateStd).date_trim(dateTrim).date_cn(dateCn).begin_of_day(beginOfDay).end_of_day(endOfDay).age(age)
.week_of_month(weekOfMonth).week_of_year(weekOfYear).day_of_week(dayOfWeek).week(week).begin_date_of_week(beginDateOfWeek)
.end_date_of_week(endDateOfWeek)
.month_std(monthStd).month_trim(monthTrim).month_cn(monthCn).month_en(monthEn).begin_date_of_month(beginDateOfMonth)
.end_date_of_month(endDateOfMonth)
.quarter_id(quarterId).quarter_num(quarterNum).quarter_en(quarterEn).quarter_cn(quarterCn)
.quarter_year_en(quarterYearEn).quarter_year_cn(quarterYearCn).begin_date_of_quarter(beginDateOfQuarter)
.end_date_of_quater(endDateOfQuarter)
.year_std(yearStd).year_cn(yearCn).chinese_zodiac(chineseZodiac).begin_date_of_year(beginDateOfYear)
.end_date_of_year(endDateOfYear).build();
return po;
}).collect(Collectors.toList());
}
public static void main(String[] args) {
generateDateDim();
}
}
用java生成日期维度表
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- paste <(seq -f "\"Feb 28 %gweeks\"" 0 15 | xargs -n 1 dat...
- 在数据分析中,日期维表的作用有多重要,无需赘言。 日期维表通常不会在OLTP库存在,所以需要数据分析师生成。日期维...
- 使用方法:在PQ中新建一个【空查询】,然后在【视图】打开【高级编辑器】,清除原来的内容,输入以下内容,设置你的起始...