时间格式转换

迈不过友情╰ 2022-12-20 03:30 318阅读 0赞

时间格式转换


前言

提示:主要记录时间格式转换:
例如:2020-11-10 00:00:00 转 2020-11-10T00:00:00.000Z


提示:以下是本篇文章正文内容,下面案例可供参考

一、年月日时分秒转UTC格式

代码如下(示例):

  1. /**
  2. * 年月日时分秒转UTC格式方法
  3. * @param args
  4. */
  5. public static void main(String[] args) {
  6. SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  7. SimpleDateFormat utcTime = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
  8. Date date = new Date();
  9. String str = "2020-11-10 00:30:00";
  10. try {
  11. date = simpleDateFormat.parse(str);
  12. String format = utcTime.format(date);
  13. System.err.println("UTC时间格式"+format);
  14. } catch (ParseException e) {
  15. e.printStackTrace();
  16. }
  17. }

输出:UTC时间格式2020-11-10T00:30:00.000Z。

2.UTC格式转年月日时分秒

代码如下(示例):

  1. /**
  2. * UTC格式转年月日时分秒方法
  3. * @param args
  4. *
  5. */
  6. public static void main(String[] args) {
  7. SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  8. SimpleDateFormat utcTime = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
  9. Date date = new Date();
  10. String str = "2020-11-10T00:30:00.000Z";
  11. try {
  12. date = utcTime.parse(str);
  13. String format = simpleDateFormat.format(date);
  14. System.err.println("年月日时分秒时间格式:"+format);
  15. } catch (ParseException e) {
  16. e.printStackTrace();
  17. }
  18. }

输出:年月日时分秒时间格式:2020-11-10 00:30:00


发表评论

表情:
评论列表 (有 0 条评论,318人围观)

还没有评论,来说两句吧...

相关阅读