import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.Locale;
public class Solution {
public static void main(String[] args) {
System.out.println(isDateOdd("MAY 1 2013"));
}
public static boolean isDateOdd(String date) {
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MMM dd yyyy");
formatter = formatter.withLocale(Locale.ENGLISH);
LocalDate outputDate = LocalDate.parse(date, formatter);
return ((outputDate.getDayOfYear()%2!=0)?true:false);
}
}
Run Code Online (Sandbox Code Playgroud)
我想知道,如果从年初到某个日期过去的天数很奇怪.我尝试使用LocalDate来解析我的字符串中的日期(2013年5月1日),但是我收到错误:
线程"main"中的异常java.time.format.DateTimeParseException:无法在java.time.format的java.time.format.DateTimeFormatter.parseResolved0(DateTimeFormatter.java:1949)的索引0处解析文本"MAY 1 2013"位于com.javarush.task.task08.task0827.Solution.isDateOdd(Solution.java:23)的java.time.LocalDate.parse(LocalDate.java:400)中的.DateTimeFormatter.parse(DateTimeFormatter.java:1851). javarush.task.task08.task0827.Solution.main(Solution.java:16)
哪里有问题?
我想使用 java 创建类似任务管理器的东西。我决定使用 PostgreSQL 来保存我的任务。此时,我想将我创建的任务保存到 PostgreSQL,为此我有以下代码:
package TaskMgr;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeFormatterBuilder;
import java.util.Locale;
public class Task {
private String title;
private String description;
private LocalDate dateFormat;
public Task(String title, String description, String date) {
this.title = title;
this.description = description;
this.dateFormat = setDate(date);
}
public LocalDate setDate(String inputDate) {
DateTimeFormatter formatter = new DateTimeFormatterBuilder()
.parseCaseInsensitive()
.appendPattern("yyyy-MM-d")
.toFormatter(Locale.ENGLISH);
formatter = formatter.withLocale(Locale.ENGLISH);
LocalDate outputDate = LocalDate.parse(inputDate, formatter);
return outputDate;
}
public String getTitle() {
return title;
}
public String …Run Code Online (Sandbox Code Playgroud)