2

Hello Please help me out, I have gone through many questions but didn't get a solution. Code

String localDate1="Miércoles, 04 Octubre 2017 12:00 PM";
Locale spanishLocale=new Locale("es", "ES");
SimpleDateFormat spanishLocale1=new SimpleDateFormat(getString(R.string.jom_events_date_input_format_12_hrs),spanishLocale);
String dateInSpanish=spanishLocale1.parse(localDate1).toString();
Log.v("@@@WWW","in Spanish: "+dateInSpanish);

Error

java.text.ParseException: Unparseable date: "Miércoles, 04 Octubre 2017 12:00 PM" (at offset 33)
Pratik Vyas
  • 644
  • 7
  • 20

3 Answers3

3

Just for the record:

You have fortunately posted your error message which points to the offset 33 (that is the position of "PM" in your input). So we can state:

Your problem is related to device-dependent localization data (or OS-dependent), here the concrete data for the Spanish representation of AM/PM. In old versions of CLDR-unicode repository (industry standard as common source for many Java-, C#- or Android distributions), the data "AM" and "PM" were used but in newer versions it uses "a. m." or "p. m." for Spanish.

So in case of mismatch between your input to be parsed (containing "PM") and the real i18n-data you have, I recommend as pragmatic solution string preprocessing:

String input = "Miércoles, 04 Octubre 2017 12:00 PM";
input = input.replace("PM", "p. m.");
// now parse your input with Spanish locale and the appropriate pattern
Meno Hochschild
  • 42,708
  • 7
  • 104
  • 126
0

Please check you spelling on this line

String localDate1="Miércoles, 04 Octubre 2017 12:00 PM";

change October instead of Octubre and also check this Miércoles

Raja
  • 2,775
  • 2
  • 19
  • 31
  • it is actually server generated date , and google translate translate it to Wednesday, 04 October 2017 12:00 PM – Pratik Vyas Sep 21 '17 at 05:24
  • The month name and the weekday-name are okay for Spanish (leaving aside the fact that it would rather not be capitalized but the parser is not case-sensitive - lenient by default). – Meno Hochschild Sep 21 '17 at 05:59
-1

You can use this code for your reference::

This code converts:--- miércoles, 04 octubre 2017 12:00 AM to Wed Oct 04 00:00:00 IST 2017

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

public class test {

    public static void main(String[] args) throws IOException, ParseException {
        //Wednesday, October 4, 2017
        String dateInString = "4-Oct-2017";
        SimpleDateFormat formatter = new SimpleDateFormat("dd-MMM-yyyy");
        Date date = formatter.parse(dateInString);

        SimpleDateFormat formato = new SimpleDateFormat("EEEE, dd MMMM yyyy hh:mm aaaa", new Locale("es", "ES"));
        String fecha = formato.format(date);
        System.out.println(fecha);

        String localDate1 = fecha;
        Locale spanishLocale = new Locale("es", "ES");
        String pattern = "E, dd MMMM yyyy hh:mm aaaa";
        SimpleDateFormat spanishLocale1 = new SimpleDateFormat(pattern, spanishLocale);
        String dateInSpanish = spanishLocale1.parse(localDate1).toString();
        System.out.println(dateInSpanish);
    }
}
Abhishek Honey
  • 645
  • 4
  • 13