Yat*_*oel 205 java timezone date java.util.date
我已经解析了java.util.Date
a,String
但是它将本地时区设置为date
对象的时区.
时区没有指定String
从中Date
解析.我想设置date
对象的特定时区.
我怎样才能做到这一点?
ZZ *_*der 293
使用DateFormat.例如,
SimpleDateFormat isoFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
isoFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
Date date = isoFormat.parse("2010-05-23T09:01:02");
Run Code Online (Sandbox Code Playgroud)
Jes*_*per 168
请注意,java.util.Date
对象本身不包含任何时区信息 - 您无法在Date
对象上设置时区.Date
对象包含的唯一内容是自"epoch" - 1970年1月1日00:00:00 UTC以来的毫秒数.
正如ZZ Coder所示,您可以在DateFormat
对象上设置时区,告诉它您要在哪个时区显示日期和时间.
Bas*_*que 78
...解析...从字符串...时区未指定...我想设置一个特定的时区
LocalDateTime.parse( "2018-01-23T01:23:45.123456789" ) // Parse string, lacking an offset-from-UTC and lacking a time zone, as a `LocalDateTime`.
.atZone( ZoneId.of( "Africa/Tunis" ) ) // Assign the time zone for which you are certain this date-time was intended. Instantiates a `ZonedDateTime` object.
Run Code Online (Sandbox Code Playgroud)
正如其他正确答案所述,java.util.Date没有时区†.它代表UTC/GMT(没有时区偏移).非常混乱,因为它的toString
方法在生成String表示时应用JVM的默认时区.
出于这个原因和其他许多原因,您应该避免使用内置的java.util.Date和.Calendar&java.text.SimpleDateFormat.众所周知,它们很麻烦.
而是使用与Java 8捆绑在一起的java.time包.
java.time类可以通过三种方式表示时间轴上的时刻:
Instant
)OffsetDateTime
带ZoneOffset
)ZonedDateTime
带ZoneId
)Instant
在java.time中,基本构建块是Instant
UTC时间线上的一个时刻.将Instant
对象用于大部分业务逻辑.
Instant instant = Instant.now();
Run Code Online (Sandbox Code Playgroud)
OffsetDateTime
申请一个ZoneOffset
来获得一个OffsetDateTime
.
ZoneOffset zoneOffset = ZoneOffset.of( "-04:00" );
OffsetDateTime odt = OffsetDateTime.ofInstant( instant , zoneOffset );
Run Code Online (Sandbox Code Playgroud)
ZonedDateTime
更好的是应用时区,偏移量以及处理夏令时(DST)等异常的规则.
申请一个ZoneId
来Instant
获得一个ZonedDateTime
.始终指定适当的时区名称.切勿使用3-4的缩写,如EST
或IST
既不是唯一的也不是标准化的.
ZoneId zoneId = ZoneId.of( "America/Montreal" );
ZonedDateTime zdt = ZonedDateTime.ofInstant( instant , zoneId );
Run Code Online (Sandbox Code Playgroud)
LocalDateTime
如果输入字符串缺少偏移量或区域的任何指示符,则解析为a LocalDateTime
.
如果您确定预期的时区,请指定a ZoneId
来生成a ZonedDateTime
.请参阅顶部的tl; dr部分中的上面的代码示例.
toString
在这三个类中的任何一个上调用方法以生成表示标准ISO 8601格式的日期时间值的String .该ZonedDateTime
班通过附加括号中的时区的名称,扩展了标准格式.
String outputInstant = instant.toString(); // Ex: 2011-12-03T10:15:30Z
String outputOdt = odt.toString(); // Ex: 2007-12-03T10:15:30+01:00
String outputZdt = zdt.toString(); // Ex: 2007-12-03T10:15:30+01:00[Europe/Paris]
Run Code Online (Sandbox Code Playgroud)
对于其他格式,请使用DateTimeFormatter
该类.通常最好让该类使用用户期望的人类语言和文化规范生成本地化格式.或者您可以指定特定格式.
该java.time框架是建立在Java 8和更高版本.这些类取代麻烦的老传统日期时间类,如java.util.Date
,Calendar
,和SimpleDateFormat
.
现在处于维护模式的Joda-Time项目建议迁移到java.time类.
要了解更多信息,请参阅Oracle教程.并搜索Stack Overflow以获取许多示例和解释.规范是JSR 310.
您可以直接与数据库交换java.time对象.使用符合JDBC 4.2或更高版本的JDBC驱动程序.不需要字符串,不需要课程.java.sql.*
从哪里获取java.time类?
该ThreeTen-额外项目与其他类扩展java.time.该项目是未来可能添加到java.time的试验场.您可以在此比如找到一些有用的类Interval
,YearWeek
,YearQuarter
,和更多.
虽然Joda-Time仍在积极维护,但其制造商告诉我们尽快迁移到java.time.我保留此部分作为参考,但我建议使用java.time
上面的部分.
在Joda-Time中,日期时间对象(DateTime
)确实知道其指定的时区.这意味着与UTC的偏移以及该时区的夏令时(DST)和其他此类异常的规则和历史记录.
String input = "2014-01-02T03:04:05";
DateTimeZone timeZone = DateTimeZone.forID( "Asia/Kolkata" );
DateTime dateTimeIndia = new DateTime( input, timeZone );
DateTime dateTimeUtcGmt = dateTimeIndia.withZone( DateTimeZone.UTC );
Run Code Online (Sandbox Code Playgroud)
调用该toString
方法以生成ISO 8601格式的String .
String output = dateTimeIndia.toString();
Run Code Online (Sandbox Code Playgroud)
Joda-Time还提供丰富的功能来生成各种其他String格式.
如果需要,您可以从Joda-Time DateTime转换为java.util.Date.
Java.util.Date date = dateTimeIndia.toDate();
Run Code Online (Sandbox Code Playgroud)
在"joda date"中搜索StackOverflow以查找更多示例,其中一些非常详细.
†其实有是嵌入在java.util.Date一个时区,用于一些内部功能(请参阅本答案的评论).但是此内部时区不作为属性公开,并且无法设置.这个内部时区是不被使用的一个toString
在生成日期时间值的字符串表示方法; 相反,JVM的当前默认时区是即时应用的.因此,作为简写,我们经常说"juDate没有时区".混乱?是.避免这些疲惫的旧课程的另一个原因.
小智 71
您还可以在JVM级别设置时区
Date date1 = new Date();
System.out.println(date1);
TimeZone.setDefault(TimeZone.getTimeZone("UTC"));
// or pass in a command line arg: -Duser.timezone="UTC"
Date date2 = new Date();
System.out.println(date2);
Run Code Online (Sandbox Code Playgroud)
输出:
Thu Sep 05 10:11:12 EDT 2013
Thu Sep 05 14:11:12 UTC 2013
Run Code Online (Sandbox Code Playgroud)
小智 9
如果必须只使用标准JDK类,则可以使用:
/**
* Converts the given <code>date</code> from the <code>fromTimeZone</code> to the
* <code>toTimeZone</code>. Since java.util.Date has does not really store time zome
* information, this actually converts the date to the date that it would be in the
* other time zone.
* @param date
* @param fromTimeZone
* @param toTimeZone
* @return
*/
public static Date convertTimeZone(Date date, TimeZone fromTimeZone, TimeZone toTimeZone)
{
long fromTimeZoneOffset = getTimeZoneUTCAndDSTOffset(date, fromTimeZone);
long toTimeZoneOffset = getTimeZoneUTCAndDSTOffset(date, toTimeZone);
return new Date(date.getTime() + (toTimeZoneOffset - fromTimeZoneOffset));
}
/**
* Calculates the offset of the <code>timeZone</code> from UTC, factoring in any
* additional offset due to the time zone being in daylight savings time as of
* the given <code>date</code>.
* @param date
* @param timeZone
* @return
*/
private static long getTimeZoneUTCAndDSTOffset(Date date, TimeZone timeZone)
{
long timeZoneDSTOffset = 0;
if(timeZone.inDaylightTime(date))
{
timeZoneDSTOffset = timeZone.getDSTSavings();
}
return timeZone.getRawOffset() + timeZoneDSTOffset;
}
Run Code Online (Sandbox Code Playgroud)
信用转到这篇文章.
java.util.Calendar
是使用JDK类处理时区的常用方法.Apache Commons还有一些可能有用的替代/实用工具.编辑 Spong的笔记提醒我,我听说Joda-Time真的很棒(虽然我自己没有用过它).
归档时间: |
|
查看次数: |
433921 次 |
最近记录: |