我在尝试密码
Date today = new Date();
Date todayWithZeroTime;
{
try {
todayWithZeroTime = formatter.parse(formatter.format(today));
} catch (ParseException e) {
e.printStackTrace();
}
}
String date = todayWithZeroTime.toString();它提供输出:- Wed Dec 11 00:00:00 :00 IST 2019,我想要2019/ 11/12/2019,其中11/12/2019是今天的日期。
发布于 2019-12-11 12:08:06
使用java.time.LocalDate,
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy/MM/dd");
LocalDate localDate = LocalDate.now();
System.out.println(dtf.format(localDate)); //2016/11/16使用DateTimeFormatter将日期设置为所需的格式。
在您的例子中,模式是"dd/MM/yyyy“。
信息⬇️
Java8为日期和时间引入了新的API,以解决旧的java.util.Date和java.util.Calendar的缺点。作为java.time包一部分的新Java8项目的核心类,如LocalDate、LocalTime、LocalDateTime、ZonedDateTime、Period、Duration及其支持的API。
LocalDate提供了各种实用方法来获取各种信息。例如:
1)下面的代码片段获取当前本地日期并添加一天:
LocalDate tomorrow = LocalDate.now().plusDays(1);2)此示例获取当前日期并减去一个月。注意它是如何接受枚举作为时间单位的:
LocalDate previousMonthSameDay = LocalDate.now().minus(1, ChronoUnit.MONTHS);发布于 2019-12-11 12:13:15
如果使用的是JAVA 8,则可以使用LocalDateTime类和DateTimeFormatter。
参见下面使用JAVA 8的示例:
LocalDateTime now = LocalDateTime.now();
System.out.println("Current DateTime Before Formatting: " + now);
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd/MM/yyyy");
String formatedDateTime = now.format(formatter);
System.out.println("Current DateTime after Formatting:: " + formatedDateTime );https://stackoverflow.com/questions/59285466
复制相似问题