Howto use org.simpleframework.xml with Locale and Calendar without exception Unparseable date












0















I am having issues with the org.simpleframework.xml.Serializer, Calendar and Locale.



When Locale is English US the Calendar is saved as:



<dateManufature class="java.util.GregorianCalendar">2018-11-20 08:58:35.0 EST</dateManufature>



When Locale is Spanish Mexico the Calendar is saved as:



'<dateManufature class="java.util.GregorianCalendar">2018-11-20 08:41:27.9 GMT-05:00</dateManufature>'


The issue is when the file is saved as Locale en_us first then when the Locale is changed to es_mx, then when reading the file there is an Exception error



'Unparseable date: "2018-11-20 08:58:35.0 EST"'


Surprisingly when en_us reads the es_mx file, there isn't an exception and it simply converts the date from GMT to EST, so maybe there is a way for Serializer to save Calendar with GMT in en_us?



How do I get org.simpleframework.xml.Serializer to read any Calendar date regardless of what the Locale it was created in?



project is checked in
https://github.com/amccombsahce/mySerializer



simply change commented out code in updateBaseContextLocale function to change from US to MX.



MainActivity.java



public class MainActivity extends AppCompatActivity
{

private Context _context;
private Locale _locale;
private String _filename;
private String _foldername;

public Cars _cars;

private TextView layout_textview_carcount;

@Override
protected void attachBaseContext(Context base)
{
super.attachBaseContext(updateBaseContextLocale(base));
}
public Context updateBaseContextLocale(Context context)
{
_locale = new Locale("es", "MX");
//_locale = new Locale("en", "US");

Configuration configuration = context.getResources().getConfiguration();
configuration.setLocale(_locale);
Locale.setDefault(_locale);
_context = context.createConfigurationContext(configuration);
return _context;
}
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

ActivityCompat.requestPermissions(this, new String {
Manifest.permission.WRITE_EXTERNAL_STORAGE,
Manifest.permission.READ_EXTERNAL_STORAGE}, 1);

layout_textview_carcount = findViewById(R.id.layout_textview_carcount);
setTitle(getString(R.string.app_name));

_foldername = String.format("%s/%s", Environment.getExternalStorageDirectory().getAbsolutePath(),
_context.getString(R.string.app_name_folder));

File folders = new File(_foldername);
boolean b = folders.mkdirs();

_filename = String.format("%s/%s", _foldername, "cars.xml");


_cars = new Cars(_context);

readFile();

layout_textview_carcount.setText(String.format(_locale, "%d", _cars.getSize()));

}

protected void buttonClick(View v)
{
Car car = new Car();
_cars.addCar(car);

writeFile();

layout_textview_carcount.setText(String.format(_locale, "%d", _cars.getSize()));
}
public boolean readFile()
{
boolean bReturn = false;
try
{
File file = new File(_filename);
if (file.exists())
{
org.simpleframework.xml.Serializer serializerREAD = new Persister();
serializerREAD.read(_cars, file);
bReturn = true;
}
}
catch (Exception ex)
{
String error = ex.getLocalizedMessage();

int breakpoint = 1;

// unparsable date
if (ex.getMessage().contains("Unparseable date"))
{

File file = new File(_filename);
file.delete();
}

}
return bReturn;
}
public boolean writeFile()
{
boolean bReturn = false;
try
{
File file = new File(_filename);
org.simpleframework.xml.Serializer serializer = new Persister();
serializer.write(_cars, file);
bReturn = true;
}
catch (Exception ex)
{
String error = ex.getLocalizedMessage();
}
return bReturn;
}


}



Car.java



@Root
public class Car implements java.io.Serializable
{
@Element
private int carID;
@Element(required=false)
private Calendar dateManufature;

private Car()
{

}
public Car(Context context)
{
_context = context;

Configuration configuration = _context.getResources().getConfiguration();
Locale locale = null;
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N)
{
locale = configuration.getLocales().get(0);
}
else
{
locale = context.getResources().getConfiguration().locale;
}

dateManufature = Calendar.getInstance(locale);
java.util.Date date = new java.util.Date();
dateManufature.setTime(date);

}

