一、最后结果集
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/abc3c3d74222346ae12ee32256675794.png)
二、代码
@GetMapping("/getTimeZoneList")
@ApiOperation(value = "获取时区列表", notes = "获取时区列表")
public Result<List<TimeZoneVO>> getTimeZoneList() {
return opLocationService.getTimeZoneList();
}
public List<TimeZoneVO> getTimeZoneList() {
List<TimeZoneInfoDTO> zoneList = TimeZoneInfoDTO.getTimeZoneList();
Map<String, List<TimeZoneInfoDTO>> timeZoneMap = zoneList.stream().collect(Collectors.groupingBy(TimeZoneInfoDTO::getTimeZone));
List<TimeZoneVO> timeZoneList = TimeZoneEnum.timeZoneList();
timeZoneList.forEach(item -> {
List<TimeZoneInfoDTO> infoList = timeZoneMap.get(item.getCode());
if (CollectionUtils.isNotEmpty(infoList)) {
item.setZoneIdList(infoList.stream().map(TimeZoneInfoDTO::getZoneId).collect(Collectors.toList()));
}
});
return timeZoneList;
}
TimeZoneVO
package com.autel.cloud.pile.base.vo;
import lombok.Data;
import java.util.List;
@Data
public class TimeZoneVO {
private static final long serialVersionUID = -8941450954477609031L;
private String code;
private String name;
private List<String> zoneIdList;
public TimeZoneVO(String code, String name, List<String> zoneIdList) {
this.code = code;
this.name = name;
this.zoneIdList = zoneIdList;
}
public String getCode() {
return this.code;
}
public String getName() {
return this.name;
}
public List<String> getZoneIdList() {
return this.zoneIdList;
}
public TimeZoneVO setCode(String code) {
this.code = code;
return this;
}
public TimeZoneVO setName(String name) {
this.name = name;
return this;
}
public TimeZoneVO setZoneIdList(List<String> zoneIdList) {
this.zoneIdList = zoneIdList;
return this;
}
public boolean equals(Object o) {
if (o == this) {
return true;
} else if (!(o instanceof TimeZoneVO)) {
return false;
} else {
TimeZoneVO other = (TimeZoneVO) o;
if (!other.canEqual(this)) {
return false;
} else {
label47:
{
Object this$code = this.getCode();
Object other$code = other.getCode();
if (this$code == null) {
if (other$code == null) {
break label47;
}
} else if (this$code.equals(other$code)) {
break label47;
}
return false;
}
Object this$name = this.getName();
Object other$name = other.getName();
if (this$name == null) {
if (other$name != null) {
return false;
}
} else if (!this$name.equals(other$name)) {
return false;
}
Object this$zoneIdList = this.getZoneIdList();
Object other$zoneIdList = other.getZoneIdList();
if (this$zoneIdList == null) {
if (other$zoneIdList != null) {
return false;
}
} else if (!this$zoneIdList.equals(other$zoneIdList)) {
return false;
}
return true;
}
}
}
protected boolean canEqual(Object other) {
return other instanceof TimeZoneVO;
}
public String toString() {
return "TimeZone(code=" + this.getCode() + ", name=" + this.getName() + ", zoneIdList=" + this.getZoneIdList() + ")";
}
}
TimeZoneInfoDTO
package com.autel.cloud.pile.base.dto;
import java.time.ZoneId;
import java.util.List;
import java.util.Set;
import java.util.TimeZone;
import java.util.stream.Collectors;
public class TimeZoneInfoDTO {
private String timeZone;
private String zoneId;
public static List getTimeZoneList() {
Set<String> zoneIds = ZoneId.getAvailableZoneIds();
return zoneIds.parallelStream().map((item) -> {
TimeZone timeZone = TimeZone.getTimeZone(item);
int time = timeZone.getRawOffset() / 3600000;
if (time > 12) {
time = 12;
}
String timeZoneStr = time >= 0 ? "UTC+" + time : "UTC" + time;
return builder().timeZone(timeZoneStr).zoneId(item).build();
}).collect(Collectors.toList());
}
public static TimeZoneInfoDTO.TimeZoneInfoDTOBuilder builder() {
return new TimeZoneInfoDTO.TimeZoneInfoDTOBuilder();
}
public String getTimeZone() {
return this.timeZone;
}
public String getZoneId() {
return this.zoneId;
}
public void setTimeZone(String timeZone) {
this.timeZone = timeZone;
}
public void setZoneId(String zoneId) {
this.zoneId = zoneId;
}
public boolean equals(Object o) {
if (o == this) {
return true;
} else if (!(o instanceof TimeZoneInfoDTO)) {
return false;
} else {
TimeZoneInfoDTO other = (TimeZoneInfoDTO) o;
if (!other.canEqual(this)) {
return false;
} else {
Object this$timeZone = this.getTimeZone();
Object other$timeZone = other.getTimeZone();
if (this$timeZone == null) {
if (other$timeZone != null) {
return false;
}
} else if (!this$timeZone.equals(other$timeZone)) {
return false;
}
Object this$zoneId = this.getZoneId();
Object other$zoneId = other.getZoneId();
if (this$zoneId == null) {
if (other$zoneId != null) {
return false;
}
} else if (!this$zoneId.equals(other$zoneId)) {
return false;
}
return true;
}
}
}
protected boolean canEqual(Object other) {
return other instanceof TimeZoneInfoDTO;
}
public String toString() {
return "TimeZoneInfoDTO(timeZone=" + this.getTimeZone() + ", zoneId=" + this.getZoneId() + ")";
}
public TimeZoneInfoDTO(String timeZone, String zoneId) {
this.timeZone = timeZone;
this.zoneId = zoneId;
}
public TimeZoneInfoDTO() {
}
public static class TimeZoneInfoDTOBuilder {
private String timeZone;
private String zoneId;
TimeZoneInfoDTOBuilder() {
}
public TimeZoneInfoDTO.TimeZoneInfoDTOBuilder timeZone(String timeZone) {
this.timeZone = timeZone;
return this;
}
public TimeZoneInfoDTO.TimeZoneInfoDTOBuilder zoneId(String zoneId) {
this.zoneId = zoneId;
return this;
}
public TimeZoneInfoDTO build() {
return new TimeZoneInfoDTO(this.timeZone, this.zoneId);
}
public String toString() {
return "TimeZoneInfoDTO.TimeZoneInfoDTOBuilder(timeZone=" + this.timeZone + ", zoneId=" + this.zoneId + ")";
}
}
}
TimeZoneEnum
package com.autel.cloud.pile.base.enums;
import cn.hutool.core.text.CharSequenceUtil;
import com.autel.cloud.pile.base.vo.TimeZoneVO;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.TimeZone;
public enum TimeZoneEnum {
UTC("UTC+0", "协调世界时"),
EAST_ONE("UTC+1", "东一区"),
EAST_TWO("UTC+2", "东二区"),
EAST_THREE("UTC+3", "东三区"),
EAST_FOUR("UTC+4", "东四区"),
EAST_FIVE("UTC+5", "东五区"),
EAST_SIX("UTC+6", "东六区"),
EAST_SEVEN("UTC+7", "东七区"),
EAST_EIGHT("UTC+8", "东八区"),
EAST_NINE("UTC+9", "东九区"),
EAST_TEN("UTC+10", "东十区"),
EAST_ELEVEN("UTC+11", "东十一区"),
EAST_TWELVE("UTC+12", "东十二区"),
WEST_ONE("UTC-1", "西一区"),
WEST_TWO("UTC-2", "西二区"),
WEST_THREE("UTC-3", "西三区"),
WEST_FOUR("UTC-4", "西四区"),
WEST_FIVE("UTC-5", "西五区"),
WEST_SIX("UTC-6", "西六区"),
WEST_SEVEN("UTC-7", "西七区"),
WEST_EIGHT("UTC-8", "西八区"),
WEST_NINE("UTC-9", "西九区"),
WEST_TEN("UTC-10", "西十区"),
WEST_ELEVEN("UTC-11", "西十一区"),
WEST_TWELVE("UTC-12", "西十二区");
private String code;
private String name;
public int getHour() {
return 0;
}
public static String getText(String code) {
if (CharSequenceUtil.isBlank(code)) {
return "";
} else {
TimeZoneEnum[] var1 = values();
int var2 = var1.length;
for (int var3 = 0; var3 < var2; ++var3) {
TimeZoneEnum timeZoneEnum = var1[var3];
if (Objects.equals(code, timeZoneEnum.code)) {
return timeZoneEnum.name;
}
}
return "";
}
}
public static List<TimeZoneVO> timeZoneList() {
List<TimeZoneVO> zoneList = new ArrayList();
TimeZoneEnum[] var1 = values();
int var2 = var1.length;
List<TimeZone> temp = new ArrayList();
for (int var3 = 0; var3 < var2; ++var3) {
TimeZoneEnum timeZoneEnum = var1[var3];
zoneList.add(new TimeZoneVO(timeZoneEnum.code, timeZoneEnum.name, null));
}
return zoneList;
}
public String getCode() {
return this.code;
}
public String getName() {
return this.name;
}
private TimeZoneEnum(String code, String name) {
this.code = code;
this.name = name;
}
}