0

To convert dd-mmm-yyyy to dd-mm-yyyy format.

String dateofbirth = ((JTextField) dobcalender.getDateEditor().getUiComponent()).getText();//date from jcalender

SimpleDateFormat myFormat = new SimpleDateFormat("dd-MM-yyyy");

tried these codes :

System.out.println(myFormat.format(myFormat.parse(dateofbirth)));
myFormat.format(myFormat.parse(dateofbirth));

showing error parseexception

  • What is the exact value of `dateofbirth`? Note that `MM` is not the same as `mm`. –  Aug 26 '19 at 07:04
  • Sir value of date of birth is 24 Feb 2019 – Aman R Singh Aug 26 '19 at 07:12
  • Can you show us the value contained by the String dateofbirth in your code ? – Patrick Aug 26 '19 at 07:17
  • I recommend you don’t use `SimpleDateFormat` and `Date`. Those classes are poorly designed and long outdated, the former in particular notoriously troublesome. Instead use `LocalDate` and `DateTimeFormatter`, both from [java.time, the modern Java date and time API](https://docs.oracle.com/javase/tutorial/datetime/). – Ole V.V. Aug 26 '19 at 08:11

3 Answers3

4

Based on the format "24 Feb 2019"

 SimpleDateFormat from=new SimpleDateFormat("dd MMM yyyy");
 SimpleDateFormat to=new SimpleDateFormat("dd-MM-yyyy");
 Date frm=from.parse("24 Feb 2019");
 System.out.println(frm);//Sun Feb 24 00:00:00 IST 2019
 System.out.println(to.format(frm));//24-02-2019
ArunKumar M N
  • 1,256
  • 1
  • 10
  • 22
1

First, you need to parse the String to Date object. Then you need to convert the Date object to a new formatted String. Here is the sample code:

String dateofbirth = "09-10-2010"; //date from jcalender
SimpleDateFormat myFormat = new SimpleDateFormat("dd-MM-yyyy");
// converting to Date object
Date date = myFormat.parse(dateofbirth);

SimpleDateFormat myFormat1 = new SimpleDateFormat("dd-MMM-yyyy");
// converting Date object to new format
String formattedDate = myFormat1.format(date);
System.out.println(formattedDate); // prints: 09-Oct-2010
Mushif Ali Nawaz
  • 3,707
  • 3
  • 18
  • 31
0
// Before Java8 , No thread safe , has to import multiple packages(util, text), throws checked Exception(ParseException)
    System.out.println(oldDate);    //21 Jul 2019
    SimpleDateFormat oldPattern = new SimpleDateFormat("dd MMM yyyy");
    SimpleDateFormat newPattern = new SimpleDateFormat("dd-MM-yyyy");
    try {
        Date date = oldPattern.parse(oldDate);
        String newDate = newPattern.format(date);
        System.out.println(newDate);   //21-07-2019
    } catch (ParseException e) {
        // Exception handling message/mechanism/logging as per company standard
    }

    // In Java8, Thread Safe, Immutable, throws unchecked Exception(DateTimeParseException)

    DateTimeFormatter oldPattern8 = DateTimeFormatter.ofPattern("dd MMM-yyyy");
    DateTimeFormatter newPattern8 = DateTimeFormatter.ofPattern("dd-MM-yyyy");

    LocalDate datetime = LocalDate.parse(oldDate, oldPattern8);
    String output = datetime.format(newPattern8);

    System.out.println(output);  //21-07-2019