时间格式转换

来源:互联网 发布:数据恢复精灵的注册码 编辑:程序博客网 时间:2024/05/21 17:44

本博客仅为自己开发日记,对此文有任何适的朋友请关闭!

1、项目需求:通过提取数据库文章上传时间,然后转换自己想要的格式

2、开发环境:某RecyclerView的Adapter里,mTextView控件显示数据库得到的时间

3、RecyclerView的Adapter片段代码:

 @Override    public void onBindViewHolder(SinglePhotoViewAdapter.ViewHolder holder, int position) {        holder.mTextView.setText(strDate(newdate(mdate.get(position).getCreatedAt())));
//mdate.get(position).getCreatedAt() 从数据集mdate中获取某一行数据的CreatedAt时间数据    }


4、时间转换代码(string转date或date转string):

public static Date newdate(String time){        Date date = null;        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");        try {            date = sdf.parse(time);        } catch (ParseException e) {            e.printStackTrace();        }        return date;    }
//上面代码先把数据库的String类型的日期转成Date类型,然后通过下面的代码把Date类型日期转成想要的String类型并显示到TextView上    public static String strDate(Date path) {        SimpleDateFormat formatter = new SimpleDateFormat("MM月dd日yyyy年"); //可转换成自己想要的格式,比如:yyyy年MM月dd日,dd日MM月yyyy年等        String newTime = formatter.format(path);        return newTime;    }


1 0
原创粉丝点击