简体   繁体   English

如何使用字符串fromTZ date从时区'fromTZ'转换为'toTZ'?

[英]how to convert from timezone 'fromTZ' to 'toTZ' using string fromTZ date?

How do I convert a datestring to a long? 如何将日期字符串转换为long? I expect the method signature will be public static long convert2(String dateStr,TimeZone fromTz, TimeZone toTz) where dateStr is in the format dd/MM/yyyy . 我希望方法签名将是public static long convert2(String dateStr,TimeZone fromTz, TimeZone toTz) ,其中dateStr的格式为dd/MM/yyyy

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;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM