存储/检索日期时间 [英] Storing/retrieving DateTime

查看:92
本文介绍了存储/检索日期时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经被检索是从1970年,如 1324657734883 存储为一个字符串作为的毫秒日期的应用程序。

我有一个的ListView 显示此日期ASIS。我想从数据库中本场显示乔达的DateTime 。我的观点显示列表视图,并通过使用 startManagingCursor()所以我不认为有转换的任何方式以毫秒为单位格式为的DateTime 之前被填充到列表视图

有没有办法解决或我一定要存储一个DateTime和若然是列的类型,我需要声明存储此类型的数据?

 保护无效的onCreate(捆绑savedInstanceState){
    super.onCreate(savedInstanceState);
    nfcscannerapplication =(NfcScannerApplication)getApplication();    //设置UI
    的setContentView(R.layout.viewtransactions);
    的setTitle(getCarername()+已完成+ nfcscannerapplication.loginValidate.getNumberOfTransactions()+今日访问);
    // transactionCount =(的TextView)findViewById(R.id.textviewtransactionsfordaycount);
    viewTransactions =(ListView控件)findViewById(R.id.listviewtransactions);    //transactionCount.setText(\"You已经完成了今天6交易);    //获取数据
    光标= nfcscannerapplication.loginValidate.queryAllFromTransactions();
    startManagingCursor(光标);    //设置适配器和显示数据
    的String [] =从{
            LoginValidate.C_NAME,LoginValidate.C_TAG_SCAN_TIME,
            LoginValidate.C_TAG_SCAN_TIME};
    INT []为= {R.id.rowcarername,R.id.rowsignedinoutstatus,R.id.rowsenttoserverat};    适配器=新SimpleCursorAdapter(nfcscannerapplication,R.layout.rowdataactual,
            游标,从,到);
    viewTransactions.setAdapter(适配器);
}}

[UPDATE1]
    公共类ViewTransactions扩展NfcBaseActivity {

 私有静态最后弦乐TAG = ViewTransactions.class.getSimpleName();NfcScannerApplication nfcscannerapplication;
光标光标;
ListView的viewTransactions;
SimpleCursorAdapter适配器;
MyAdapter myAdapter;
// TextView的transactionCount; //现在写入的状态栏@燮pressWarnings(德precation)
@覆盖
保护无效的onCreate(捆绑savedInstanceState){
    super.onCreate(savedInstanceState);
    nfcscannerapplication =(NfcScannerApplication)getApplication();    //设置UI
    的setContentView(R.layout.viewtransactions);
    的setTitle(getCarername()+已完成+ nfcscannerapplication.loginValidate.getNumberOfTransactions()+今日访问);
    // transactionCount =(的TextView)findViewById(R.id.textviewtransactionsfordaycount);
    viewTransactions =(ListView控件)findViewById(R.id.listviewtransactions);    //transactionCount.setText(\"You已经完成了今天6交易);    //获取数据
    光标= nfcscannerapplication.loginValidate.queryAllFromTransactions();
    startManagingCursor(光标);    //设置适配器和显示数据
    的String [] =从{
            LoginValidate.C_NAME,LoginValidate.C_TAG_SCAN_TIME,
            LoginValidate.C_TAG_SCAN_TIME};
    INT []为= {R.id.rowcarername,R.id.rowsignedinoutstatus,R.id.rowsenttoserverat};    myAdapter =新MyAdapter(nfcscannerapplication,R.layout.rowdataactual,
            游标,从,到);
    viewTransactions.setAdapter(适配器);
}
类MyAdapter扩展SimpleCursorAdapter {    公共MyAdapter(上下文的背景下,INT布局,光标C,的String []从,
            INT []到){
        超(背景下,布局,C,从,到);
    }    @覆盖
    上市
    查看getView(INT位置,查看convertView,父母的ViewGroup){
        视图V = super.getView(位置,convertView,父母);
        如果(V == NULL)
            返回null;        光标C =(光标)的getItem(位置);
        串VAL = c.getString(c.getColumnIndex(LoginValidate.C_TAG_SCAN_TIME));
        日期DT =新的日期(的Long.parseLong(VAL));
        SimpleDateFormat的DF =新的SimpleDateFormat(DD MMMM YYYY);
        字符串资源= df.format(DT);        ((的TextView)v.findViewById(R.id.rowsignedinoutstatus))的setText(RES);
        ((的TextView)v.findViewById(R.id.rowsenttoserverat))的setText(RES);        返回伏;
    }
}}


解决方案

创建扩展 SimpleCursorAdapter 自定义适配器:

 类MyAdapter扩展SimpleCursorAdapter {
    @覆盖
    查看getView(INT位置,查看convertView,父母的ViewGroup){
        视图V = super.getView();
        如果(V == NULL)
            返回null;        光标C =(光标)的getItem(位置);
        串VAL = c.getString(c.getColumnIndex(LoginValidate.C_TAG_SCAN_TIME));
        日期DT =新的日期(的Long.parseLong(VAL));
        SimpleDateFormat的DF =新的SimpleDateFormat(DD MMMM YYYY);
        字符串资源= df.format(VAL);        ((的TextView)v.findViewById(R.id.rowsignedinoutstatus))的setText(RES);
        ((的TextView)v.findViewById(R.id.rowsenttoserverat))的setText(RES);        返回伏;
    }
}

