如何将日期字符串转换为长整型?我希望方法签名是public static long convert2(String dateStr,TimeZone fromTz, TimeZone toTz),而dateStr的格式是dd/MM/yyyy。
发布于 2012-06-04 16:12:51
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;https://stackoverflow.com/questions/10878027
复制相似问题