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 将本地时间转换为UTC的问题_Java_Date_Datetime - Fatal编程技术网

Java 将本地时间转换为UTC的问题

Java 将本地时间转换为UTC的问题,java,date,datetime,Java,Date,Datetime,我尝试了以下代码将本地日期时间转换为UTC日期时间。但他们也一样来了。我想我错过了一些东西。有谁能帮我从本地日期时间2013年10月15日上午09:00 GMT+05:30获取UTC日期时间吗。这个日期时间字符串是我从外部系统获取的,因此如果需要,我可以更改格式 SimpleDateFormat outputFormatInUTC = new SimpleDateFormat("MM/dd/yyyy hh:mm aaa z"); String startDateTime = "10/15/201

我尝试了以下代码将本地日期时间转换为UTC日期时间。但他们也一样来了。我想我错过了一些东西。有谁能帮我从本地日期时间2013年10月15日上午09:00 GMT+05:30获取UTC日期时间吗。这个日期时间字符串是我从外部系统获取的,因此如果需要,我可以更改格式

SimpleDateFormat outputFormatInUTC = new SimpleDateFormat("MM/dd/yyyy hh:mm aaa z");
String startDateTime = "10/15/2013 09:00 AM GMT+05:30";
Date utcDate = outputFormatInUTC.parse(startDateTime);
只需设置时区:

输出:

10/15/2013 03:30 AM UTC
试试这个:

    SimpleDateFormat outputFormatInUTC = new SimpleDateFormat("MM/dd/yyyy hh:mm aaa z");
    System.out.println(new Date()); //prints local date-time
    outputFormatInUTC.setTimeZone(TimeZone.getTimeZone("UTC"));
    System.out.println(outputFormatInUTC.format(new Date())); //prints UTC date-time
ISO 8601格式 是的,如果您可以更改字符串的格式,您应该。为此目的制定了一项国际标准:。比如这个2013-12-26T21:19:39+00:00或者这个2013-12-26T21:19Z

乔达时间 避免使用java.util.Date/Calendar类。它们是出了名的坏。Java8用新的Java.time.*JSR310类取代了它们。同时,您可以使用启发JSR310的库

下面的示例代码使用在Mac上运行的Java7中的JodaTime2.3

示例代码 //©2013巴西尔布尔克。此源代码可以由任何对此承担全部责任的人自由使用。 //导入org.joda.time.*; //导入org.joda.time.format.*; 字符串字符串=2013年10月15日上午9:00 GMT+05:30; DateTimeFormatter formatter=DateTimeFormat.forPattern MM/dd/yyyy hh:MM aaa zZ; DateTime DateTime=formatter.parseDateTime字符串; System.out.println dateTime:+dateTime; 当运行时

日期时间:2013-10-15T03:30:00.000Z
谢谢你的回复。在这里,我首先得到date对象,然后再次得到date作为字符串,所以现在如果我需要一个date对象,我必须再次将这个字符串传输到date对象。我说得对吗?如果是的话,有更好的方法吗?根本没有,我们首先设置SimpleDataFormat->configure time zone->parse string date->根据DateFormat提取正确的字符串日期。所以现在我有了UTC格式的日期字符串。我怎样才能把它转成约会呢。
    SimpleDateFormat outputFormatInUTC = new SimpleDateFormat("MM/dd/yyyy hh:mm aaa z");
    System.out.println(new Date()); //prints local date-time
    outputFormatInUTC.setTimeZone(TimeZone.getTimeZone("UTC"));
    System.out.println(outputFormatInUTC.format(new Date())); //prints UTC date-time