Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/330.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
基于时区Java/Groovy的日期时间转换_Java_Datetime_Groovy_Calendar_Date Format - Fatal编程技术网

基于时区Java/Groovy的日期时间转换

基于时区Java/Groovy的日期时间转换,java,datetime,groovy,calendar,date-format,Java,Datetime,Groovy,Calendar,Date Format,我在MST,我希望我的约会在PST。我设置了我想要的时区。 现在,如果我执行c.getTime()操作,我总是能获得服务器时间。 相反,我想要太平洋约会时间。请帮忙 如何获取指定时区中的日期时间对象 Calendar c= Calendar.getInstance(); TimeZone timezone= TimeZone.getTimeZone("PST"); c.setTimeZone(timezone) Java日期对象没有时区——它只是表示一个时间点 如果要将日期

我在MST,我希望我的约会在PST。我设置了我想要的时区。 现在,如果我执行
c.getTime()
操作,我总是能获得服务器时间。 相反,我想要太平洋约会时间。请帮忙 如何获取指定时区中的日期时间对象

   Calendar c= Calendar.getInstance();
   TimeZone timezone= TimeZone.getTimeZone("PST"); 
   c.setTimeZone(timezone)

Java日期对象没有时区——它只是表示一个时间点

如果要将日期格式化为时区,可以在DateFormat类中进行设置。例如:

    Date date = new Date ();
    DateFormat df = DateFormat.getDateTimeInstance();
    df.setTimeZone(TimeZone.getTimeZone("PST"));
    System.out.println(df.format(date));
    df.setTimeZone(TimeZone.getTimeZone("EST"));
    System.out.println(df.format(date));
将以PST显示时间,然后以EST显示时间。

或者,使用


您可以根据需要将默认时区设置为PST/MST,然后获取日期。如果可能的话,我会用测试方法来做

更新:java.Time类成功地完成了Joda Time项目。看


(a) 使用(或Java 8中内置的新JSR 310)。甚至不要考虑使用臭名昭著的java.util.Date/Calendar

(b) 你的问题不清楚。你对答案的评论谈到比较,但你在问题中没有提到比较


(c) 避免使用三个字母的时区缩写。请阅读Joda Time文档中的课堂弃用说明

(d) 避免默认时区。说你的意思。计算机的时区可以有意更改,也可以不更改

(e) 在StackOverflow中搜索“joda”,查找大量代码片段和示例

(f) 下面是一些Joda Time示例代码,让您开始学习

// © 2013 Basil Bourque. This source code may be used freely forever by anyone taking full responsibility for doing so.

// Specify your time zone rather than rely on default.
org.joda.time.DateTimeZone californiaTimeZone = org.joda.time.DateTimeZone.forID( "America/Los_Angeles" );
org.joda.time.DateTimeZone denverTimeZone = org.joda.time.DateTimeZone.forID( "America/Denver" );

org.joda.time.DateTime nowDenver = new org.joda.time.DateTime( denverTimeZone );
org.joda.time.DateTime nowCalifornia = nowDenver.toDateTime( californiaTimeZone );

// Same moment in the Universe’s timeline, but presented in the local context.
System.out.println( "nowDenver: " + nowDenver );
System.out.println( "nowCalifornia: " + nowCalifornia );
当运行时

nowDenver: 2013-11-21T18:12:49.372-07:00
nowCalifornia: 2013-11-21T17:12:49.372-08:00
约达时间

// Joda-Time - The popular alternative to Sun/Oracle's notoriously bad date, time, and calendar classes bundled with Java 7 and earlier.
// http://www.joda.org/joda-time/

// Joda-Time will become outmoded by the JSR 310 Date and Time API introduced in Java 8.
// JSR 310 was inspired by Joda-Time but is not directly based on it.
// http://jcp.org/en/jsr/detail?id=310

// By default, Joda-Time produces strings in the standard ISO 8601 format.
// https://en.wikipedia.org/wiki/ISO_8601

// About Daylight Saving Time (DST): https://en.wikipedia.org/wiki/Daylight_saving_time

// Time Zone list: http://joda-time.sourceforge.net/timezones.html

最近我自己也遇到了类似的问题,将时区设置为一个地区对我更有效(即不是EST/EDT,而是美国/纽约)。我试过EST,然后试着为EDT做夏令时补偿,结果证明这要容易得多。将您的时区设置为您想要的任何时区,然后使用Date对象创建一个新的日期,它将为该时区创建一个新的日期。然后,您可以使用format方法来获取时间戳,不管您喜欢什么

