0

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

4

1 回答 1

1
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;
于 2012-06-04T08:12:51.117 回答