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;
} }

最新文章

  1. JSP复习整理(三)基本语法续
  2. 关于Visual Studio 2015中没有报表项(ReportViewer)的解决方案。
  3. Webform(Linq高级查、分页、组合查询)
  4. 纯CSS实现侧边栏/分栏高度自动相等
  5. Perl脚本学习经验(四)--Perl中sftp的使用
  6. 配置好maven后,设置tomcat:run运行程序
  7. C语言中volatilekeyword的作用
  8. linux下的oracle数据库和表空间的导入导出
  9. 关于Program Size
  10. 阿里云ECS每天一件事D3:挂载硬盘
  11. 【剑指offer】树的子结构
  12. javascript 六种数据类型(一)
  13. Opencv读取并获取视频属性
  14. one-hot编码
  15. 提取PPT文件中的Vba ProjectStg Compressed Atom。Extract PPT VBA Compress Stream
  16. Pinyin4j实战
  17. PyCharm‘s Project Deployment
  18. [c/c++]可变参数的使用
  19. Daily Scrum6 11.10
  20. 【Python3】【树形dp】uva10253 Series-Parallel Networks

热门文章

  1. RESTful API接口文档规范小坑
  2. asp.net三层架构增删改查
  3. 20190421-那些年使用过的CSS预处理器(CSS Preprocessor)之Sass and Less
  4. Netty 核心内容之 编解码器
  5. 关于Java 中跳出多重循环
  6. 简单的C#实体映射 AutoMapper
  7. Windows -- 使用批处理文件.bat删除旧文件
  8. July 12th, 2018. Thursday, Week 28th.
  9. .net问号的作用
  10. 转自阿里云邪-如何从小白成长为 Apache Committer?