Commit d7139ffc by gechengyang

提交业务要素转ISO的测试程序

parent f6ca4161
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";
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment