本文介绍了如何将LocalDate格式化为yyyyMMDD(不使用JodaTime)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试获取下一个下一个星期五的日期,并将其格式设置为yyyyMMDD.如果可能的话,我想在不使用JodaTime的情况下执行此操作.这是我的代码:

I am trying to get the date of the the next upcoming Friday and format it as yyyyMMDD. I would like to do this without using JodaTime if possible. Here is my code:

import java.time.LocalDate;
import java.time.temporal.TemporalAdjusters;
import java.time.DayOfWeek;
import java.time.format.DateTimeFormatter;

// snippet from main method
LocalDate friday = LocalDate.now().with(TemporalAdjusters.next(DayOfWeek.FRIDAY));
DateTimeFormatter formatter = DateTimeFormatter.ofPattern('yyyyMMDD');
System.out.println(friday.format(formatter));

但是当我运行它时,出现以下错误(今天运行20170809)

But when I run this I get the following error (running it today 20170809)

java.time.DateTimeException: Field DayOfYear cannot be printed as the value 223 exceeds the maximum print width of 2

我在做什么错了?

我正在使用Java 8

edit: I am using Java 8

推荐答案

D 表示每年的日期.您必须使用小 d .

Big D means day-of-year. You have to use small d.

因此,在您的情况下,请使用"yyyyMMdd" .

So in your case use "yyyyMMdd".

您可以在此处.

此特定模式内置于Java 8及更高版本中: DateTimeFormatter.BASIC_ISO_DATE

This particular pattern is built into Java 8 and later: DateTimeFormatter.BASIC_ISO_DATE

这篇关于如何将LocalDate格式化为yyyyMMDD(不使用JodaTime)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-20 22:56