package com.zy.acs.gateway.domain; import com.zy.acs.gateway.utils.ValidUtil; import io.netty.buffer.ByteBuf; import java.time.Instant; import java.time.LocalDateTime; import java.time.ZoneId; import java.util.Date; public class PacDateTime { private int year; private int month; private int day; private int hour; private int min; private int sec; public int getYear() { return year; } public PacDateTime setYear(int year) { this.year = year; return this; } public int getMonth() { return month; } public PacDateTime setMonth(int month) { this.month = month; return this; } public int getDay() { return day; } public PacDateTime setDay(int day) { this.day = day; return this; } public int getHour() { return hour; } public PacDateTime setHour(int hour) { this.hour = hour; return this; } public int getMin() { return min; } public PacDateTime setMin(int min) { this.min = min; return this; } public int getSec() { return sec; } public PacDateTime setSec(int sec) { this.sec = sec; return this; } public boolean checkDateTime() { return ValidUtil.rangeValid(this.year, 0, 99) && ValidUtil.rangeValid(this.month, 1, 12) && ValidUtil.rangeValid(this.day, 1, 31) && ValidUtil.rangeValid(this.hour, 0, 23) && ValidUtil.rangeValid(this.min, 0, 59) && ValidUtil.rangeValid(this.sec, 0, 59); } public PacDateTime write2Buf(ByteBuf buf) { buf.writeByte(this.year & 0xFF) .writeByte(this.month & 0xFF) .writeByte(this.day & 0xFF) .writeByte(this.hour & 0xFF) .writeByte(this.min & 0xFF) .writeByte(this.sec & 0xFF); return this; } public static PacDateTime readFromBuf(ByteBuf buf) { PacDateTime newDateTime = new PacDateTime() .setYear(buf.readUnsignedByte()) .setMonth(buf.readUnsignedByte()) .setDay(buf.readUnsignedByte()) .setHour(buf.readUnsignedByte()) .setMin(buf.readUnsignedByte()) .setSec(buf.readUnsignedByte()); if (newDateTime.checkDateTime()) { return newDateTime; } else { throw new RuntimeException("日期时间不正确"); } } public static PacDateTime readFromTime(long timestamp) { Instant instant = Instant.ofEpochMilli(timestamp); LocalDateTime localDateTime = LocalDateTime.ofInstant(instant, ZoneId.systemDefault()); PacDateTime newDateTime = new PacDateTime() .setYear(localDateTime.getYear() - 2000) .setMonth(localDateTime.getMonthValue()) .setDay(localDateTime.getDayOfMonth()) .setHour(localDateTime.getHour()) .setMin(localDateTime.getMinute()) .setSec(localDateTime.getSecond()); if (newDateTime.checkDateTime()) { return newDateTime; } else { throw new RuntimeException("日期时间不正确"); } } public static void main(String[] args) { PacDateTime time = PacDateTime.readFromTime(new Date().getTime()); System.out.println(time); } }