日期时间字符串+时区 [英] Date time String + TimeZone

查看:611
本文介绍了日期时间字符串+时区的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个本地日期时间的字符串表示法和Java TimeZone。



我正在尝试以MM / dd / yyyy HH格式获取输出: mm:ssZ,但我无法弄清楚如何创建具有正确日期时间和时区的日历或JodaTime对象。如何将TimeZone转换为可以通过SimpleDateFormat'Z'或'z'解析的值?

  TimeZone tz = TimeZone.getTimeZone(America / Chicago); 
String startDate =08/14/2014 15:00:00;

SimpleDateFormat sdf = new SimpleDateFormat(MM / dd / yyyy HH:mm:ss);
日历cal = Calendar.getInstance(tz);
cal.setTime(sdf.parse(startDate));
SimpleDateFormat sdf = new SimpleDateFormat(MM / dd / yyyy HH:mm:ssZ);

  sdfZ.format(cal.getTime())

返回

  08/14/2014 15:00:00-0400 

这是 EST



创建日历或Joda DateTime,并通过解析字符串08/14/2014 15:00:00来设置个别年/月/日/小时/分钟的值?

解决方式

日历 getTime() - 返回表示此日历时间值的Date对象(与Epoch的毫秒偏移量(01-01-1970 00:00 GMT)),不管你显示哪个时区,但是在不同的TimeZone中的小时将不同。 get(Calendar.HOUR_OF_DAY)



您应该尝试

  sdfZ.setTimeZone(tz); 


I have a String representation of a local date time, and a Java TimeZone.

I am trying to get output in the format MM/dd/yyyy HH:mm:ssZ but I can't figure out how to create a Calendar or JodaTime object with the correct date time and timezone. How do you get a TimeZone converted to a value that can be parsed by SimpleDateFormat 'Z' or 'z'?

TimeZone tz = TimeZone.getTimeZone("America/Chicago");
String startDate = "08/14/2014 15:00:00";

SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
Calendar cal = Calendar.getInstance(tz);
cal.setTime(sdf.parse(startDate));
SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy HH:mm:ssZ");

and

sdfZ.format(cal.getTime()) 

returns

08/14/2014 15:00:00-0400

which is EST.

Is the only workaround to create a Calendar or Joda DateTime and set the individual year/month/day/hour/min values by parsing the string "08/14/2014 15:00:00" ?

解决方案

Calendar getTime() - Returns a Date object representing this Calendar's time value (millisecond offset from the Epoch(01-01-1970 00:00 GMT)") irrespective of which timezone you are displaying. But hour of day in different TimeZone will be different. get(Calendar.HOUR_OF_DAY)

You should try

sdfZ.setTimeZone(tz);

这篇关于日期时间字符串+时区的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