简体   繁体   English

如何将字符串转换为Calendar对象?

[英]How to convert a String to a Calendar object?

To store a Calendar object in SQLite database, i found that the easiest way is to convert the Calendar object to a string and store it in the database as text. 为了将Calendar对象存储在SQLite数据库中,我发现最简单的方法是将Calendar对象转换为字符串并将其作为文本存储在数据库中。

Now, the problem lies in extracting the stored date from the string. 现在,问题出在从字符串中提取存储的日期。

How do I parse the string containing the Calendar object and set it to a Calendar value? 如何解析包含Calendar对象的字符串并将其设置为Calendar值?

My code is: 我的代码是:

    String CREATE_DOCTORS_TABLE = "CREATE TABLE " + TABLE_DOCTORS + "("
            + KEY_ID_DOC + " INTEGER PRIMARY KEY," + KEY_DOCTOR_NAME + " TEXT,"             
            + KEY_CLINIC_ADDRESS + " TEXT," + KEY_LAST_CHECKUP + " TEXT" + ");";
    db.execSQL(CREATE_DOCTORS_TABLE);       

where KEY_LAST_CHECKUP contains a value like this: 其中KEY_LAST_CHECKUP包含这样的值:

java.util.GregorianCalendar[time=?,areFieldsSet=false,lenient=true,zone=Asia/Calcutta,firstDayOfWeek=1,minimalDaysInFirstWeek=1,ERA=1,YEAR=2013,MONTH=4,WEEK_OF_YEAR=29,WEEK_OF_MONTH=3,DAY_OF_MONTH=16,DAY_OF_YEAR=198,DAY_OF_WEEK=4,DAY_OF_WEEK_IN_MONTH=3,AM_PM=1,HOUR=4,HOUR_OF_DAY=16,MINUTE=19,SECOND=47,MILLISECOND=823,ZONE_OFFSET=19800000,DST_OFFSET=0]

and i've stored it in a database this way: 并且我以这种方式将其存储在数据库中:

values.put(KEY_LAST_CHECKUP, (doctor.getLastCheckUpDate().toString())); // last check up date

Now, how do i retrieve the DAY, MONTH and YEAR from the stored string? 现在,如何从存储的字符串中检索DAY,MONTH和YEAR?

I read about how to convert a date string to a calendar object here: How to convert a date String to a Date or Calendar object? 我在这里了解了如何将日期字符串转换为日历对象: 如何将日期字符串转换为日期或日历对象?

but my string is not just a date string. 但是我的字符串不只是日期字符串。 It contains a lot of other details too. 它还包含许多其他细节。

What is the best way forward? 最好的前进方向是什么?

Change your data model to use a Date . 更改数据模型以使用Date This is the usual type to be stored in the database. 这是通常存储在数据库中的类型。

You can set the Date to a Calendar by using 您可以使用以下方式将Date设置为Calendar

Calendar c = Calendar.getInstance();
c.setTime(date);

To retrieve the Date from a Calendar you can use 要从Calendar检索Date ,您可以使用

date = c.getTime();

Using a String to store a Date in a database needs formatting and parsing of the String s and also no comparision iside the database can be done. 使用StringDate存储在数据库中需要对String格式化和解析,并且也无法进行数据库比较。

If you would like to keep string value in KEY_LAST_CHECKUP column. 如果要将字符串值保留在KEY_LAST_CHECKUP列中。 Try to use SimpleDateFormat . 尝试使用SimpleDateFormat

If you keep long value, you don't need to use SimpleDateFormat . 如果保留长值,则无需使用SimpleDateFormat

For insert : 对于插入:

SimpleDateFormat simpleFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
String strDate = simpleFormat.format(doctor.getLastCheckUpDate());
values.put(KEY_LAST_CHECKUP, strDate);

For retrieve: 为了检索:

try {
    String strDate = --> from DB
    Date parsedDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS").parse(strDate);
    Calendar c = Calendar.getInstance();
    c.setTime(parsedDate);
} catch (ParseException e) {
    return "Unknown";
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM