Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/date/2.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 如何根据时区的字符串值转换日期_Java_Date - Fatal编程技术网

Java 如何根据时区的字符串值转换日期

Java 如何根据时区的字符串值转换日期,java,date,Java,Date,我有一个日期对象,我需要将其转换为登录用户的时区。问题是,时区在DB中表示为GMT的字符串值加上或减去小时偏移量。例如,纽约时间的“GMT”或“GMT-5”或“GMT+5” 当我只有“GMT-3”或“GMT+5”这样的字符串时,如何将日期对象转换为用户的时间 提前感谢您的帮助。一个示例应该会有所帮助,但它似乎是一个1字符: 它应该会起作用 yyyy-MM-dd'T'HH:MM:ss'GMT'X符合iso8601时区 myDate=myDate.replace(“-”,“-0”)将日期调整为您的

我有一个日期对象,我需要将其转换为登录用户的时区。问题是,时区在DB中表示为GMT的字符串值加上或减去小时偏移量。例如,纽约时间的“GMT”或“GMT-5”或“GMT+5”

当我只有“GMT-3”或“GMT+5”这样的字符串时,如何将日期对象转换为用户的时间


提前感谢您的帮助。

一个示例应该会有所帮助,但它似乎是一个1字符:

它应该会起作用

  • yyyy-MM-dd'T'HH:MM:ss'GMT'X
    符合iso8601时区
  • myDate=myDate.replace(“-”,“-0”)将日期调整为您的格式
偏移量≠ 时区 正如Jon Skeet在评论中所说,时区不仅仅是相对于/GMT的偏移量。存储偏移量小时(和分钟)是在数据库/存储中处理日期时间的一种不太理想的策略

乔达时间 众所周知,java.util.Date和java.util.Calendar类非常麻烦。避开它们。使用。或者,在Java8中,使用新的,由JSR310定义的,受Joda Time启发但重新架构的

我们可以创建一个来表示偏移量,但如前所述,这并不能在逻辑上构成完整的时区

我们可以将java.util.Date对象直接传递给Joda时间构造函数。同时,我们传递一个物体。要转到转换的另一个方向,请对DateTime对象调用
toDate

java.util.Date date = new java.util.Date(); // Retrieved from elsewhere. Faked here.

String offsetInput = "GMT-5";
int offsetHours = 0, offsetMinutes = 0;
offsetInput = offsetInput.replace( "GMT", "" ); // Delete 'GMT' characters.
String[] parts = offsetInput.split(":"); // About splitting a string: http://stackoverflow.com/q/3481828/642706

// Handle results of split.

if( parts.length == 0 ) {
    // Add some error handling here
} 

if ( parts.length >= 1 ) {
    offsetHours = Integer.parseInt( parts[0] ); // Retrieve text of first number (zero-based index counting).
}

if ( parts.length >= 2 ) {
    offsetMinutes = Integer.parseInt( parts[1] ); // Retrieve text of second number (zero-based index counting).
}

if( parts.length >= 3 ) {
    // Add some error handling here
} 

DateTimeZone partialTimeZoneWithOnlyOffset = DateTimeZone.forOffsetHoursMinutes( offsetHours, offsetMinutes );

DateTime dateTime = new DateTime( date, partialTimeZoneWithOnlyOffset );
转储到控制台

System.out.println( "date: " + date ); // BEWARE: JVM's default time zone applied in the implicit call to "toString" of a Date. Very misleading.
System.out.println( "partialTimeZoneWithOnlyOffset: " + partialTimeZoneWithOnlyOffset );
System.out.println( "dateTime: " + dateTime );
System.out.println( "dateTime with alternate formatting: " + DateTimeFormat.forStyle( "FF" ).withLocale( Locale.US ).print(  dateTime ) );
当运行时

日期:2014年2月8日星期六22:40:57太平洋标准时间
partialTimeZoneWithOnlyOffset:-05:00
日期时间:2014-02-09T01:40:57.810-05:00
具有替代格式的日期时间:2014年2月9日星期日上午1:40:57-05:00

您可以更改数据库结构吗?如果您只是存储一个偏移量,那么将其存储为一个数字是明智的——请记住,并非所有偏移量都是小时数。还要注意,偏移量与时区不同。可以有多个时区,它们当前具有相同的偏移量,但在其他时间具有不同的偏移量。此外,日期对象不必“转换”。日期对象与时区无关。在将日期格式化为字符串或将字符串解析为日期时,您应该只关心时区。这是一个很好的示例。但请记住SimpleDataFormat不是线程安全的。当使用DB连接时,通常会有一个线程池,并且由于缺少线程安全性,可能会遇到“奇怪的行为”。我建议使用FastDateFormat,它的工作原理与SimpleDateFormat完全相同,但具有内置的线程安全性:是的,SimpleDateFormat不是线程安全的,因为同步可能会导致性能损失,创建新实例比同步更快。FastDateFormat应该是一个很好的机会,但它不支持iso8601。。。。
System.out.println( "date: " + date ); // BEWARE: JVM's default time zone applied in the implicit call to "toString" of a Date. Very misleading.
System.out.println( "partialTimeZoneWithOnlyOffset: " + partialTimeZoneWithOnlyOffset );
System.out.println( "dateTime: " + dateTime );
System.out.println( "dateTime with alternate formatting: " + DateTimeFormat.forStyle( "FF" ).withLocale( Locale.US ).print(  dateTime ) );