首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用字符串fromTZ date将时区'fromTZ‘转换为'toTZ’?

问如何使用字符串fromTZ date将时区'fromTZ‘转换为'toTZ’?
EN

Stack Overflow用户
提问于 2012-06-04 15:54:12
回答 1查看 102关注 0票数 0

如何将日期字符串转换为长整型?我希望方法签名是public static long convert2(String dateStr,TimeZone fromTz, TimeZone toTz),而dateStr的格式是dd/MM/yyyy。

EN

回答 1

Stack Overflow用户

发布于 2012-06-04 16:12:51

代码语言:javascript
复制
public static long convert2(String dateStr,TimeZone fromTz, TimeZone toTz)
// Format in which you are getting the date
DateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");  
// Set the formater to the timezone in which you are getting the date
formatter.setTimeZone(fromTz);  

// Converting the string date to date
Date date = formatter.parse(dateStr);

// Prints the date in the from time zone timezone. Not required as per the quest. Just for info  
System.out.println(formatter.format(date));  

// Set the formatter to use a different timezone  
formatter.setTimeZone(toTz);  

// Prints the date in the to time zone timezone. Not required as per the quest. Just for info  
System.out.println(formatter.format(date)); 

// converting the new Timzone date to long as that is what is required
long longDate = date.getTime();

// return the long date.
return longDate;
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/10878027

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档