如何使用SimpleDateFormat.parse()将Calendar.toString()转换为日期? [英] How can I Convert Calendar.toString() into date using SimpleDateFormat.parse()?
问题描述
我正在开发一个使用数据库的Android应用,每当用户插入新的寄存器时,使用
<$ p将当前数据和时间保存在db中。 $ p>
日历cal = Calendar.getInstance();
所以,当我从数据库中检索数据时,得到了这样的字符串:
java.util.GregorianCalendar [time = 1496007575129,areFieldsSet = true,lenient = true,zone = America / Mexico_City,firstDayOfWeek = 1,minimalDaysInFirstWeek = 1, ERA = 1,YEAR = 2017,MONTH = 4,WEEK_OF_YEAR = 22,WEEK_OF_MONTH = 5,DAY_OF_MONTH = 28,DAY_OF_YEAR = 148,DAY_OF_WEEK = 1,DAY_OF_WEEK_IN_MONTH = 4,AM_PM = 1,HOUR = 4,HOUR_OF_DAY = 16,MINUTE 39,SECOND = 35,MILLISECOND = 129,ZONE_OFFSET = -21600000,DST_OFFSET = 3600000]
当我尝试将其转换时会出现问题使用SimpleDateFormat.parse将其显示在RecyclerView中的字符串,我总是得到相同的日期:09/04/2017。
这是我的RecViewAdapter.java中的代码:
@Override
public void onBindViewHolder(ViewHolderholder,int position){
items.moveToPosition(position );
字符串s,d,p,f;
s = items.getString(ConsultaTomas.SISTOLICA);
holder.systolica.setText(s);
d = items.getString(ConsultaTomas.DIASTOLICA);
holder.diastolica.setText(d);
p = items.getString(ConsultaTomas.PULSO);
holder.pulso.setText(p);
f = items.getString(ConsultaTomas.FECHA);
SimpleDateFormat sdf = new SimpleDateFormat( dd / MM / yyyy);
试试{
holder.fecha.setText(sdf.format(sdf.parse(f)));
} catch(ParseException e){
Log.d( PARSINGFECHA, Error parcear fecha);
}
}
其他数据在RecView中正确显示,并且Calendar String都不同,因此这些字符串中没有相同的日期/小时。因此,问题是:
如何使用<$ c $将 Calendar.toString()
转换为日期c> SimpleDateFormat.parse()?
这是在真实设备上运行应用程序的结果:
您需要修改存储日历
的方式,调用 getTime()
并根据需要对其进行格式化。例如,
Calendar cal = Calendar.getInstance();
DateFormat sdf = new SimpleDateFormat( MM / dd / yyyy);
System.out.println(sdf.format(cal.getTime()));
I'm developing an Android app that uses a database, every time that the user insert a new register the current data and time is save in the db using
Calendar cal = Calendar.getInstance();
So, When I retrieve the data from the db, got a String like this:
java.util.GregorianCalendar[time=1496007575129,areFieldsSet=true,lenient=true,zone=America/Mexico_City,firstDayOfWeek=1,minimalDaysInFirstWeek=1,ERA=1,YEAR=2017,MONTH=4,WEEK_OF_YEAR=22,WEEK_OF_MONTH=5,DAY_OF_MONTH=28,DAY_OF_YEAR=148,DAY_OF_WEEK=1,DAY_OF_WEEK_IN_MONTH=4,AM_PM=1,HOUR=4,HOUR_OF_DAY=16,MINUTE=39,SECOND=35,MILLISECOND=129,ZONE_OFFSET=-21600000,DST_OFFSET=3600000]
The problem comes when I try convert that String using SimpleDateFormat.parse to display it in a RecyclerView, I get always the same date: 09/04/2017.
This is the code in my RecViewAdapter.java:
@Override
public void onBindViewHolder(ViewHolder holder,int position){
items.moveToPosition(position);
String s,d,p,f;
s = items.getString(ConsultaTomas.SISTOLICA);
holder.systolica.setText(s);
d = items.getString(ConsultaTomas.DIASTOLICA);
holder.diastolica.setText(d);
p = items.getString(ConsultaTomas.PULSO);
holder.pulso.setText(p);
f = items.getString(ConsultaTomas.FECHA);
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
try {
holder.fecha.setText(sdf.format(sdf.parse(f)));
}catch (ParseException e){
Log.d("PARSINGFECHA","Error al parcear fecha");
}
}
The other data is showed correctly in the RecView and the Calendar String are all diferent, so there is not the same date/hour in these strings. So, the question is:
How can I Convert Calendar.toString()
into date using SimpleDateFormat.parse()
?
This is the result running the app in a real device:
You need to modify the way you store the Calendar
, call getTime()
and format it as desired to begin with. For example,
Calendar cal = Calendar.getInstance();
DateFormat sdf = new SimpleDateFormat("MM/dd/yyyy");
System.out.println(sdf.format(cal.getTime()));
这篇关于如何使用SimpleDateFormat.parse()将Calendar.toString()转换为日期?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!