英文:
Parsing Hijri Date in Spring Boot
问题
我想在Spring Boot中解析Hijri日期。在我的应用程序中,Hijri日期将显示为"yyyy-MM-dd"
。我希望将其解析为"dd/MM/yyyy"
的格式。对于29/02
和30/02
日期,我有问题,它们被视为分别是01/03
和02/03
。
英文:
I want to parse Hijri Date in Spring Boot. In my application Hijri date will come as "yyyy-MM-dd"
. I want to parse it in the "dd/MM/yyyy"
format. I am having issue for 29/02
and 30/02
date consider as 01/03
and 02/03
respectively.
答案1
得分: 1
Your input date-format is same as the ISO8601 format and therefore you won't need to define a DateTimeFormatter
for it. However, you will need to define one for the output string.
import java.time.LocalDate;
import java.time.chrono.HijrahChronology;
import java.time.chrono.HijrahDate;
import java.time.format.DateTimeFormatter;
public class Main {
public static void main(String[] args) {
// Given date-string
String dateStr = "2020-09-23";
LocalDate date = LocalDate.parse(dateStr);
System.out.println(date);
// Formatter for output string
DateTimeFormatter outputFormatter = DateTimeFormatter.ofPattern("dd/MM/yyyy");
// Get HijrahDate corresponding to the given LocalDate
HijrahDate hijrahDate = HijrahChronology.INSTANCE.date(date);
// Print the default format
System.out.println(hijrahDate);
// Print the string using custom format
System.out.println(hijrahDate.format(outputFormatter));
}
}
Output:
2020-09-23
Hijrah-umalqura AH 1442-02-06
06/02/1442
英文:
Your input date-format is same as the ISO8601 format and therefore you won't need to define a DateTimeFormatter
for it. However, you will need to define one for the output string.
import java.time.LocalDate;
import java.time.chrono.HijrahChronology;
import java.time.chrono.HijrahDate;
import java.time.format.DateTimeFormatter;
public class Main {
public static void main(String[] args) {
// Given date-string
String dateStr = "2020-09-23";
LocalDate date = LocalDate.parse(dateStr);
System.out.println(date);
// Formatter for output string
DateTimeFormatter outputFormatter = DateTimeFormatter.ofPattern("dd/MM/yyyy");
// Get HijrahDate corresponding to the given LocalDate
HijrahDate hijrahDate = HijrahChronology.INSTANCE.date(date);
// Print the default format
System.out.println(hijrahDate);
// Print the string using custom format
System.out.println(hijrahDate.format(outputFormatter));
}
}
Output:
2020-09-23
Hijrah-umalqura AH 1442-02-06
06/02/1442
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论