解析在Spring Boot中的伊斯兰历日期

huangapple go评论71阅读模式
英文:

Parsing Hijri Date in Spring Boot

问题

我想在Spring Boot中解析Hijri日期。在我的应用程序中,Hijri日期将显示为"yyyy-MM-dd"。我希望将其解析为"dd/MM/yyyy"的格式。对于29/0230/02日期,我有问题,它们被视为分别是01/0302/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

huangapple
  • 本文由 发表于 2020年9月23日 22:07:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/64029809.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定