java UTC时间格式化
2024-08-27 09:31:33
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;
} }
最新文章
- JSP复习整理(三)基本语法续
- 关于Visual Studio 2015中没有报表项(ReportViewer)的解决方案。
- Webform(Linq高级查、分页、组合查询)
- 纯CSS实现侧边栏/分栏高度自动相等
- Perl脚本学习经验(四)--Perl中sftp的使用
- 配置好maven后,设置tomcat:run运行程序
- C语言中volatilekeyword的作用
- linux下的oracle数据库和表空间的导入导出
- 关于Program Size
- 阿里云ECS每天一件事D3:挂载硬盘
- 【剑指offer】树的子结构
- javascript 六种数据类型(一)
- Opencv读取并获取视频属性
- one-hot编码
- 提取PPT文件中的Vba ProjectStg Compressed Atom。Extract PPT VBA Compress Stream
- Pinyin4j实战
- PyCharm‘s Project Deployment
- [c/c++]可变参数的使用
- Daily Scrum6 11.10
- 【Python3】【树形dp】uva10253 Series-Parallel Networks
热门文章
- RESTful API接口文档规范小坑
- asp.net三层架构增删改查
- 20190421-那些年使用过的CSS预处理器(CSS Preprocessor)之Sass and Less
- Netty 核心内容之 编解码器
- 关于Java 中跳出多重循环
- 简单的C#实体映射 AutoMapper
- Windows -- 使用批处理文件.bat删除旧文件
- July 12th, 2018. Thursday, Week 28th.
- .net问号的作用
- 转自阿里云邪-如何从小白成长为 Apache Committer?