public void setCarID(int value)
{
carID = value;
}
}


Cars.java



@Root(name = "myserialzer")
public class Cars implements java.io.Serializable
{

private Context _context;

@ElementList(required=false, name="cars")
private ArrayList<Car> mycars = new ArrayList<>();

public Cars(Context context)
{
_context = context;
}
public ArrayList<Car> getCars() { return mycars; }

public void addCar(Car car)
{
car.setCarID(mycars.size() + 1);
mycars.add(car);
}

public int getSize()
{
return mycars.size();
}
}









share|improve this question



























    0















    I am having issues with the org.simpleframework.xml.Serializer, Calendar and Locale.



    When Locale is English US the Calendar is saved as:



    <dateManufature class="java.util.GregorianCalendar">2018-11-20 08:58:35.0 EST</dateManufature>



    When Locale is Spanish Mexico the Calendar is saved as:



    '<dateManufature class="java.util.GregorianCalendar">2018-11-20 08:41:27.9 GMT-05:00</dateManufature>'


    The issue is when the file is saved as Locale en_us first then when the Locale is changed to es_mx, then when reading the file there is an Exception error



    'Unparseable date: "2018-11-20 08:58:35.0 EST"'


    Surprisingly when en_us reads the es_mx file, there isn't an exception and it simply converts the date from GMT to EST, so maybe there is a way for Serializer to save Calendar with GMT in en_us?



    How do I get org.simpleframework.xml.Serializer to read any Calendar date regardless of what the Locale it was created in?



    project is checked in
    https://github.com/amccombsahce/mySerializer



    simply change commented out code in updateBaseContextLocale function to change from US to MX.



    MainActivity.java



    public class MainActivity extends AppCompatActivity
    {

    private Context _context;
    private Locale _locale;
    private String _filename;
    private String _foldername;

    public Cars _cars;

    private TextView layout_textview_carcount;

    @Override
    protected void attachBaseContext(Context base)
    {
    super.attachBaseContext(updateBaseContextLocale(base));
    }
    public Context updateBaseContextLocale(Context context)
    {
    _locale = new Locale("es", "MX");
    //_locale = new Locale("en", "US");

    Configuration configuration = context.getResources().getConfiguration();
    configuration.setLocale(_locale);
    Locale.setDefault(_locale);
    _context = context.createConfigurationContext(configuration);
    return _context;
    }
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    ActivityCompat.requestPermissions(this, new String {
    Manifest.permission.WRITE_EXTERNAL_STORAGE,
    Manifest.permission.READ_EXTERNAL_STORAGE}, 1);

    layout_textview_carcount = findViewById(R.id.layout_textview_carcount);
    setTitle(getString(R.string.app_name));

    _foldername = String.format("%s/%s", Environment.getExternalStorageDirectory().getAbsolutePath(),
    _context.getString(R.string.app_name_folder));

    File folders = new File(_foldername);
    boolean b = folders.mkdirs();

    _filename = String.format("%s/%s", _foldername, "cars.xml");


    _cars = new Cars(_context);

    readFile();

    layout_textview_carcount.setText(String.format(_locale, "%d", _cars.getSize()));

    }

    protected void buttonClick(View v)
    {
    Car car = new Car();
    _cars.addCar(car);

    writeFile();

    layout_textview_carcount.setText(String.format(_locale, "%d", _cars.getSize()));
    }
    public boolean readFile()
    {
    boolean bReturn = false;
    try
    {
    File file = new File(_filename);
    if (file.exists())
    {
    org.simpleframework.xml.Serializer serializerREAD = new Persister();
    serializerREAD.read(_cars, file);
    bReturn = true;
    }
    }
    catch (Exception ex)
    {
    String error = ex.getLocalizedMessage();

    int breakpoint = 1;

    // unparsable date
    if (ex.getMessage().contains("Unparseable date"))
    {

    File file = new File(_filename);
    file.delete();
    }

    }
    return bReturn;
    }
    public boolean writeFile()
    {
    boolean bReturn = false;
    try
    {
    File file = new File(_filename);
    org.simpleframework.xml.Serializer serializer = new Persister();
    serializer.write(_cars, file);
    bReturn = true;
    }
    catch (Exception ex)
    {
    String error = ex.getLocalizedMessage();
    }
    return bReturn;
    }


    }



    Car.java



    @Root
    public class Car implements java.io.Serializable
    {
    @Element
    private int carID;
    @Element(required=false)
    private Calendar dateManufature;

    private Car()
    {

    }
    public Car(Context context)
    {
    _context = context;

    Configuration configuration = _context.getResources().getConfiguration();
    Locale locale = null;
    if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N)
    {
    locale = configuration.getLocales().get(0);
    }
    else
    {
    locale = context.getResources().getConfiguration().locale;
    }

    dateManufature = Calendar.getInstance(locale);
    java.util.Date date = new java.util.Date();
    dateManufature.setTime(date);

    }

    public void setCarID(int value)
    {
    carID = value;
    }
    }


    Cars.java



    @Root(name = "myserialzer")
    public class Cars implements java.io.Serializable
    {

    private Context _context;

    @ElementList(required=false, name="cars")
    private ArrayList<Car> mycars = new ArrayList<>();

    public Cars(Context context)
    {
    _context = context;
    }
    public ArrayList<Car> getCars() { return mycars; }

    public void addCar(Car car)
    {
    car.setCarID(mycars.size() + 1);
    mycars.add(car);
    }

    public int getSize()
    {
    return mycars.size();
    }
    }









    share|improve this question

























      0












      0








      0








      I am having issues with the org.simpleframework.xml.Serializer, Calendar and Locale.



      When Locale is English US the Calendar is saved as:



      <dateManufature class="java.util.GregorianCalendar">2018-11-20 08:58:35.0 EST</dateManufature>



      When Locale is Spanish Mexico the Calendar is saved as:



      '<dateManufature class="java.util.GregorianCalendar">2018-11-20 08:41:27.9 GMT-05:00</dateManufature>'


      The issue is when the file is saved as Locale en_us first then when the Locale is changed to es_mx, then when reading the file there is an Exception error



      'Unparseable date: "2018-11-20 08:58:35.0 EST"'


      Surprisingly when en_us reads the es_mx file, there isn't an exception and it simply converts the date from GMT to EST, so maybe there is a way for Serializer to save Calendar with GMT in en_us?



      How do I get org.simpleframework.xml.Serializer to read any Calendar date regardless of what the Locale it was created in?



      project is checked in
      https://github.com/amccombsahce/mySerializer



      simply change commented out code in updateBaseContextLocale function to change from US to MX.



      MainActivity.java



      public class MainActivity extends AppCompatActivity
      {

      private Context _context;
      private Locale _locale;
      private String _filename;
      private String _foldername;

      public Cars _cars;

      private TextView layout_textview_carcount;

      @Override
      protected void attachBaseContext(Context base)
      {
      super.attachBaseContext(updateBaseContextLocale(base));
      }
      public Context updateBaseContextLocale(Context context)
      {
      _locale = new Locale("es", "MX");
      //_locale = new Locale("en", "US");

      Configuration configuration = context.getResources().getConfiguration();
      configuration.setLocale(_locale);
      Locale.setDefault(_locale);
      _context = context.createConfigurationContext(configuration);
      return _context;
      }
      @Override
      protected void onCreate(Bundle savedInstanceState)
      {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);

      ActivityCompat.requestPermissions(this, new String {
      Manifest.permission.WRITE_EXTERNAL_STORAGE,
      Manifest.permission.READ_EXTERNAL_STORAGE}, 1);

      layout_textview_carcount = findViewById(R.id.layout_textview_carcount);
      setTitle(getString(R.string.app_name));

      _foldername = String.format("%s/%s", Environment.getExternalStorageDirectory().getAbsolutePath(),
      _context.getString(R.string.app_name_folder));

      File folders = new File(_foldername);
      boolean b = folders.mkdirs();

      _filename = String.format("%s/%s", _foldername, "cars.xml");


      _cars = new Cars(_context);

      readFile();

      layout_textview_carcount.setText(String.format(_locale, "%d", _cars.getSize()));

      }

      protected void buttonClick(View v)
      {
      Car car = new Car();
      _cars.addCar(car);

      writeFile();

      layout_textview_carcount.setText(String.format(_locale, "%d", _cars.getSize()));
      }
      public boolean readFile()
      {
      boolean bReturn = false;
      try
      {
      File file = new File(_filename);
      if (file.exists())
      {
      org.simpleframework.xml.Serializer serializerREAD = new Persister();
      serializerREAD.read(_cars, file);
      bReturn = true;
      }
      }
      catch (Exception ex)
      {
      String error = ex.getLocalizedMessage();

      int breakpoint = 1;

      // unparsable date
      if (ex.getMessage().contains("Unparseable date"))
      {

      File file = new File(_filename);
      file.delete();
      }

      }
      return bReturn;
      }
      public boolean writeFile()
      {
      boolean bReturn = false;
      try
      {
      File file = new File(_filename);
      org.simpleframework.xml.Serializer serializer = new Persister();
      serializer.write(_cars, file);
      bReturn = true;
      }
      catch (Exception ex)
      {
      String error = ex.getLocalizedMessage();
      }
      return bReturn;
      }


      }



      Car.java



      @Root
      public class Car implements java.io.Serializable
      {
      @Element
      private int carID;
      @Element(required=false)
      private Calendar dateManufature;

      private Car()
      {

      }
      public Car(Context context)
      {
      _context = context;

      Configuration configuration = _context.getResources().getConfiguration();
      Locale locale = null;
      if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N)
      {
      locale = configuration.getLocales().get(0);
      }
      else
      {
      locale = context.getResources().getConfiguration().locale;
      }

      dateManufature = Calendar.getInstance(locale);
      java.util.Date date = new java.util.Date();
      dateManufature.setTime(date);

      }

      public void setCarID(int value)
      {
      carID = value;
      }
      }


      Cars.java



      @Root(name = "myserialzer")
      public class Cars implements java.io.Serializable
      {

      private Context _context;

      @ElementList(required=false, name="cars")
      private ArrayList<Car> mycars = new ArrayList<>();

      public Cars(Context context)
      {
      _context = context;
      }
      public ArrayList<Car> getCars() { return mycars; }

      public void addCar(Car car)
      {
      car.setCarID(mycars.size() + 1);
      mycars.add(car);
      }

      public int getSize()
      {
      return mycars.size();
      }
      }









      share|improve this question














      I am having issues with the org.simpleframework.xml.Serializer, Calendar and Locale.



      When Locale is English US the Calendar is saved as:



      <dateManufature class="java.util.GregorianCalendar">2018-11-20 08:58:35.0 EST</dateManufature>



      When Locale is Spanish Mexico the Calendar is saved as:



      '<dateManufature class="java.util.GregorianCalendar">2018-11-20 08:41:27.9 GMT-05:00</dateManufature>'


      The issue is when the file is saved as Locale en_us first then when the Locale is changed to es_mx, then when reading the file there is an Exception error



      'Unparseable date: "2018-11-20 08:58:35.0 EST"'


      Surprisingly when en_us reads the es_mx file, there isn't an exception and it simply converts the date from GMT to EST, so maybe there is a way for Serializer to save Calendar with GMT in en_us?



      How do I get org.simpleframework.xml.Serializer to read any Calendar date regardless of what the Locale it was created in?



      project is checked in
      https://github.com/amccombsahce/mySerializer



      simply change commented out code in updateBaseContextLocale function to change from US to MX.



      MainActivity.java



      public class MainActivity extends AppCompatActivity
      {

      private Context _context;
      private Locale _locale;
      private String _filename;
      private String _foldername;

      public Cars _cars;

      private TextView layout_textview_carcount;

      @Override
      protected void attachBaseContext(Context base)
      {
      super.attachBaseContext(updateBaseContextLocale(base));
      }
      public Context updateBaseContextLocale(Context context)
      {
      _locale = new Locale("es", "MX");
      //_locale = new Locale("en", "US");

      Configuration configuration = context.getResources().getConfiguration();
      configuration.setLocale(_locale);
      Locale.setDefault(_locale);
      _context = context.createConfigurationContext(configuration);
      return _context;
      }
      @Override
      protected void onCreate(Bundle savedInstanceState)
      {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);

      ActivityCompat.requestPermissions(this, new String {
      Manifest.permission.WRITE_EXTERNAL_STORAGE,
      Manifest.permission.READ_EXTERNAL_STORAGE}, 1);

      layout_textview_carcount = findViewById(R.id.layout_textview_carcount);
      setTitle(getString(R.string.app_name));

      _foldername = String.format("%s/%s", Environment.getExternalStorageDirectory().getAbsolutePath(),
      _context.getString(R.string.app_name_folder));

      File folders = new File(_foldername);
      boolean b = folders.mkdirs();

      _filename = String.format("%s/%s", _foldername, "cars.xml");


      _cars = new Cars(_context);

      readFile();

      layout_textview_carcount.setText(String.format(_locale, "%d", _cars.getSize()));

      }

      protected void buttonClick(View v)
      {
      Car car = new Car();
      _cars.addCar(car);

      writeFile();

      layout_textview_carcount.setText(String.format(_locale, "%d", _cars.getSize()));
      }
      public boolean readFile()
      {
      boolean bReturn = false;
      try
      {
      File file = new File(_filename);
      if (file.exists())
      {
      org.simpleframework.xml.Serializer serializerREAD = new Persister();
      serializerREAD.read(_cars, file);
      bReturn = true;
      }
      }
      catch (Exception ex)
      {
      String error = ex.getLocalizedMessage();

      int breakpoint = 1;

      // unparsable date
      if (ex.getMessage().contains("Unparseable date"))
      {

      File file = new File(_filename);
      file.delete();
      }

      }
      return bReturn;
      }
      public boolean writeFile()
      {
      boolean bReturn = false;
      try
      {
      File file = new File(_filename);
      org.simpleframework.xml.Serializer serializer = new Persister();
      serializer.write(_cars, file);
      bReturn = true;
      }
      catch (Exception ex)
      {
      String error = ex.getLocalizedMessage();
      }
      return bReturn;
      }


      }



      Car.java



      @Root
      public class Car implements java.io.Serializable
      {
      @Element
      private int carID;
      @Element(required=false)
      private Calendar dateManufature;

      private Car()
      {

      }
      public Car(Context context)
      {
      _context = context;

      Configuration configuration = _context.getResources().getConfiguration();
      Locale locale = null;
      if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N)
      {
      locale = configuration.getLocales().get(0);
      }
      else
      {
      locale = context.getResources().getConfiguration().locale;
      }

      dateManufature = Calendar.getInstance(locale);
      java.util.Date date = new java.util.Date();
      dateManufature.setTime(date);

      }

      public void setCarID(int value)
      {
      carID = value;
      }
      }


      Cars.java



      @Root(name = "myserialzer")
      public class Cars implements java.io.Serializable
      {

      private Context _context;

      @ElementList(required=false, name="cars")
      private ArrayList<Car> mycars = new ArrayList<>();

      public Cars(Context context)
      {
      _context = context;
      }
      public ArrayList<Car> getCars() { return mycars; }

      public void addCar(Car car)
      {
      car.setCarID(mycars.size() + 1);
      mycars.add(car);
      }

      public int getSize()
      {
      return mycars.size();
      }
      }






      java android calendar locale






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 20 '18 at 15:13









      user3554851user3554851

      85




      85
























          0






          active

          oldest

          votes











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53396031%2fhowto-use-org-simpleframework-xml-with-locale-and-calendar-without-exception-unp%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          0






          active

          oldest

          votes








          0






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes
















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53396031%2fhowto-use-org-simpleframework-xml-with-locale-and-calendar-without-exception-unp%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Guess what letter conforming each word

          Port of Spain

          Run scheduled task as local user group (not BUILTIN)