我有一个ggplot(data, aes(x = value)) +
geom_histogram(color = "red", stat = StatBin2) + # specify stat = StatBin2
facet_wrap(~ key, ncol = 1)
日期。此日期为西方日期格式。我想要的是日文格式。
String
当前错误:
String a = "2019-05-10";
DateTimeFormatter timeFormatter = new DateTimeFormatterBuilder()
.appendPattern("YYYY-MM-DD")
.toFormatter(Locale.JAPAN);
String aa = LocalTime.parse(a, timeFormatter)
.format(DateTimeFormatter.ofPattern("YYYY年MM月DD日", Locale.JAPAN));
System.out.println(aa);
预期输出:
Exception in thread "main" java.time.format.DateTimeParseException: Text '2019-05-10' could not be parsed: Unable to obtain LocalTime from TemporalAccessor: {DayOfYear=10, MonthOfYear=5, WeekBasedYear[WeekFields[SUNDAY,1]]=2019},ISO of type java.time.format.Parsed
at java.time.format.DateTimeFormatter.createError(Unknown Source)
at java.time.format.DateTimeFormatter.parse(Unknown Source)
at java.time.LocalTime.parse(Unknown Source)
at testing.DatumTypeDecimal.main(DatumTypeDecimal.java:223)
Caused by: java.time.DateTimeException: Unable to obtain LocalTime from TemporalAccessor: {DayOfYear=10, MonthOfYear=5, WeekBasedYear[WeekFields[SUNDAY,1]]=2019},ISO of type java.time.format.Parsed
at java.time.LocalTime.from(Unknown Source)
at java.time.format.Parsed.query(Unknown Source)
... 3 more
答案 0 :(得分:4)
好的,这需要一些步骤...
首先,将输入内容输入有效的日期容器(即threshold = 0.5
prediction=np.array([0,0.2,0.9,0.7])
prediction[prediction<=threshold]=0
prediction[prediction>threshold]=1
)
LocalDate
接下来,构造一个日期格式化程序,它将能够将日期容器格式化为我们想要的格式。请注意,我使用String input = "2019-05-10";
DateTimeFormatter inputFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
LocalDate localDate = LocalDate.parse(input, inputFormatter);
而不是提供模式,因为它可以产生更好的结果
DateTimeFormatter.ofLocalizedDate(FormatStyle.FULL)
最后,格式化日期容器...
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);
此外,2019年5月10日金曜日
生成的FormatStye.LONG
似乎更符合您的目标异常
答案 1 :(得分:2)
对于日期,您应该使用适当的类,而不是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日
要将其存储在String
中,只需执行
String myJapaneseDate = ldA.format(DateTimeFormatter.ofPattern("yyyy年MM月dd日", Locale.JAPAN));
编辑:
根据要求,下面是一个示例方法,该方法用于(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);
}
答案 2 :(得分:0)
我不知道其他解决方案,但是希望这段代码对您有所帮助。
该方法仅替换字符'-',然后替换append()
最后一个字符。
public String japaneseFormat(String date){
StringBuilder date =new StringBuilder(date);
date.setCharAt(4,'年');
date.setCharAt(7,'月');
date.append("日");
return date.toString();
}
答案 3 :(得分:0)
你可以用 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日