Home > Back-end >  How to convert CharSequence to Date?
How to convert CharSequence to Date?

Time:10-02

I have CharSequence inside it I have Date Like CharSequence startDate="1998/10/05"; I need date format like DD/MM/YYYY

CodePudding user response:

Use LocalDate class to parse and generate text in various formats.

DateTimeFormatter in = DateTimeFormatter.ofPattern( "uuuu/MM/dd" ) ; 
LocalDate ld = LocalDate.parse( "1998/10/05" , in ) ;

DateTimeFormatter out = DateTimeFormatter.ofPattern( "dd/MM/uuuu" ) ; 
String output = ld.format( out ) ;

Tip: Educate the publisher of your data about the ISO 8601 standard for exchanging date-time values textually.

CodePudding user response:

public class Test1 {

public static void main(String[] args) {

    CharSequence date = "1996/12/25";
    CharSequence formattedDate = date.subSequence(8, 10)   "/"   date.subSequence(5, 7)   "/"
              date.subSequence(0, 4);

    System.out.println(date);
    System.out.println(formattedDate);

    }
  • Related