我试图将格式化的日期字符串转换为Date对象.日期字符串格式化为某个其他时区.
当我这样做时sdf.parse(String),返回我的系统日期对象.
代码如下,
static Date convertGMTTime(String timeZone, long longDate){
Date convertedTime = null;
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
try{
Date date = new Date(longDate);
System.out.println("timezone: "+timeZone +", timestamp: "+date);
Locale locale = Locale.ENGLISH;
TimeZone destTimeZone = TimeZone.getTimeZone(timeZone);// TimeZone.getDefault();
System.out.println("Source timezone: "+destTimeZone);
/* DateFormat formatter = DateFormat.getDateTimeInstance(
DateFormat.DEFAULT,
DateFormat.DEFAULT,
locale);
formatter.setTimeZone(destTimeZone);*/
sdf.setTimeZone(destTimeZone);
String convertedDateStr = sdf.format(date);
System.out.println("convertedDateStr: "+convertedDateStr);
convertedTime = sdf.parse(convertedDateStr);
System.out.println("convertedTime: "+convertedTime + "sdf: "+sdf.getTimeZone());
}catch(Exception e){
e.printStackTrace();
}
return convertedTime;
}
Run Code Online (Sandbox Code Playgroud)
如果有人能提供帮助并指出我哪里出错,我将不胜感激.提前致谢.
输出:
时区:Atlantic/Cape_Verde,时间戳:Tue Jun 26 17:38:11 IST 2012
来源时区:sun.util.calendar.ZoneInfo [id ="Atlantic/Cape_Verde",偏移= -3600000,dstSavings = 0,useDaylight = false,过渡= 6,lastRule =空]convertedDateStr:2012-06-26 11:08:11
convertedTime:Tue Jun 26 17:38:11 IST 2012
sdf:sun.util.calendar.ZoneInfo [id ="Atlantic/Cape_Verde",offset = -3600000,dstSavings = 0,useDaylight = false,transitions = 6,lastRule = null ]
分享的更多细节,当我使用另一个sdf对象(没有为它设置时区)时,它会返回正确的时间和日期,但仍然从系统时钟中选择时区
码
static Date convertGMTTime(String timeZone, long longDate){
Date convertedTime = null;
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
SimpleDateFormat sdfParse = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
try{
Date date = new Date(longDate);
TimeZone destTimeZone = TimeZone.getTimeZone(timeZone);// TimeZone.getDefault();
System.out.println("Source timezone: "+destTimeZone);
sdf.setTimeZone(destTimeZone);
String convertedDateStr = sdf.format(date);
System.out.println("convertedDateStr: "+convertedDateStr );
convertedTime = sdfParse.parse(convertedDateStr,new ParsePosition(0));
System.out.println("convertedTime: "+convertedTime + "sdf: "+sdf.getTimeZone());
}catch(Exception e){
e.printStackTrace();
}
return convertedTime;
}
Run Code Online (Sandbox Code Playgroud)
产量
Source timezone: sun.util.calendar.ZoneInfo[id="Atlantic/Cape_Verde",offset=-3600000,dstSavings=0,useDaylight=false,transitions=6,lastRule=null]
convertedDateStr: 2012-06-26 12:24:56
convertedTime: Tue Jun 26 12:24:56 IST 2012
sdf: sun.util.calendar.ZoneInfo[id="Atlantic/Cape_Verde",offset=-3600000,dstSavings=0,useDaylight=false,transitions=6,lastRule=null]
Run Code Online (Sandbox Code Playgroud)
据我所知,当我没有为sdf分配时区时,它需要系统时区,但为什么不在系统时区显示时间?我在时区中显示它,因为它在字符串中,但时区是不同的.
当我设置时区时,它按照我的系统时间返回日期对象,而不管sdf是否设置了其他时区.
任何人都可以解释sdf.parse和sdf.format的功能行为.
对我来说,当我们使用格式时,sdf.setTimeZone()确实会产生影响,当我们使用sdf.parse()时它会无效.我觉得很奇怪.
在这方面表示感谢.
您已经有Date(或Date的毫秒数),因此无需转换.日期没有任何时区.这是一个普遍的时刻.时区仅在您显示此日期时才相关,因为日期65647678000在某个时区可能是12:38,而在某个其他时区可能是10:38.当你解析 Date的String表示时,它也是相关的,因为10:38在某个时区是65647678000,而在其他时区是65657678000.
虽然您不显示Date对象,或将字符串解析为日期,但您不需要关心时区.并选择显示/解析时使用的时区,设置时区DateFormat,然后使用DateFormat.format()/ DateFormat.parse()格式化/解析日期.
当您Date.toString()用来显示日期时,它将始终使用您当前的时区.
我觉得通过不考虑日期作为一天,一个月,一年,一小时等来理解我的意思更容易,但是作为片刻:"当肯尼迪被枪杀时"."当肯尼迪被枪杀时"对每个人来说都是同一时刻.但是如果你代表在达拉斯时区"肯尼迪被枪杀"的那一刻,这与你在巴黎时区代表同一时刻所获得的结果不同.