简体   繁体   中英

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

How do I convert a datestring to a 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)
// 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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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