Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/310.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/jpa/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中将基于GMT/CST的字符串转换为时间戳_Java - Fatal编程技术网

在java中将基于GMT/CST的字符串转换为时间戳

在java中将基于GMT/CST的字符串转换为时间戳,java,Java,我试图从一个时区(比如IST或任何其他时区)获取时间,然后将其转换为GMT时区字符串。但当我试图从基于GMT的字符串时间中获取时间戳时,我得到了本地时间戳值。有什么具体的原因吗 “有什么具体的原因吗?”是的,因为Java假定默认情况下您希望“打印”(呈现)JVM运行的本地时区中的日期值 @Before public void setup() { sdf = new SimpleDateFormat(Hello.TS_FORMAT);// TS_FORMAT = "yyyyMMdd'T'H

我试图从一个时区(比如IST或任何其他时区)获取时间,然后将其转换为GMT时区字符串。但当我试图从基于GMT的字符串时间中获取时间戳时,我得到了本地时间戳值。有什么具体的原因吗

“有什么具体的原因吗?”是的,因为Java假定默认情况下您希望“打印”(呈现)JVM运行的本地时区中的日期值

@Before
public void setup() {
    sdf = new SimpleDateFormat(Hello.TS_FORMAT);// TS_FORMAT = "yyyyMMdd'T'HHmmssXX";
    Calendar cal = sdf.getCalendar();
    cal.setTimeZone(TimeZone.getTimeZone(UTC_TIME_ZONE));// UTC_TIME_ZONE = "GMT";
    sdf.setCalendar(cal);
    ...
}

@Test
public void testTimestampFormat03() {
    String inboundTimestampText = '20170322T170805-0700';// means inbound is in Pacific Time Zone (17:08:05 on 03/22)
    Date dt = sdf.parse(inboundTimestampText);
    String defaultFormat = dt.toString();// default locale is Central Time Zone (19:08:05 on 03/22)
    String actualFormat = sdf.format(dt);
    String expectedFormat = inboundTimestampText.replace('T17', 'T00');
    expectedFormat = expectedFormat.replace('0322', '0323');// expected Time Zone is UTC (00:08:05 on 03/23) 
    expectedFormat = expectedFormat.replace('-', 'Z');
    assertEquals(expectedFormat, actualFormat + '0700');
}
您必须指定要在其中“呈现”日期值的时区。基本上,您需要使用“相同”的格式化程序来打印日期
formatter.format(aDate)
,这是您在web上找到的日期字符串
formatter.parse(aDtaeString)
中读取的

Calendar calendar = Calendar.getInstance();
TimeZone fromTimeZone = calendar.getTimeZone();
TimeZone toTimeZone = TimeZone.getTimeZone("CST");

calendar.setTimeZone(fromTimeZone);
calendar.add(Calendar.MILLISECOND, fromTimeZone.getRawOffset() * -1);
if (fromTimeZone.inDaylightTime(calendar.getTime())) {
    calendar.add(Calendar.MILLISECOND, calendar.getTimeZone().getDSTSavings() * -1);
}

calendar.add(Calendar.MILLISECOND, toTimeZone.getRawOffset());
if (toTimeZone.inDaylightTime(calendar.getTime())) {
    calendar.add(Calendar.MILLISECOND, toTimeZone.getDSTSavings());
}

System.out.println(calendar.getTime());

在这里发布您的代码