Java HijrahChronology parse String like "6/16/1466 1"

Description

Java HijrahChronology parse String like "6/16/1466 1"

import java.time.LocalDate;
import java.time.chrono.ChronoLocalDate;
import java.time.chrono.Chronology;
import java.time.chrono.HijrahChronology;
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);
  }//www. ja v  a 2  s  .  c o m
  public static void main(String[] args) {
    System.out.printf("%s%n", fromString("6/16/1466 1", HijrahChronology.INSTANCE));
  }
}



PreviousNext

Related