0

日付文字列をlongに変換するにはどうすればよいですか?メソッドのシグネチャは、の形式のpublic static long convert2(String dateStr,TimeZone fromTz, TimeZone toTz)どこにあるかを期待しています。dateStrdd/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 に答える