TimeZone.setDefault(TimeZone.getTimeZone("America/New_York"));
Date date = new Date();
timeStamp = date.format('yyyy-MM-dd HH:mm:ss.SSSZ');
System.out.println(timeStamp);
返回

"2019-07-25 17:09:23:626-0400"
tl;博士 看这个。(请注意,该站点上的系统时钟今天似乎慢了大约半小时。)

zdt.toString():2019-07-27T12:29:42.029531-07:00[美国/洛杉矶]

java.time 现代方法使用java 8和更高版本中内置的java.time类,这些类在JSR 310中定义

我在MST,我希望我的约会在PST。我设置了我想要的时区

永远不要在运行时依赖JVM的当前默认时区。作为程序员,您无法控制默认设置。因此,代码的结果可能会发生意外的变化

始终指定日期时间方法的可选时区参数

现在,如果我使用c.getTime(),我总是会得到服务器时间

学习不要考虑客户机时间或服务器时间,而是UTC时间。大多数业务逻辑、数据存储、数据交换和日志记录都应该在UTC中完成。将UTC视为唯一的真实时间™, 所有其他偏移/分区都只是变化

对于UTC,使用
Instant

Instant instant = Instant.now() ;  // Capture the current moment in UTC.
以标准ISO 8601格式生成表示该时刻的文本

String output = instant.toString() ;
相反,我想要太平洋约会时间。请帮助如何获取指定时区中的日期时间对象

   Calendar c= Calendar.getInstance();
   TimeZone timezone= TimeZone.getTimeZone("PST"); 
   c.setTimeZone(timezone)
您的术语(
Pacific
MST
PST
)都不是真正的时区

大陆/地区
的格式指定,例如
美国/蒙特利尔
非洲/卡萨布兰卡
,或
太平洋/奥克兰
。切勿使用2-4个字母的缩写,如
EST
IST
,因为它们不是真正的时区,也不是标准化的,甚至不是唯一的(!)

要从UTC调整到时区,请应用
ZoneId
以获取
zoneDateTime

ZoneId z = ZoneId.of( "America/Edmonton" ) ;  // https://time.is/Edmonton
ZonedDateTime zdt = instant.atZone( z ) ;
试试北美西海岸的一个时区

ZoneId z = ZoneId.of( "America/Los_Angeles" ) ;  // https://time.is/Los_Angeles
ZonedDateTime zdt = instant.atZone( z ) ;
要以ISO 8601以外的格式生成字符串,请使用
DateTimeFormatter
类。搜索堆栈溢出,因为这已经被讨论过很多次了


关于java.time 该框架内置于Java8及更高版本中。这些类取代了麻烦的旧日期时间类,例如,&

要了解更多信息,请参阅。并搜索堆栈溢出以获得许多示例和解释。规格是

该项目现已启动,建议迁移到类

您可以直接与数据库交换java.time对象。使用兼容的或更高版本。不需要字符串,也不需要
java.sql.*

从哪里获得java.time类

  • 、和更高版本-标准Java API的一部分,带有捆绑实现。
    • Java9添加了一些次要功能和修复
    • 大多数java.time功能都在中向后移植到Java6和Java7
    • 更高版本的Android捆绑包实现了java.time类

    • 对于早期的Android(感谢@Adrian,但这里我想将我的日期与太平洋时间日期进行比较。这就是我将其转换为PST日期时区的原因。再一次,日期没有时区——我假设您有一个日期字符串(例如,PST中的“1:00:00”),并且要将其与MST中的另一个日期进行比较?如果是,则应在解析该日期时设置时区,以便相应的日期对象包含正确的时间信息,然后比较逻辑将起作用:Calendar cal=Calendar.getInstance();SimpleDataFormat sdf=new SimpleDataFormat(“HH:mm:ss”);sdf.setTimeZone(TimeZone.getTimeZone(“PST”);cal.setTime(sdf.parse(“1:00:00”));//这是太平洋标准时间1:00:00PST代表的时区中性时间Hanks@tim_yates,但这里我想将我的日期与太平洋时间日期进行比较。这就是我将其转换为太平洋标准时间日期时区的原因。避免使用三个字母的时区代码。请阅读Joda time类上的不推荐注释。请澄清您的问题。下面的评论建议你的问题还有很多。仅供参考
      ZoneId z = ZoneId.of( "America/Montreal" ) ;  
      
      ZoneId z = ZoneId.of( "America/Edmonton" ) ;  // https://time.is/Edmonton
      ZonedDateTime zdt = instant.atZone( z ) ;
      
      ZoneId z = ZoneId.of( "America/Los_Angeles" ) ;  // https://time.is/Los_Angeles
      ZonedDateTime zdt = instant.atZone( z ) ;