[英]Date format Issue in Java
我在后端java中使用以下簡單的日期格式來格式化用戶通過用戶界面指定的日期。
SimpleDateFormat sdf = new SimpleDateFormat("MMM");
SimpleDateFormat sdfMonthYear = new SimpleDateFormat("MMM-yyyy");
SimpleDateFormat sdfMonthYearDisplay = new SimpleDateFormat("MMM yyyy");
當它部署在我的本地計算機上時,它會填充正確的月份值,如Jan,Feb,Mar,Apr等。但是proj現在部署在瑞典系統的其他服務器ex中。所以現在我獲得的月份值為Maj而不是May和Okt而不是10月。我已經驗證了本月格式是基於瑞典語的。我想要一種通用的月格式,即使它已部署在Jan,Feb,may,Oct等任何國家/地區。
請幫我解決這個問題。
創建SimpleDateFormat
時,只需指定語言環境:
SimpleDateFormat sdf = new SimpleDateFormat("MMM", Locale.US);
請注意,為所有用戶使用一個特定的Locale
通常適合進行機器對機器的通信-例如,當您解析ISO-8601格式時。 要向用戶顯示日期,通常可以使用他們的語言環境並獲取適當的格式,例如
DateFormat format = DateFormat.getDateInstance(DateFormat.LONG, userLocale);
SimpleDateFormat sdf = new SimpleDateFormat("MMM", Locale.YourCountryCode);
將其更改為您的本地日期時間
對於代碼,請遵循:
使用另一個SimpleDateFormat
構造函數,其中包括Locale
:
SimpleDateFormat(String pattern, Locale locale)
這里的語言環境是應該使用日期格式符號的語言環境
喜歡 :
SimpleDateFormat sdf = new SimpleDateFormat("MMM", Locale.US);
基本上,您需要java.text.SimpleDateFormat的構造函數SimpleDateFormat(String pattern, Locale locale)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.