luxiaotao1123
2024-09-09 ea7e27ff897da58be02c14a6f18adbf387a56ee1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
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);
    }
 
}