0

I have a date that comes to me as a string type as follows:

String fecha = "23 de marzo de 2021";

But I want to change the date as follows:

String resultado = "23-03-2021";

I tried using the following but can't:

try {
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
    Date date1 = sdf.parse("23 de marzo de 2021");
    System.out.println("Fecha --->" + date1);
} catch (Exception e) {
    System.out.println(e);
}
Arvind Kumar Avinash
  • 71,965
  • 6
  • 74
  • 110
DANIEL
  • 27
  • 5
  • 1
    I’m voting to close this question because it is not in English – Dharman Mar 25 '21 at 20:31
  • @Dharman - I've translated the three sentences into English leaving the code as it is. – Arvind Kumar Avinash Mar 25 '21 at 20:44
  • @ArvindKumarAvinash I see, but we really shouldn't be translating questions. Please close them next time. I don't this question will be very useful in the future. Maybe you know of a duplicate that could be used to close this question instead? – Dharman Mar 25 '21 at 20:45
  • @Dharman - There is no exact duplicate e.g. as mentioned in this comment, https://stackoverflow.com/questions/66665987/parse-oracle-date-to-java#comment117882069_66665987, there are differences from duplicate targets which came from my searches. – Arvind Kumar Avinash Mar 25 '21 at 20:46
  • 4
    Hi @DANIEL, does the translation accurately reflect your words/question? It is the asker's responsibility to post in English on Stack Overflow, not an answerer's prerogative. – TylerH Mar 25 '21 at 20:52

1 Answers1

1
  1. Use the correct format which is dd 'de' MMMM 'de' yyyy for your date string.
  2. Always use Locale with date-time formatting/parsing API.

Demo:

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

public class Main {
    public static void main(String[] args) throws ParseException {
        SimpleDateFormat sdfInput = new SimpleDateFormat("dd 'de' MMMM 'de' yyyy", new Locale("es", "ES"));
        Date date1 = sdfInput.parse("23 de marzo de 2021");

        // Default string i.e. Date#toString implementation
        System.out.println(date1);

        // Custom string
        SimpleDateFormat sdfOutput = new SimpleDateFormat("dd-MM-yyyy", new Locale("es", "ES"));
        System.out.println(sdfOutput.format(date1));
    }
}

Output:

Tue Mar 23 00:00:00 GMT 2021
23-03-2021

Note that the java.util date-time API and their formatting API, SimpleDateFormat are outdated and error-prone. It is recommended to stop using them completely and switch to the modern date-time API* .

Using modern date-time API:

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.Locale;

public class Main {
    public static void main(String[] args) {
        DateTimeFormatter dtfInput = DateTimeFormatter.ofPattern("dd 'de' MMMM 'de' uuuu", new Locale("es", "ES"));
        LocalDate date1 = LocalDate.parse("23 de marzo de 2021", dtfInput);

        // Default string i.e. LocalDate#toString implementation
        System.out.println(date1);

        // Custom string
        DateTimeFormatter dtfOutput = DateTimeFormatter.ofPattern("dd-MM-uuuu", new Locale("es", "ES"));
        System.out.println(dtfOutput.format(date1));
    }
}

Output:

2021-03-23
23-03-2021

Learn more about the modern date-time API from Trail: Date Time.


* For any reason, if you have to stick to Java 6 or Java 7, you can use ThreeTen-Backport which backports most of the java.time functionality to Java 6 & 7. If you are working for an Android project and your Android API level is still not compliant with Java-8, check Java 8+ APIs available through desugaring and How to use ThreeTenABP in Android Project.

Arvind Kumar Avinash
  • 71,965
  • 6
  • 74
  • 110
  • 2
    Please do not answer non-English questions. They're off-topic on Stack Overflow and must be closed with reason "Needs clarity" or "Not suitable..." – Dharman Mar 25 '21 at 20:31