Java 8 输出日期为 YYYY-MM-DD
Java 8 Output date as YYYY-MM-DD
我在 Java 8 中有一个 getter,我设置为以下,问题是输出是 Sat Jan 11 00:00:00 AEDT 2020
class 必须保留为 Date
,StartDate
是 LocalDate
。
public static Date getStartDate() {
return Date.from(StartDate.atStartOfDay()
.atZone(ZoneId.systemDefault())
.toInstant());
}
我需要它return YYYY-MM-DD
的值,我正在努力解决它。
是的,这是一项作业。不知道我们被告知将一个设置为 Date
而另一个设置为 LocalDate
的逻辑,除了惹恼我......
感谢任何帮助。
我想你可以试试这个来获取日期值 "yyyy-MM-dd"
import java.text.SimpleDateFormat;
import java.util.Date;
public class Test {
public static void main(String[] args) {
String pattern = "yyyy-MM-dd";
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern);
String date = simpleDateFormat.format(new Date());
System.out.println(date);
}
}
直截了当的答案 - SimpleDateFormatter
正如您所说 getStartDate
返回的类型必须保持 Date
。在这种情况下,最直接的方法是使用 SimpleDateFormat
class:
您可以将日期传递到 SimpleDateFormatter
:
SimpleDateFormat iso_8601_formatter = new SimpleDateFormat("yyyy-MM-dd");
System.out.println(iso_8601_formatter.format(getStartDate());
可以在 this 博客 post 上找到更多详细信息。
最佳实践答案 - 使用 java.time
如 Ole V.V 所述,如果可以重构代码,最好围绕 java.time
统一代码库,而不是将 API 与 java.util.Date
。这可以通过 DateTimeFormatter
来完成
DateTimeFormatter iso_8601_formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
LocalDate start = StartDate.atStartOfDay();
System.out.println(start.format(iso_8601_formatter));
请注意,DateTimeFormatter 提供常量 DateTimeFormatter.ISO_LOCAL_DATE
用于以 ISO-8601 格式打印值。
有关新 API 改进的更多信息,请参阅 this 文章。
返回 java.util.Date
的实例时,您无法对其进行格式化。它将始终作为 Date 对象返回。如果你想要它在字符串中,
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
String formattedDate = sdf.format(new Date());
其中 new Date()
是今天的日期,无论您放置什么日期对象,sdf 对象 (java.text.SimpleDateFormat
) 都会这样做。
更多可以参考java 8 docs of SimpleDateFormat
请尝试此解决方案。
public static String getStartDate() {
DateTimeFormatter oldPattern = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSSz");
DateTimeFormatter newPattern = DateTimeFormatter.ofPattern("yyyy-MM-dd");
LocalDateTime datetime = LocalDateTime.parse(new Date().toInstant().toString(), oldPattern);
return datetime.format(newPattern);
}
我在 Java 8 中有一个 getter,我设置为以下,问题是输出是 Sat Jan 11 00:00:00 AEDT 2020
class 必须保留为 Date
,StartDate
是 LocalDate
。
public static Date getStartDate() {
return Date.from(StartDate.atStartOfDay()
.atZone(ZoneId.systemDefault())
.toInstant());
}
我需要它return YYYY-MM-DD
的值,我正在努力解决它。
是的,这是一项作业。不知道我们被告知将一个设置为 Date
而另一个设置为 LocalDate
的逻辑,除了惹恼我......
感谢任何帮助。
我想你可以试试这个来获取日期值 "yyyy-MM-dd"
import java.text.SimpleDateFormat;
import java.util.Date;
public class Test {
public static void main(String[] args) {
String pattern = "yyyy-MM-dd";
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern);
String date = simpleDateFormat.format(new Date());
System.out.println(date);
}
}
直截了当的答案 - SimpleDateFormatter
正如您所说 getStartDate
返回的类型必须保持 Date
。在这种情况下,最直接的方法是使用 SimpleDateFormat
class:
您可以将日期传递到 SimpleDateFormatter
:
SimpleDateFormat iso_8601_formatter = new SimpleDateFormat("yyyy-MM-dd");
System.out.println(iso_8601_formatter.format(getStartDate());
可以在 this 博客 post 上找到更多详细信息。
最佳实践答案 - 使用 java.time
如 Ole V.V 所述,如果可以重构代码,最好围绕 java.time
统一代码库,而不是将 API 与 java.util.Date
。这可以通过 DateTimeFormatter
DateTimeFormatter iso_8601_formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
LocalDate start = StartDate.atStartOfDay();
System.out.println(start.format(iso_8601_formatter));
请注意,DateTimeFormatter 提供常量 DateTimeFormatter.ISO_LOCAL_DATE
用于以 ISO-8601 格式打印值。
有关新 API 改进的更多信息,请参阅 this 文章。
返回 java.util.Date
的实例时,您无法对其进行格式化。它将始终作为 Date 对象返回。如果你想要它在字符串中,
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
String formattedDate = sdf.format(new Date());
其中 new Date()
是今天的日期,无论您放置什么日期对象,sdf 对象 (java.text.SimpleDateFormat
) 都会这样做。
更多可以参考java 8 docs of SimpleDateFormat
请尝试此解决方案。
public static String getStartDate() {
DateTimeFormatter oldPattern = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSSz");
DateTimeFormatter newPattern = DateTimeFormatter.ofPattern("yyyy-MM-dd");
LocalDateTime datetime = LocalDateTime.parse(new Date().toInstant().toString(), oldPattern);
return datetime.format(newPattern);
}