这个问题在这里已有答案:
我可以将String
转换为Date
:
private static final SimpleDateFormat CARD_DATE_FORMAT = new SimpleDateFormat("yyMMdd", Locale.getDefault());
public static Date toCardDateFormat(String date){
try {
return CARD_DATE_FORMAT.parse(date);
} catch (ParseException e) {
return null;
}
}
例如,我有200908
- 它将转换为2020-09-08
,但我需要将日期设置为每月的第1天。我需要2020-09-01
。我该怎么做?
根据您的需要,您可以使用此方法:
private static final SimpleDateFormat CARD_DATE_FORMAT = new SimpleDateFormat("yyMMdd", Locale.getDefault());
public static String toCardDateFormat(String date) {
try {
Date value = CARD_DATE_FORMAT.parse(date);
SimpleDateFormat dateFormatter = new SimpleDateFormat("yy-MM", Locale.getDefault());
String datetimeLocale = dateFormatter.format(value);
String newDate = datetimeLocale + "-01";
return newDate;
} catch (ParseException e) {
return null;
}
}
对于日期对象,您可以使用此:
public static Date toCardDateFormat(String date) {
try {
Date value = CARD_DATE_FORMAT.parse(date);
SimpleDateFormat dateFormatter = new SimpleDateFormat("yy-MM", Locale.getDefault());
String datetimeLocale = dateFormatter.format(value);
String newDate = datetimeLocale + "-01";
SimpleDateFormat dateFormat = new SimpleDateFormat("yy-MM-dd",Locale.getDefault());
Date d = dateFormat.parse(newDate);
return d;
} catch (ParseException e) {
return null;
}
}
Maciej的答案是正确的,但如果使用Java 8或更高版本,最好使用java.time类:
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyMMdd");
// parse and change the day of month
LocalDate d = LocalDate.parse("200908", formatter).withDayOfMonth(1);
System.out.println(d); // 2020-09-01
请注意,LocalDate
以您想要的格式打印 - 符合ISO8601标准。如果你想要一个不同的格式,只需使用另一个DateTimeFormatter
并调用format
方法。
按照其他人的建议手动更改字符串也可能有效,但如果您正在处理日期,为什么不使用正确的日期处理API?在无效日期(格式化程序将为无效输入引发异常)的情况下,或者如果您尝试将日期更改为无效值(例如4月份的第31天或2月份的29日),直接字符串操作将无法帮助您非闰年,由API检查,如果值无效则抛出异常)。
您的问题的答案是(使用旧的日期api) - 您可以使用日历来执行此操作:
public static Date toCardDateFormat(String date){
Date result = null;
try {
result = new SimpleDateFormat("yyMMdd", Locale.getDefault()).parse(date);
} catch (ParseException e) {
return null; // or better throw exception or return Optional.empty()
}
Calendar cal = Calendar.getInstance();
cal.setTime(result);
cal.set(Calendar.DAY_OF_MONTH, 1);
return cal.getTime();
}
你可以尝试这样的事情:
String[] arr = date.split("-");
String newDate = arr[0] + "-" + arr[1] + "-01";
private static final DateTimeFormatter CARD_DATE_FORMAT
= DateTimeFormatter.ofPattern("yyMMdd", Locale.getDefault());
public static Optional<YearMonth> toCardDateFormat(String date) {
try {
return Optional.of(YearMonth.parse(date, CARD_DATE_FORMAT));
} catch (DateTimeParseException e) {
return Optional.empty();
}
}
不要从方法返回null
。呼叫方的NullPointerException
风险很大。如果您认为如果字符串格式错误或包含无效日期,则不返回值是正确的,请使用Optional
强制调用者考虑无返回值的可能性。另一个明显的选择是将任何解析异常留给调用者:
public static YearMonth toCardDateFormat(String date) {
return YearMonth.parse(date, CARD_DATE_FORMAT);
}
DateTimeParseException
是一个未经检查的异常,因此不需要在方法签名中声明。我们来试试吧:
System.out.println(toCardDateFormat("200908"));
这打印:
2020-09
其他消息:
SimpleDateFormat
非常麻烦。我认为你应该避免它们。现代API可以更好地使用。YearMonth
类完全适合您。如果您想要一个完整的日期,请使用LocalDate
中的xunts’ answer类。链接:Oracle tutorial: Date Time解释如何使用java.time
。