我有一个名为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));
}
希望这能帮助到一些人,如果有人看到了更好的方法来完成这个任务,请添加您的输入。
其他回答
在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'
编码快乐!:)
可以简短为:
LocalDate.now().format(DateTimeFormatter.ofPattern("dd/MM/yyyy"));
System.out.println(LocalDate.now().format(DateTimeFormatter.ofPattern("dd.MMMM yyyy")));
上面的答案就是今天的答案
在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));
}
希望这能帮助到一些人,如果有人看到了更好的方法来完成这个任务,请添加您的输入。
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”
推荐文章
- 将字节转换为十六进制
- HashSet和HashMap的区别?
- 我如何有效地解析HTML与Java?
- Java构造函数继承
- 如何在Java中将int[]转换为Integer[] ?
- 你能解释一下流的概念吗?
- 导致java.lang.VerifyError错误的原因
- 如何在Java中监控计算机的CPU、内存和磁盘使用情况?
- 如何设置超时在改造库?
- java lambda可以有一个以上的参数吗?
- HashMap -获取第一个键值
- 使用Jackson将JSON字符串转换为漂亮的打印JSON输出
- Android - SPAN_EXCLUSIVE_EXCLUSIVE跨度不能为零长度
- Javadoc @see或{@link}?
- 在准备语句中使用“like”通配符