如何使用 SimpleDateFormat.parse() 将 Calendar.toString() 转换为日期?

2023-02-21Java开发问题
2

本文介绍了如何使用 SimpleDateFormat.parse() 将 Calendar.toString() 转换为日期?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我正在开发一个使用数据库的 Android 应用程序,每次用户插入新的寄存器时,当前数据和时间都会保存在数据库中

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]

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.

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.

这是我的 RecViewAdapter.java 中的代码:

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");
    }


}

其他数据在 RecView 中正确显示并且日历字符串都不同,因此这些字符串中的日期/小时不一样.所以,问题是:

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:

如何使用 SimpleDateFormat.parse()Calendar.toString() 转换为日期?

How can I Convert Calendar.toString() into date using SimpleDateFormat.parse()?

这是在真实设备上运行应用程序的结果:2个提线木偶"

This is the result running the app in a real device:

推荐答案

需要修改Calendar的存储方式,调用getTime(),格式化为想要开始.例如,

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() 转换为日期?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

如何使用 JAVA 向 COM PORT 发送数据?
How to send data to COM PORT using JAVA?(如何使用 JAVA 向 COM PORT 发送数据?)...
2024-08-25 Java开发问题
21

如何使报表页面方向更改为“rtl"?
How to make a report page direction to change to quot;rtlquot;?(如何使报表页面方向更改为“rtl?)...
2024-08-25 Java开发问题
19

在 Eclipse 项目中使用西里尔文 .properties 文件
Use cyrillic .properties file in eclipse project(在 Eclipse 项目中使用西里尔文 .properties 文件)...
2024-08-25 Java开发问题
18

有没有办法在 Java 中检测 RTL 语言?
Is there any way to detect an RTL language in Java?(有没有办法在 Java 中检测 RTL 语言?)...
2024-08-25 Java开发问题
11

如何在 Java 中从 DB 加载资源包消息?
How to load resource bundle messages from DB in Java?(如何在 Java 中从 DB 加载资源包消息?)...
2024-08-25 Java开发问题
13

如何更改 Java 中的默认语言环境设置以使其保持一致?
How do I change the default locale settings in Java to make them consistent?(如何更改 Java 中的默认语言环境设置以使其保持一致?)...
2024-08-25 Java开发问题
13