import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; import java.util.TimeZone; /** * @author 王睿 * @date 2019-01-24 14:32 */ public class TimeFormat { public static void main(String[] args) throws ParseException { String text = "2019-01-03T08:26:15.503162206Z"; text = "2019-01-03T08:26:15Z"; Date date = parseUTCText(text); System.out.println(date); } /** * @param text 时间字符串,格式支持两种 * 1、不包含毫秒值,如"2019-01-03T08:26:15Z"; * 2、支持任意位数的毫秒值:2019-01-03T08:26:15.503162206Z; * 转换出来的Date类型精度知道毫秒位 * @return * @throws ParseException */ public static Date parseUTCText(String text) throws ParseException { SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"); sdf.setTimeZone(TimeZone.getTimeZone("UTC")); if (text.indexOf(".") > -1) { String prefix = text.substring(0, text.indexOf(".")); String suffix = text.substring(text.indexOf(".")); if (suffix.length() >= 5) { suffix = suffix.substring(0, 4) + "Z"; } else { int len = 5 - suffix.length(); String temp = ""; temp += suffix.substring(0, suffix.length() - 1); for (int i = 0; i < len; i++) { temp += "0"; } suffix = temp + "Z"; } text = prefix + suffix; } else { text = text.substring(0, text.length() - 1) + ".000Z"; } Date date = sdf.parse(text); return date; } }
标签:UTC,01,java,suffix,text,2019,格式化,String From: https://www.cnblogs.com/lvjinlin/p/18128423