无关风月
2024-09-05 a3aa8bd77b4a9a74601238cc467cfd115b0821b6
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
package com.ruoyi.integration.iotda.utils.tools;
 
import javax.xml.datatype.DatatypeConfigurationException;
import javax.xml.datatype.DatatypeFactory;
import javax.xml.datatype.XMLGregorianCalendar;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.TimeZone;
 
public class CP56Time2aConverter {
 
    public static String convertToCP56Time2a(Date date) {
        try {
            DatatypeFactory datatypeFactory = DatatypeFactory.newInstance();
            GregorianCalendar calendar = new GregorianCalendar();
            calendar.setTime(date);
            TimeZone timeZone = calendar.getTimeZone();
            XMLGregorianCalendar xmlGregorianCalendar = datatypeFactory.newXMLGregorianCalendar(
                    calendar.get(Calendar.YEAR),
                    calendar.get(Calendar.MONTH)+1,
                    calendar.get(Calendar.DAY_OF_MONTH),
                    calendar.get(Calendar.HOUR_OF_DAY),
                    calendar.get(Calendar.MINUTE),
                    calendar.get(Calendar.SECOND),
                    calendar.get(Calendar.MILLISECOND),
                    (timeZone.getRawOffset() / (60 * 1000)));
            return xmlGregorianCalendar.toXMLFormat();
        } catch (DatatypeConfigurationException e) {
            throw new RuntimeException("Error creating DatatypeFactory", e);
        }
    }
 
    public static void main(String[] args) {
        Date now = new Date();
        String cp56Time2a = convertToCP56Time2a(now);
        System.out.println("CP56Time2a: " + cp56Time2a);
    }
}