SwiftIsoUtil.java 3.61 KB
Newer Older
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
package com.brilliance;

import com.brilliance.swift.exception.SwiftException;
import com.brilliance.swift.util.StringUtil;

import javax.xml.datatype.DatatypeConfigurationException;
import javax.xml.datatype.DatatypeFactory;
import javax.xml.datatype.XMLGregorianCalendar;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.TimeZone;

public class SwiftIsoUtil {

    public static String mt_to_mxDateTime(String mtDateTime, int zoneOffset) {
        if (StringUtil.isEmpty(mtDateTime)) {
            throw new SwiftException("MT日期时间转MX日期时间失败:MT日期时间为空");
        }
        SimpleDateFormat mtFormat = new SimpleDateFormat("yyyyMMddHHmmZ");
        mtFormat.setLenient(false);
        Date mtDate;
        try {
            mtDate = mtFormat.parse(mtDateTime);
        } catch (ParseException e) {
            try {
                mtDate = mtFormat.parse("20" + mtDateTime);
            } catch (ParseException ex) {
                throw new SwiftException("MT日期时间转MX日期时间失败:MT日期时间(" + mtDateTime + ")格式不合法", ex);
            }
        }
        GregorianCalendar gregorianCalendar = new GregorianCalendar();
        gregorianCalendar.setTime(mtDate);
        XMLGregorianCalendar xmlGregorianCalendar;
        try {
            xmlGregorianCalendar = DatatypeFactory.newInstance().newXMLGregorianCalendar(gregorianCalendar);
        } catch (DatatypeConfigurationException e) {
            throw new SwiftException("MT日期时间转MX日期时间失败", e);
        }
        xmlGregorianCalendar.setTimezone(zoneOffset * 60);
        return xmlGregorianCalendar.toXMLFormat();
    }

    public static String dateTransfer(String mtDateTime) {
        if (StringUtil.isEmpty(mtDateTime)) {
            throw new SwiftException("MT日期时间转MX日期时间失败:MT日期时间为空");
        }
        String[] split = mtDateTime.split("[+-]", -1);
        if (split.length == 2) {
            SimpleDateFormat mtFormat = new SimpleDateFormat("yyyyMMddHHmm");
            mtFormat.setLenient(false);
            SimpleDateFormat mxFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:00");
            String mxDateTime = null;
            try {
                mxDateTime = mxFormat.format(mtFormat.parse(split[0]));
            } catch (ParseException e) {
                try {
                    mxDateTime = mxFormat.format(mtFormat.parse("20" + split[0]));
                } catch (ParseException ignore) {
                }
            }
            if (mxDateTime != null && split[1].matches("((0\\d)|(1[0-3]))([0-5]\\d)?")) {
                mxDateTime = mxDateTime + mtDateTime.charAt(mtDateTime.length() - split[1].length() - 1) + split[1].substring(0, 2) + ":";
                mxDateTime += (split[1].length() > 2 ? split[1].substring(2) : "00");
                return mxDateTime;
            }
        }
        throw new SwiftException("MT日期时间转MX日期时间失败:MT日期时间(" + mtDateTime + ")格式不合法");
    }


    public static String date2IsoDateTime(String dateStr) throws ParseException {

        SimpleDateFormat inputFormat = new SimpleDateFormat("yyyyMMddHHmmss");
        Date date = inputFormat.parse(dateStr);

        SimpleDateFormat outputFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.sss");
        outputFormat.setTimeZone(TimeZone.getTimeZone("GMT+8")); // 设置为东八区(北京时间)
        String formattedDate = outputFormat.format(date);

        System.out.println(formattedDate);
        return  formattedDate+"+00:00";
    }
}