我在org.simpleframework.xml.Serializer,日历和语言环境方面遇到问题。
当语言环境为美国英语时,日历另存为:
<dateManufature class="java.util.GregorianCalendar">2018-11-20 08:58:35.0 EST</dateManufature>
当语言环境为西班牙墨西哥时,日历另存为:
'<dateManufature class="java.util.GregorianCalendar">2018-11-20 08:41:27.9 GMT-05:00</dateManufature>'
问题是,首先将文件另存为Locale en_us,然后将Locale更改为es_mx,然后在读取文件时出现异常错误
'Unparseable date: "2018-11-20 08:58:35.0 EST"'
令人惊讶的是,当en_us读取es_mx文件时,没有异常,它只是将日期从GMT转换为EST,所以也许序列化程序可以将带有GMT的日历保存在en_us中?
我如何使org.simpleframework.xml.Serializer读取任何日历日期,而不管它在什么语言环境中创建?
项目已签入
https://github.com/amccombsahce/mySerializer
只需将updateBaseContextLocale函数中注释掉的代码更改为从美国更改为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();
}
}