我有一个名为date的LocalDate变量,当我打印它时显示1988-05-05,我需要将其转换为打印为05。1988年5月。如何做到这一点?
当前回答
在ProgrammersBlock帖子的帮助下,我想出了这个。我的需求略有不同。我需要获取一个字符串并将其作为LocalDate对象返回。我拿到的代码使用的是旧的Calendar和SimpleDateFormat。我想让它更与时俱进。这是我想到的。
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
void ExampleFormatDate() {
LocalDate formattedDate = null; //Declare LocalDate variable to receive the formatted date.
DateTimeFormatter dateTimeFormatter; //Declare date formatter
String rawDate = "2000-01-01"; //Test string that holds a date to format and parse.
dateTimeFormatter = DateTimeFormatter.ISO_LOCAL_DATE;
//formattedDate.parse(String string) wraps the String.format(String string, DateTimeFormatter format) method.
//First, the rawDate string is formatted according to DateTimeFormatter. Second, that formatted string is parsed into
//the LocalDate formattedDate object.
formattedDate = formattedDate.parse(String.format(rawDate, dateTimeFormatter));
}
希望这能帮助到一些人,如果有人看到了更好的方法来完成这个任务,请添加您的输入。
其他回答
在ProgrammersBlock帖子的帮助下,我想出了这个。我的需求略有不同。我需要获取一个字符串并将其作为LocalDate对象返回。我拿到的代码使用的是旧的Calendar和SimpleDateFormat。我想让它更与时俱进。这是我想到的。
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
void ExampleFormatDate() {
LocalDate formattedDate = null; //Declare LocalDate variable to receive the formatted date.
DateTimeFormatter dateTimeFormatter; //Declare date formatter
String rawDate = "2000-01-01"; //Test string that holds a date to format and parse.
dateTimeFormatter = DateTimeFormatter.ISO_LOCAL_DATE;
//formattedDate.parse(String string) wraps the String.format(String string, DateTimeFormatter format) method.
//First, the rawDate string is formatted according to DateTimeFormatter. Second, that formatted string is parsed into
//the LocalDate formattedDate object.
formattedDate = formattedDate.parse(String.format(rawDate, dateTimeFormatter));
}
希望这能帮助到一些人,如果有人看到了更好的方法来完成这个任务,请添加您的输入。
System.out.println(LocalDate.now().format(DateTimeFormatter.ofPattern("dd.MMMM yyyy")));
上面的答案就是今天的答案
可以简短为:
LocalDate.now().format(DateTimeFormatter.ofPattern("dd/MM/yyyy"));
在Joda库中有一个内置的方法来格式化LocalDate
import org.joda.time.LocalDate;
LocalDate localDate = LocalDate.now();
String dateFormat = "MM/dd/yyyy";
localDate.toString(dateFormat);
如果你还没有它-添加到build.gradle:
implementation 'joda-time:joda-time:2.9.5'
编码快乐!:)
SimpleDateFormat将不能工作,如果他开始与LocalDate,这是新的在Java 8。在我看来,您必须使用DateTimeFormatter, http://docs.oracle.com/javase/8/docs/api/java/time/format/DateTimeFormatter.html。
LocalDate localDate = LocalDate.now();//For reference
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd LLLL yyyy");
String formattedString = localDate.format(formatter);
应该是1988年5月5日。要获得日之后月之前的周期,可能必须使用“dd”。LLLL yyyy”
推荐文章
- Java 8接口方法中不允许“同步”的原因是什么?
- 如何找到Java堆大小和内存使用(Linux)?
- 使用Enum实现单例(Java)
- RabbitMQ与通道和连接之间的关系
- buildSessionFactory()配置方法在Hibernate中已弃用?
- Spring MVC -如何获得所有的请求参数在一个地图在Spring控制器?
- 如何在Java中按两个字段排序?
- 文件之间的差异。路径中的分隔符和斜杠
- 在方法参数中使用NotNull注释
- Spring MVC中处理可选参数的@RequestParam
- Tomcat:如何查找正在运行的Tomcat版本?
- “java”、“javaw”和“javaws”之间有什么区别?
- 将Date对象转换为日历对象
- 在Java中保存最后N个元素的大小有限的队列
- 如何运行一个类从Jar不是主类在其清单文件