Java ThaiBuddhistChronology parse String like "10/29/2587 B.E."

Description

Java ThaiBuddhistChronology parse String like "10/29/2587 B.E."

import java.time.LocalDate;
import java.time.chrono.ChronoLocalDate;
import java.time.chrono.Chronology;
import java.time.chrono.ThaiBuddhistChronology;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeFormatterBuilder;
import java.time.format.DecimalStyle;
import java.time.temporal.TemporalAccessor;
import java.util.Locale;

public class Main {
  public static LocalDate fromString(String text, Chronology chrono) {
    Locale locale = Locale.getDefault(Locale.Category.FORMAT);
    String pattern = "M/d/yyyy GGGGG";
    DateTimeFormatter df = new DateTimeFormatterBuilder().parseLenient().appendPattern(pattern).toFormatter()
        .withChronology(chrono).withDecimalStyle(DecimalStyle.of(locale));
    TemporalAccessor temporal = df.parse(text);
    ChronoLocalDate cDate = chrono.date(temporal);
    return LocalDate.from(cDate);
  }//from   w w  w. j a va  2s .c o m
  public static void main(String[] args) {
    System.out.printf("%s%n", fromString("10/29/2587 B.E.", ThaiBuddhistChronology.INSTANCE));
  }
}



PreviousNext

Related