大数据全系列 教程
1869个小节阅读:465k
JAVA全系列 教程
面向对象的程序设计语言
Python全系列 教程
Python3.x版本,未来主流的版本
人工智能 教程
顺势而为,AI创新未来
大厂算法 教程
算法,程序员自我提升必经之路
C++ 教程
一门通用计算机编程语言
微服务 教程
目前业界流行的框架组合
web前端全系列 教程
通向WEB技术世界的钥匙
大数据全系列 教程
站在云端操控万千数据
AIGC全能工具班
A A
White Night
xxxxxxxxxx
package cn.itbaizhan.tranformer.model;
import cn.itbaizhan.common.DateEnum;
import cn.itbaizhan.util.TimeUtil;
import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;
import java.util.Calendar;
import java.util.Date;
import java.util.Objects;
public class DateDimension extends BaseDimension{
private int id;
private int year;
private int season;
private int month;
private int week;
private int day;
private String type;
private Date calendar;
public DateDimension() {super();}
public DateDimension(int year, int season, int month, int week, int day, String type) {
super();
this.year = year;
this.season = season;
this.month = month;
this.week = week;
this.day = day;
this.type = type;
}
public DateDimension(int year, int season, int month, int week, int day, String type, Date calendar) {
this(year,season,month,week,day,type);
this.calendar = calendar;
}
public DateDimension(int id,int year, int season, int month, int week, int day, String type, Date calendar) {
this(year,season,month,week,day,type,calendar);
this.id = id;
}
public int compareTo(BaseDimension o) {
if(this==o){
return 0;
}
DateDimension that = (DateDimension) o;
int tmp = Integer.compare(this.id,that.getId());
if(tmp!=0){
return tmp;
}
tmp = Integer.compare(this.year,that.getYear());
if(tmp!=0){
return tmp;
}
tmp = Integer.compare(this.season,that.getSeason());
if(tmp!=0){
return tmp;
}
tmp = Integer.compare(this.month,that.getMonth());
if(tmp!=0){
return tmp;
}
tmp = Integer.compare(this.week,that.getWeek());
if(tmp!=0){
return tmp;
}
tmp = Integer.compare(this.day,that.getDay());
if(tmp!=0){
return tmp;
}
tmp = this.type.compareTo(that.getType());
if(tmp!=0){
return tmp;
}
tmp = Long.compare(this.calendar.getTime(),that.getCalendar().getTime());
return tmp;
}
public void write(DataOutput out) throws IOException {
out.writeInt(this.id);
out.writeInt(this.year);
out.writeInt(this.season);
out.writeInt(this.month);
out.writeInt(this.week);
out.writeInt(this.day);
out.writeUTF(this.type);
out.writeLong(this.calendar.getTime());
}
public void readFields(DataInput in) throws IOException {
this.id = in.readInt();
this.year = in.readInt();
this.season = in.readInt();
this.month = in.readInt();
this.week = in.readInt();
this.day = in.readInt();
this.type = in.readUTF();
this.calendar.setTime(in.readLong());
}
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
DateDimension that = (DateDimension) o;
if(id!=that.getId()) return false;
if(year!=that.getYear()) return false;
if(season!=that.getSeason()) return false;
if(month!=that.getMonth()) return false;
if(week!=that.getWeek()) return false;
if(day!=that.getDay()) return false;
if(type==null){
if(that.getType()!=null){
return false;
}
}else if(!type.equals(that.getType())){
return false;
}
return true;
}
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime*result+id;
result = prime*result+year;
result = prime*result+season;
result = prime*result+month;
result = prime*result+week;
result = prime*result+day;
result = prime*result+((type==null)?0:type.hashCode());
return result;
}
/**根据type的类型,构造一个对应的时间维度的一个对象
* @param time 时间戳
* @param type 类型
* @return 封装了相关信息的DateDimension对象
*/
public static DateDimension buildDate(long time, DateEnum type){
Calendar calendar = Calendar.getInstance();
//清空之前的数据
calendar.clear();
int year = TimeUtil.getDateInfo(time,DateEnum.YEAR);
if(DateEnum.YEAR.equals(type)){
calendar.set(year,0,1);
return new DateDimension(year,0,0,0,0,type.name,calendar.getTime());
}
int season = TimeUtil.getDateInfo(time,DateEnum.SEASON);
if(DateEnum.SEASON.equals(type)){
int month = 3*season-2;
calendar.set(year,month-1,1);
return new DateDimension(year,season,0,0,0,type.name,calendar.getTime());
}
int month = TimeUtil.getDateInfo(time,DateEnum.MONTH);
if(DateEnum.MONTH.equals(type)){
calendar.set(year,month-1,1);
return new DateDimension(year,season,month,0,0,type.name,calendar.getTime());
}
int week = TimeUtil.getDateInfo(time,DateEnum.WEEK);
if(DateEnum.WEEK.equals(type)){
//获取指定时间戳所属周的第一天的时间戳 52*7=364
long firstDayOfWeek = TimeUtil.getFirstDayOfThisWeek(time);
year = TimeUtil.getDateInfo(time,DateEnum.YEAR);
season = TimeUtil.getDateInfo(time,DateEnum.SEASON);
month = TimeUtil.getDateInfo(time,DateEnum.MONTH);
week = TimeUtil.getDateInfo(time,DateEnum.WEEK);
if(month==12&&week==1){
week = 53;
}
return new DateDimension(year,season,month,week,0,type.name,new Date(firstDayOfWeek));
}
int day = TimeUtil.getDateInfo(time,DateEnum.DAY);
if(DateEnum.DAY.equals(type)){
calendar.set(year,month-1,day);
if(month==12&&week==1){
week = 53;
}
return new DateDimension(year,season,month,week,day,type.name,calendar.getTime());
}
throw new RuntimeException("不支持所要求的DateEnum类型来获取DateDimension对象"+type);
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public int getYear() {
return year;
}
public void setYear(int year) {
this.year = year;
}
public int getSeason() {
return season;
}
public void setSeason(int season) {
this.season = season;
}
public int getMonth() {
return month;
}
public void setMonth(int month) {
this.month = month;
}
public int getWeek() {
return week;
}
public void setWeek(int week) {
this.week = week;
}
public int getDay() {
return day;
}
public void setDay(int day) {
this.day = day;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public Date getCalendar() {
return calendar;
}
public void setCalendar(Date calendar) {
this.calendar = calendar;
}
}