[英]“How to format LocalDate with Custom Pattern” in Java
I am new to JodaTime and learning it to my own. 我是JodaTime的新手,自己学习。 Actually what I want is to just format the LocateDate
in my own format. 实际上,我想要的只是将LocateDate
格式化为自己的格式。 My format is "dd-mm-yyyy"
我的格式是"dd-mm-yyyy"
I have method that calculates the difference with two dates: 我有一种方法可以计算两个日期之间的差异:
private void sampleDaysDifference() {
DateTime todayDate = getLocalTodayDate();
DateTime dateAfterTwoDays = getDateAfterTwoDays();
//get the days difference
int differenceOfDates = Days.daysBetween(todayDate, dateAfterTwoDays).getDays();
Log.e("logX","differenceOfDates: " + differenceOfDates);
}
To get the today date I am using: 要获取今天的日期,我正在使用:
private DateTime getLocalTodayDate() {
LocalDate now = LocalDate.now();
DateTimeFormatter fmt = DateTimeFormat.forPattern(DATE_FORMAT);
return fmt.parseDateTime(now.toString());//return the today date
}
and to get the date after two days: 并在两天后获得日期:
private DateTime getDateAfterTwoDays() {
LocalDate now = LocalDate.now();
DateTimeFormatter fmt = DateTimeFormat.forPattern(DATE_FORMAT);
return fmt.parseDateTime(now.plusDays(2).toString());//return date after two days
}
The problem is that I have no idea how to format date using JodaTime, can somebody please tell me how to format a JodaTime LocalDate
! 问题是我不知道如何使用JodaTime格式化日期,有人可以告诉我如何格式化JodaTime LocalDate
!
Actually my app is crashing with the stacktrace: 实际上,我的应用程序因堆栈跟踪而崩溃:
Caused by: java.lang.IllegalArgumentException: Invalid format: "16 January, 2019" is malformed at " January, 2019"
You are almost there but just the pattern is wrong that you specified, change "dd-mm-yyyy"
to this "dd-MM-yyyy"
docs-for-patterns 您快到了,但是只是您指定的模式错误,请将"dd-mm-yyyy"
更改为此"dd-MM-yyyy"
docs-for-patterns
Simple Example 简单的例子
System.out.println(LocalDate.now().format(DateTimeFormatter.ofPattern("dd-MM-yyyy"))); //16-01-2019
From Joda DateTimeFormatter
joda-docs 来自Joda DateTimeFormatter
joda-docs
The pattern syntax is mostly compatible with java.text.SimpleDateFormat 模式语法主要与java.text.SimpleDateFormat兼容
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.