然后代替默认的使用它:

 适配器=新MyAdapter(nfcscannerapplication,R.layout.rowdataactual,光标,从,到);

I've an app that is retrieving a date that is stored as a string as a millisecs from 1970, eg 1324657734883.

I have a ListView that displays this date asis. I'd like to display the joda DateTime from this field in the database. My view displays the listview and populates it by using startManagingCursor() so i don't think there is any way of converting the milisec format to a DateTime before it is populated to the listview.

Is there a way around this or do i have to store a DateTime and if so what is the column type i need to declare to store this type of data?

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    nfcscannerapplication = (NfcScannerApplication) getApplication();

    // setup UI
    setContentView(R.layout.viewtransactions);
    setTitle(getCarername() + " has completed " + nfcscannerapplication.loginValidate.getNumberOfTransactions() + " visits today");
    //transactionCount = (TextView)findViewById(R.id.textviewtransactionsfordaycount);
    viewTransactions = (ListView) findViewById(R.id.listviewtransactions);

    //transactionCount.setText("You have completed 6 transactions today");

    // get data
    cursor = nfcscannerapplication.loginValidate.queryAllFromTransactions();
    startManagingCursor(cursor);

    // setup adapter and show the data




    String[] from = { 
            LoginValidate.C_NAME, LoginValidate.C_TAG_SCAN_TIME,
            LoginValidate.C_TAG_SCAN_TIME};
    int[] to = { R.id.rowcarername, R.id.rowsignedinoutstatus, R.id.rowsenttoserverat };

    adapter = new SimpleCursorAdapter(nfcscannerapplication, R.layout.rowdataactual,
            cursor, from, to);
    viewTransactions.setAdapter(adapter);


}

}

.

[update1] public class ViewTransactions extends NfcBaseActivity{

private static final String TAG = ViewTransactions.class.getSimpleName();

NfcScannerApplication nfcscannerapplication;
Cursor cursor;
ListView viewTransactions;
SimpleCursorAdapter adapter;
MyAdapter myAdapter;
//TextView transactionCount; //now written to status bar

@SuppressWarnings("deprecation")
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    nfcscannerapplication = (NfcScannerApplication) getApplication();

    // setup UI
    setContentView(R.layout.viewtransactions);
    setTitle(getCarername() + " has completed " + nfcscannerapplication.loginValidate.getNumberOfTransactions() + " visits today");
    //transactionCount = (TextView)findViewById(R.id.textviewtransactionsfordaycount);
    viewTransactions = (ListView) findViewById(R.id.listviewtransactions);

    //transactionCount.setText("You have completed 6 transactions today");

    // get data
    cursor = nfcscannerapplication.loginValidate.queryAllFromTransactions();
    startManagingCursor(cursor);

    // setup adapter and show the data




    String[] from = { 
            LoginValidate.C_NAME, LoginValidate.C_TAG_SCAN_TIME,
            LoginValidate.C_TAG_SCAN_TIME};
    int[] to = { R.id.rowcarername, R.id.rowsignedinoutstatus, R.id.rowsenttoserverat };

    myAdapter = new MyAdapter(nfcscannerapplication, R.layout.rowdataactual,
            cursor, from, to);
    viewTransactions.setAdapter(adapter);


}


class MyAdapter extends SimpleCursorAdapter {

    public MyAdapter(Context context, int layout, Cursor c, String[] from,
            int[] to) {
        super(context, layout, c, from, to);
    }

    @Override
    public
    View getView(int position, View convertView, ViewGroup parent) {
        View v = super.getView(position, convertView, parent);
        if(v == null)
            return null;

        Cursor c = (Cursor)getItem(position);
        String val = c.getString(c.getColumnIndex(LoginValidate.C_TAG_SCAN_TIME));
        Date dt = new Date(Long.parseLong(val));
        SimpleDateFormat df = new SimpleDateFormat("dd MMMM yyyy");
        String res = df.format(dt);

        ((TextView)v.findViewById(R.id.rowsignedinoutstatus)).setText(res);
        ((TextView)v.findViewById(R.id.rowsenttoserverat)).setText(res);

        return v;
    }
}

}

解决方案

Create a custom adapter that extends SimpleCursorAdapter:

class MyAdapter extends SimpleCursorAdapter {
    @Override
    View getView(int position, View convertView, ViewGroup parent) {
        View v = super.getView();
        if(v == null)
            return null;

        Cursor c = (Cursor)getItem(position);
        String val = c.getString(c.getColumnIndex(LoginValidate.C_TAG_SCAN_TIME));
        Date dt = new Date(Long.parseLong(val));
        SimpleDateFormat df = new SimpleDateFormat("dd MMMM yyyy");
        String res = df.format(val);

        ((TextView)v.findViewById(R.id.rowsignedinoutstatus)).setText(res);
        ((TextView)v.findViewById(R.id.rowsenttoserverat)).setText(res);

        return v;
    }
}

Then use it in place of the default one:

adapter = new MyAdapter(nfcscannerapplication, R.layout.rowdataactual, cursor, from, to);

这篇关于存储/检索日期时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