2013-01-17 17 views
0

我現在在香港。我如何從美國得到年,月和日?
我曾嘗試:如何從不同的時區獲得年,月和日?

Calendar calendar = Calendar.getInstance(); 
SimpleDateFormat sdf = new SimpleDateFormat("dd-MMM-yyyy hh:mm:ss z"); 
sdf.setTimeZone(TimeZone.getTimeZone("America/Los_Angeles")); 
sdf.format(calendar.getTime()); 

但我不知道怎麼弄的年,月。 請幫助

回答

1

下面的代碼產生三個所需變量:

Calendar calendar = Calendar.getInstance(); 
Calendar LATime = new GregorianCalendar(TimeZone.getTimeZone("America/Los_Angeles")); 
LATime.setTimeInMillis(calendar.getTimeInMillis()); 
int year = LATime.get(Calendar.YEAR); 
int month = LATime.get(Calendar.MONTH); 
int date = LATime.get(Calendar.DATE); 

希望它能幫助。

+0

對不起日期回覆17,但應該是16 – jjLin

+0

編輯,一年應該從LATime – jjLin

+0

得到對!感謝修正。 – DNax

0

你需要做類似如下(假設選項dateObj是香港時間日期實例):

SimpleDateFormat sdf = new SimpleDateFormat("dd-MMM-yyyy hh:mm:ss z"); 
sdf.setTimeZone(TimeZone.getTimeZone("America/Los_Angeles")); 
String usaDateStr= sdf.format(dateObj); 

看到這個example

0

您也可以使用日期,時間操縱一個有用的lib像Joda LIB

我也嘗試UTC時間轉換爲本地時間時使用它,你可能會嘗試不同的時區之間的轉換。 祝你好運! :)

相關問題