Date本身没有时区概念

查看源码可以知道, Date对象中存储的是一个long型变量
这个变量的值为自1997-01-01 00:00:00(GMT)至Date对象记录时刻所经过的毫秒数
可以通过getTime()方法,获取这个变量值,且这个变量值和时区没有关系
全球任意地点同时执行new Date().getTime()获取到的值相同

Date源码

 private transient long fastTime;/*** Allocates a <code>Date</code> object and initializes it so that* it represents the time at which it was allocated, measured to the* nearest millisecond.** @see     java.lang.System#currentTimeMillis()*/public Date() {this(System.currentTimeMillis());}/*** Allocates a <code>Date</code> object and initializes it to* represent the specified number of milliseconds since the* standard base time known as "the epoch", namely January 1,* 1970, 00:00:00 GMT.** @param   date   the milliseconds since January 1, 1970, 00:00:00 GMT.* @see     java.lang.System#currentTimeMillis()*/public Date(long date) {fastTime = date;}/*** Returns the number of milliseconds since January 1, 1970, 00:00:00 GMT* represented by this <tt>Date</tt> object.** @return  the number of milliseconds since January 1, 1970, 00:00:00 GMT*          represented by this date.*/public long getTime() {return getTimeImpl();}private final long getTimeImpl() {if (cdate != null && !cdate.isNormalized()) {normalize();}return fastTime;}/*** Converts this <code>Date</code> object to a <code>String</code>* of the form:* <blockquote><pre>* dow mon dd hh:mm:ss zzz yyyy</pre></blockquote>* where:<ul>* <li><tt>dow</tt> is the day of the week (<tt>Sun, Mon, Tue, Wed,*     Thu, Fri, Sat</tt>).* <li><tt>mon</tt> is the month (<tt>Jan, Feb, Mar, Apr, May, Jun,*     Jul, Aug, Sep, Oct, Nov, Dec</tt>).* <li><tt>dd</tt> is the day of the month (<tt>01</tt> through*     <tt>31</tt>), as two decimal digits.* <li><tt>hh</tt> is the hour of the day (<tt>00</tt> through*     <tt>23</tt>), as two decimal digits.* <li><tt>mm</tt> is the minute within the hour (<tt>00</tt> through*     <tt>59</tt>), as two decimal digits.* <li><tt>ss</tt> is the second within the minute (<tt>00</tt> through*     <tt>61</tt>, as two decimal digits.* <li><tt>zzz</tt> is the time zone (and may reflect daylight saving*     time). Standard time zone abbreviations include those*     recognized by the method <tt>parse</tt>. If time zone*     information is not available, then <tt>zzz</tt> is empty -*     that is, it consists of no characters at all.* <li><tt>yyyy</tt> is the year, as four decimal digits.* </ul>** @return  a string representation of this date.* @see     java.util.Date#toLocaleString()* @see     java.util.Date#toGMTString()*/public String toString() {// "EEE MMM dd HH:mm:ss zzz yyyy";BaseCalendar.Date date = normalize();StringBuilder sb = new StringBuilder(28);int index = date.getDayOfWeek();if (index == BaseCalendar.SUNDAY) {index = 8;}convertToAbbr(sb, wtb[index]).append(' ');                        // EEEconvertToAbbr(sb, wtb[date.getMonth() - 1 + 2 + 7]).append(' ');  // MMMCalendarUtils.sprintf0d(sb, date.getDayOfMonth(), 2).append(' '); // ddCalendarUtils.sprintf0d(sb, date.getHours(), 2).append(':');   // HHCalendarUtils.sprintf0d(sb, date.getMinutes(), 2).append(':'); // mmCalendarUtils.sprintf0d(sb, date.getSeconds(), 2).append(' '); // ss// 注意: 这里涉及到时区TimeZone zi = date.getZone();if (zi != null) {sb.append(zi.getDisplayName(date.isDaylightTime(), TimeZone.SHORT, Locale.US)); // zzz} else {sb.append("GMT");}sb.append(' ').append(date.getYear());  // yyyyreturn sb.toString();}

getTime()获取毫秒数,获取到的毫秒数和格式化后时间的关系

这个变量的值为自1997-01-01 00:00:00(GMT)至Date对象记录时刻所经过的毫秒数
这个毫秒数和格式化后时间是模型和视图的关系, 时区(TimeZone)决定了同一模型展示成什么样的视图(格式化Date)

 Date date = new Date();System.out.println(date);System.out.println(date.getTime());// 输出Tue Dec 10 18:44:24 CST 20191575974664352

格式化Date对象成字符串, 涉及时区

不管是调用Date对象的toString方法, 还是使用SimpleDateFormat的format方法去格式化Date对象,或者使用parse解析字符串成Date对象都会涉及到时区,
也就是说Date对象没有时区概念, 但是格式化Date对象, 或者解析字符串成Date对象时, 是有时区概念的

toString

 Date date = new Date();// 默认是系统时区System.out.println(date);// 修改默认时区TimeZone.setDefault(TimeZone.getTimeZone("GMT"));System.out.println(date);// 输出Tue Dec 10 19:03:46 CST 2019Tue Dec 10 11:03:46 GMT 2019

SimpleDateFormat

 Date date = new Date();// 默认是系统时区SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");System.out.println(dateFormat.format(date));// 设置时区dateFormat.setTimeZone(TimeZone.getTimeZone("GMT+1:00"));System.out.println(dateFormat.format(date));// 输出2019-12-10 19:06:312019-12-10 12:06:31

解析字符串成Date对象, 涉及时区

将同一个时间字符串按照不同的时区来解析, 得到的Date对象值不一样
很好理解: 东八区8点当然和0时区8点不一样

 String dateStr = "2019-12-10 08:00:00";// 默认是系统时区SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");Date date1 = dateFormat.parse(dateStr);System.out.println(date1.getTime());// 设置时区dateFormat.setTimeZone(TimeZone.getTimeZone("GMT+1:00"));Date date2 = dateFormat.parse(dateStr);System.out.println(date2.getTime());// 输出15759360000001575961200000

将本地的时间字符串转换成另一时区的时间字符串

 String dateStr = "2019-12-10 08:00:00";// 按照本地时区解析字符串成DateSimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");Date date1 = dateFormat.parse(dateStr);// 使用目标时区格式化DatedateFormat.setTimeZone(TimeZone.getTimeZone("GMT+9:00"));System.out.println(dateFormat.format(date1));// 输出2019-12-10 09:00:00

java中的Date和时区相关推荐

  1. 探索 Java 中的 Date, Calendar, TimeZone 和Timestamp

    探索 Java 中的 Date, Calendar, TimeZone 和Timestamp java 2010-12-31 08:56:49 阅读8 评论0  字号:大中小 订阅 对象 宋晟 (sh ...

  2. Java中的时间与时区__java

         转:https://yq.aliyun.com/ziliao/245667      摘要: 本文讲的是Java中的时间与时区__java, 0. 前言: 时间格式: //世界标准时间,其中 ...

  3. Elasticsearch中的date与时区问题

    1 前言 本文主要讲解Elasticsearch中date类型数据的底层存储原理,以及对带时区日期字符串和不带时区的日期字符串如何在ES底层存储进行验证.对于直接存储Long类型时间戳,不作过多描述. ...

  4. Java中的时间与时区

    0. 前言: 时间格式: //世界标准时间,其中T表示时分秒的开始(或者日期与时间的间隔),Z表示这是一个世界标准时间 2017-12-13T01:47:07.081Z//本地时间,也叫不含时区信息的 ...

  5. java中使用 Date 和 SimpleDateFormat 类表示时间

    使用 Date 和 SimpleDateFormat 类表示时间 在程序开发中,经常需要处理日期和时间的相关数据,此时我们可以使用 java.util 包中的 Date 类.这个类最主要的作用就是获取 ...

  6. 关于JavaScript中的date和java中的date差14小时问题

    今天遇到一个问题,在java中获取的时间传到前台页面, 原时间是这样的:2016-11-10 15:29:11, 传到前台来是这样的:Thu Nov 10 15:29:11 CST 2016, 在js ...

  7. Java中的时间、时区和夏令时

    相关概念 时区 时区是地球上的区域使用同一个时间定义.以前,人们通过观察太阳的位置(时角)决定时间,这就使得不同经度的地方的时间有所不同(地方时).1863年,首次使用时区的概念.时区通过设立一个区域 ...

  8. java中时间入数据库格式转换_数据库中字段类型为datetime,转换成java中的Date类型...

    数据类型对照 点击打开链接 JDBC: PreparedStatement ps = conn.prepareStatement(sql); ResultSet rs = ps.executeQuer ...

  9. java中对date的一些处理以及获取date

    在写项目的过程中,经常会遇到对日期的一些处理,或者对日期的获取 之前写的时候都是需要哪个百度查找,写上就没有下文了,这次我把自己遇到过得,写过的总结一下,相当于是一个记录 private final ...

  10. java中关于Date的用法

    先讲一讲Date中parse和format的用法: 看例子: public static void main(String[] args) {DateFormat dfat=new SimpleDat ...

最新文章

  1. oracle trunc()截断函数
  2. 调试Docker容器
  3. matlab仿真随机数的产生
  4. Windows下调试PostGreSQL源码第一步 - 下载和编译源码、构造VS工程
  5. 用上 RocketMQ,系统性能提升了 10 倍!
  6. mysql创建数据库时候同时创建表空间_MySQL 创建InnoDB表空间_编程学问网
  7. c语言 3个人比饭量大小,OpenJudge计算概论-比饭量【枚举法、信息数字化】
  8. c++ winpcap开发(3)
  9. 【干货】统计学思维导图
  10. mybaits 返回ListString
  11. mailR:利用R语言发邮件
  12. 5ecsgo正在发送客户端_MQTT X 桌面客户端使用指南
  13. vscode风格个人主页源码
  14. 斯坦福大学终身教授张首晟:区块链最核心的理念,必然是「 In Math We Trust 」
  15. 信数金服:物联网案例之工业物联网中故障预警与风险管理的规范性分析
  16. 绿纹龙的森林游记——UPC
  17. 钟汉良日记:百善孝为先,其它都靠边
  18. html载入3d模型,webGL3D模型的加载与使用
  19. CSS中background-attachment的介绍和用法
  20. UR5机械臂仿真环境搭建

热门文章

  1. 基础平台系列-1-第三方服务
  2. ssdp协议 upnp_SSDP协议编程 upnp设备查找方法
  3. C# log4net App.config 配置系统未能初始化问题
  4. 优趣短视频解析客户端小程序源代码
  5. 基于C语言的移位密码和仿射密码
  6. (附源码)Springboot宠物医院管理系统 毕业设计 180923
  7. 用ERStudio生成带注释的SQL,为每个column生成注释
  8. python中chardet库的安装和导入
  9. 超级搜索(Super search)
  10. Houdini 地形知识点