gpt4 book ai didi

java - 如何将 "MM-dd-yyyy hh:mm"字符串日期格式转换为 GMT 格式?

转载 作者:行者123 更新时间:2023-11-29 03:35:19 25 4
gpt4 key购买 nike

我的日期格式类似于“MM-dd-yyyy hh:mm”,它不是当前日期,我必须发送这个日期 到服务器但在发送之前需要将此日期更改为 GMT 格式但是当我通过以下代码更改时:

private String[] DateConvertor(String datevalue)
{
String date_value[] = null;
String strGMTFormat = null;
SimpleDateFormat objFormat,objFormat1;
Calendar objCalendar;
Date objdate1,objdate2;
if(!datevalue.equals(""))
{
try
{
//Specify your format
objFormat1 = new SimpleDateFormat("MM-dd-yyyy,HH:mm");
objFormat1.setTimeZone(Calendar.getInstance().getTimeZone());

objFormat = new SimpleDateFormat("MM-dd-yyyy,HH:mm");
objFormat.setTimeZone(TimeZone.getTimeZone("GMT"));

//Convert into GMT format
//objFormat.setTimeZone(TimeZone.getDefault());//);
objdate1=objFormat1.parse(datevalue);
//
//objdate2=objFormat.parse(datevalue);


//objFormat.setCalendar(objCalendar);
strGMTFormat = objFormat.format(objdate1.getTime());
//strGMTFormat = objFormat.format(objdate1.getTime());
//strGMTFormat=objdate1.toString();
if(strGMTFormat!=null && !strGMTFormat.equals(""))
date_value = strGMTFormat.split(",");
}
catch (Exception e)
{
e.printStackTrace();
e.toString();
}
finally
{
objFormat = null;
objCalendar = null;
}
}
return date_value;

}

它没有改变所需的格式,我尝试通过上面的代码首先尝试获取当前时区,然后在转换 GMT 之后尝试将字符串日期更改为该时区。任何人指导我。

提前致谢。

最佳答案

试试下面的代码。第一个 sysout 打印日期对象,该对象选择默认操作系统时区,即我的 IST。第二个系统输出在将日期转换为 GMT 时区后以所需格式打印日期。

如果您知道日期字符串的时区,请在格式化程序中进行设置。我假设您需要 GMT 时区的相同日期格式。

SimpleDateFormat format = new SimpleDateFormat("MM-dd-yyyy,HH:mm");

Date date = format.parse("01-23-2012,09:40");
System.out.println(date);

format.setTimeZone(TimeZone.getTimeZone("GMT"));
System.out.println(format.format(date));

关于java - 如何将 "MM-dd-yyyy hh:mm"字符串日期格式转换为 GMT 格式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15893159/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com