4 回答

TA貢獻1906條經(jīng)驗 獲得超10個贊
好的,這需要幾個步驟......
首先,將輸入輸入有效的日期容器(即LocalDate)
String input = "2019-05-10";
DateTimeFormatter inputFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
LocalDate localDate = LocalDate.parse(input, inputFormatter);
接下來,構(gòu)造一個日期格式化程序,它將能夠?qū)⑷掌谌萜鞲袷交癁槲覀兿胍母袷?。請注意,我使用DateTimeFormatter.ofLocalizedDate(FormatStyle.FULL)而不是提供模式,因為它可以產(chǎn)生更好的結(jié)果
DateTimeFormatter outputFormatter = new DateTimeFormatterBuilder()
.append(DateTimeFormatter.ofLocalizedDate(FormatStyle.FULL))
// .appendPattern("yyyy年MM月dd日")
.toFormatter(Locale.JAPAN);
最后,格式化日期容器...
String output = outputFormatter.format(localDate);
System.out.println(output);
這將產(chǎn)生2019年5月10日金曜日
此外,對于它的價值,F(xiàn)ormatStye.LONG生成2019年5月10日的似乎更符合您的目標異常

TA貢獻1853條經(jīng)驗 獲得超6個贊
您應該為日期使用正確的類,LocalTime但不是LocalDate。此外,由于大寫Y和 ,您的模式是錯誤的D。
我嘗試過以下方式:
public static void main(String args[]) throws Exception {
String a = "2019-05-10"; // ISO date format
String b = "2019-5-10"; // date format with only a single digit for month
LocalDate ldA = LocalDate.parse(a, DateTimeFormatter.ISO_DATE);
LocalDate ldB = LocalDate.parse(b, DateTimeFormatter.ofPattern("yyyy-M-dd"));
// note the lower case year and day, month stays upper case
System.out.println(ldA.format(DateTimeFormatter.ofPattern("yyyy年MM月dd日", Locale.JAPAN)));
System.out.println(ldB.format(DateTimeFormatter.ofPattern("yyyy年MM月dd日", Locale.JAPAN)));
}
并且輸出
2019年05月10日
為了將其存儲在 a 中String,只需執(zhí)行
String myJapaneseDate = ldA.format(DateTimeFormatter.ofPattern("yyyy年MM月dd日", Locale.JAPAN));
編輯:
根據(jù)要求,這是一個使用方法 (1) 使給定的String有效 ISO 格式和 (2) 返回LocalDate從中解析的方法的示例。
public static void main(String args[]) throws Exception {
String a = "2019-05-10"; // ISO date format
String b = "2019-5-10"; // date format with only a single digit for month
LocalDate ldA = makeItIsoFormat(a);
LocalDate ldB = makeItIsoFormat(b);
// note the lower case year and day, month stays upper case
System.out.println(ldA.format(DateTimeFormatter.ofPattern("yyyy年MM月dd日", Locale.JAPAN)));
System.out.println(ldB.format(DateTimeFormatter.ofPattern("yyyy年MM月dd日", Locale.JAPAN)));
}
public static LocalDate makeItIsoFormat(String date) {
// first, split the input String by a hyphon
String[] splitDate = date.split("-");
// check its length, it has to have 3 parts (year, month, day)
if (splitDate.length != 3) {
// if it has more or less parts, the date is considered invalid
System.err.println("invalid date String provided");
} else {
// otherwise, check the amount of digits it has for month
if (splitDate[1].length() == 1) {
// and if it is just one, add a leading zero
System.out.println("The given date \""
+ date
+ "\" has a single digit for month, add leading zero");
splitDate[1] = "0" + splitDate[1];
}
}
/*
* CONSIDER ADDING A CHECK FOR SINGLE-DIGIT DAYS, it is not yet included
*/
// recreate the date String
String isoDate = splitDate[0] + "-" + splitDate[1] + "-" + splitDate[2];
// and return the parsed LocalDate
return LocalDate.parse(isoDate, DateTimeFormatter.ISO_DATE);
}

TA貢獻1966條經(jīng)驗 獲得超4個贊
你可以用 2 SimpleDateFormat
例子:
String input = "2019-05-10";
Date date = new SimpleDateFormat("yyyy-MM-dd").parse(input);
System.out.println(new SimpleDateFormat("yyyy年MM月dd日").format(date));
輸出 :
2019年05月10日

TA貢獻1829條經(jīng)驗 獲得超7個贊
我不知道其他解決方案,但我希望這段代碼有所幫助。該方法只替換字符“-”,然后append()是最后一個。
public String japaneseFormat(String date){
StringBuilder date =new StringBuilder(date);
date.setCharAt(4,'年');
date.setCharAt(7,'月');
date.append("日");
return date.toString();
}
添加回答
舉報