如何在java中将日期转换为gmt

如何在java中将日期转换为gmt,java,Java,以上是我的代码,当我尝试将日期转换为gmt时,我得到错误: 线程“main”java.lang.IllegalArgumentException中的异常:无法 将给定对象的格式设置为 java.text.DateFormat.format(DateFormat.java:310)位于 java.text.Format.Format(Format.java:157)位于 main(Sample.java:24) 请指出我做错了什么 DateFormat df = new SimpleDateFor

以上是我的代码,当我尝试将日期转换为
gmt
时,我得到错误:

线程“main”java.lang.IllegalArgumentException中的异常:无法 将给定对象的格式设置为 java.text.DateFormat.format(DateFormat.java:310)位于 java.text.Format.Format(Format.java:157)位于 main(Sample.java:24)

请指出我做错了什么

DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'SSSX");
df.setTimeZone(TimeZone.getTimeZone("GMT"));
System.out.println("Current date and time in GMT: " + df.format("2017-01-04"));
上述代码工作正常,但我需要的东西,任何日期的工作

当我尝试此代码时:

 DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'SSSX");
 df.setTimeZone(TimeZone.getTimeZone("GMT"));
 System.out.println("Current date and time in GMT: " + df.format(new Date()));

我得到了不可解析的日期:“2017-01-04”。请告诉我如何修复它

当您有一个日期对象时,您可以根据需要格式化它:

try {
    DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'SSSX");
    df.setTimeZone(TimeZone.getTimeZone("GMT"));
    System.out.println("Current date and time in GMT: " + df.parse("2017-01-04"));

} catch(Exception e) {
    e.printStackTrace();
}
如果您将日期作为字符串,还可以执行以下操作:

DateFormat format = new SimpleDateFormat("yyyy-MM-dd");
format.setTimeZone(TimeZone.getTimeZone("GMT"));

//create a calendar representing your date.
Calendar cal = Calendar.getInstance();
cal.set(2017, 01, 04);

//format the date object to the pattern you want.
String DateToStr = format.format(cal.getTime());

System.out.println(DateToStr);

如果您有日期对象,则可以根据需要对其进行格式化:

try {
    DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'SSSX");
    df.setTimeZone(TimeZone.getTimeZone("GMT"));
    System.out.println("Current date and time in GMT: " + df.parse("2017-01-04"));

} catch(Exception e) {
    e.printStackTrace();
}
如果您将日期作为字符串,还可以执行以下操作:

DateFormat format = new SimpleDateFormat("yyyy-MM-dd");
format.setTimeZone(TimeZone.getTimeZone("GMT"));

//create a calendar representing your date.
Calendar cal = Calendar.getInstance();
cal.set(2017, 01, 04);

//format the date object to the pattern you want.
String DateToStr = format.format(cal.getTime());

System.out.println(DateToStr);
试试看

String string = "2017-01-04";
DateFormat format1 = new SimpleDateFormat("yyyy-MM-dd");
format1.setTimeZone(TimeZone.getTimeZone("GMT"));
Date date = null;
try {
    date = format1.parse(string);
} catch (Exception ex) {
    ex.printStackTrace();
}
System.out.println(date);
试试看

String string = "2017-01-04";
DateFormat format1 = new SimpleDateFormat("yyyy-MM-dd");
format1.setTimeZone(TimeZone.getTimeZone("GMT"));
Date date = null;
try {
    date = format1.parse(string);
} catch (Exception ex) {
    ex.printStackTrace();
}
System.out.println(date);

下面的代码有助于更好地理解-您可以尝试取消注释注释行并比较结果。在整个块中,您感兴趣的是
gdf.parse(gmtFormat)


下面的代码有助于更好地理解-您可以尝试取消注释注释行并比较结果。在整个块中,您感兴趣的是
gdf.parse(gmtFormat)



我的日期在String@paner中只要在SDF中包含时区,您的答案看起来很好。Wed Jan 04 00:00:00 IST 2017它将在IST中出现,而我需要2017-01-04T101Zthis@Paner请建议我解决方案您现在有正确的日期如何格式化它是在我的答案的第一个代码段也看到了我的日期是字符串的例子@Paner只需将时区包含在SDF中,您的答案看起来很好。Wed Jan 04 00:00:00 IST 2017它将在IST中出现,而我需要2017-01-04T101Zthis@Paner请建议我解决方案您现在有正确的日期如何格式化它是在我的答案中的第一个代码片段也看到了示例完整的用例是什么?您希望将服务器/主机时间转换为GMT吗?或者构建一个在时区之间转换的转换器?或者别的什么?df.format方法接受日期对象而不是字符串。对于最近的编辑,请将第一行修改为DateFormat df=new SimpleDataFormat(“yyy-MM-dd”);您应该能够解析请检查我的查询我需要类似2017-01-04T101Z的日期,GMT格式对应于毫秒()-您需要毫秒值忽略小时、分钟和秒吗?是的,请在这方面帮助我完整的用例是什么?您希望将服务器/主机时间转换为GMT吗?或者构建一个在时区之间转换的转换器?或者别的什么?df.format方法接受日期对象而不是字符串。对于最近的编辑,请将第一行修改为DateFormat df=new SimpleDataFormat(“yyy-MM-dd”);您应该能够解析请检查我的查询我需要类似2017-01-04T101Z的日期,GMT格式对应于毫秒()-您需要毫秒值忽略小时,分钟和秒?是的,请帮助我在这2016-11-21 13:30:00.0我的日期是这样来的现在我使用你的代码然后我得到2017-01-03T18:30:00Z我错了请建议我2016-11-21 13:30:00.0我的日期是这样来的现在我使用你的代码然后我得到2017-01-03T18:30:00Z我错了请建议我