如何将dd/mm/yyyy中的日期转换为支持sqlite的格式yyyy-MM-dd'T'HH:mm:ss
例如:
public static String convertStringToData(String stringData)
throws ParseException {
SimpleDateFormat sdf = new SimpleDateFormat("dd/mm/aaaa");//yyyy-MM-dd'T'HH:mm:ss
SimpleDateFormat output = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date data = sdf.parse(stringData);
String formattedTime = output.format(data);
return formattedTime;
}
Run Code Online (Sandbox Code Playgroud) 我想更改我的日期格式
String date ="29/07/13";
Run Code Online (Sandbox Code Playgroud)
但它显示*错误*Unparseable date:"29/07/2013"(偏移2)*我想以这种格式获取日期2013年7月29日.
这是我用来更改格式的代码.
tripDate = (TextView) findViewById(R.id.tripDate);
SimpleDateFormat df = new SimpleDateFormat("MMM d, yyyy");
try {
oneWayTripDate = df.parse(date);
} catch (ParseException e) {
e.printStackTrace();
}
tripDate.setText(oneWayTripDate.toString());
Run Code Online (Sandbox Code Playgroud) 我getRelativeTimeSpanString (long time, long now, long minResolution, int flags)用来计算相对日期.文档说这flags是"格式化选项的一点掩盖,例如FORMAT_NUMERIC_DATE或FORMAT_ABBREV_RELATIVE".但是,定义了大约十几个其他标志DateUtils,并且只有任何具有任何描述的标志都被弃用.
getRelativeTimeSpanString()?5d,6h和2m,但我没有看到任何东西,看起来像它这样做的.我必须自己动手吗?我试图根据用户的设置获取格式化的日期(年、月、日期)和时间(小时、分钟、秒)字符串。Android Developers Google Group 中的这篇文章描述了我遇到的确切问题,但没有人帮助该人解决问题。我总结一下:
Android 有这些类尝试执行上述操作,但没有一个类同时使用用户首选项和显示秒数。
java.text.DateFormat
不使用设置应用程序中设置的首选项
android.text.format.DateFormat
获取一个java.text.DateFormat使用getDateFormat()and正确格式化输出的对象getTimeFormat(),但getTimeFormat()不包括秒。
android.text.format.DateUtils
不使用设置应用程序中显示日期的首选项,也无法显示秒。
例如,将“设置”中的首选项设置为 DD/MM/YYYY 和 24 小时格式 = on,运行以下代码:
long timestamp=System.currentTimeMillis();
sometextview.setText(
java.text.format.DateFormat.getDateTimeInstance().format(new Date(timestamp))
+"\n"+
android.text.format.DateFormat.getDateFormat(this).format(new Date(timestamp))
+" "+
android.text.format.DateFormat.getTimeFormat(this).format(new Date(timestamp))
+"\n"+
android.text.format.DateUtils.formatDateTime(this,
timestamp,
DateUtils.FORMAT_SHOW_DATE|
DateUtils.FORMAT_SHOW_TIME|
DateUtils.FORMAT_SHOW_YEAR)
);
Run Code Online (Sandbox Code Playgroud)
在文本视图中给我以下输出:
long timestamp=System.currentTimeMillis();
sometextview.setText(
java.text.format.DateFormat.getDateTimeInstance().format(new Date(timestamp))
+"\n"+
android.text.format.DateFormat.getDateFormat(this).format(new Date(timestamp))
+" "+
android.text.format.DateFormat.getTimeFormat(this).format(new Date(timestamp))
+"\n"+
android.text.format.DateUtils.formatDateTime(this,
timestamp,
DateUtils.FORMAT_SHOW_DATE|
DateUtils.FORMAT_SHOW_TIME|
DateUtils.FORMAT_SHOW_YEAR)
);
Run Code Online (Sandbox Code Playgroud)
None 提供所需的输出,使用上述首选项将是这样的:
Apr 27,2014 5:47:18 PM
27/04/2014 17:47
April 27,2014, 17:47 …Run Code Online (Sandbox Code Playgroud) 我想将日期字符串格式化为没有年份(例如:“1/4”)。
int flags = DateUtils.FORMAT_NUMERIC_DATE | DateUtils.FORMAT_NO_YEAR;
Run Code Online (Sandbox Code Playgroud)
上述标志仍然在日期后附加一年(例如:“1/4/2016”)。怎么降年?
如何在 android 开发中自定义日期格式,使其类似于 twitter 和 instagram。我在当前代码下面有什么,但我不喜欢它产生的格式,如“11 分钟前”或“34 分钟前”。我更喜欢 Twitter 格式,如“11m”或“34m”。请有人知道我如何格式化我的日期吗?
Date createdAt = message.getCreatedAt();//get the date the message was created from parse backend
long now = new Date().getTime();//get current date
String convertedDate = DateUtils.getRelativeTimeSpanString(
createdAt.getTime(), now, DateUtils.SECOND_IN_MILLIS).toString();
mPostMessageTimeLabel.setText(convertedDate); //sets the converted date into the message_item.xml view
Run Code Online (Sandbox Code Playgroud) 我正在使用此代码在我的应用程序中获取相对时间:
DateUtils.getRelativeTimeSpanString(
mReferenceTime,
now,
DateUtils.MINUTE_IN_MILLIS,
DateUtils.FORMAT_ABBREV_RELATIVE
);
Run Code Online (Sandbox Code Playgroud)
它正确返回了所有内容,但我想缩短月份。例如,代替“December 21”,它应该返回“Dec 21”。我怎样才能做到这一点?
我有一个 RecyclerView 显示带有标题、文本和上次编辑时间的项目。

在 RecyclerView 适配器的 onBindViewHolder 中,我使用getRelativeTimeSpanString并将笔记对象的时间与当前时间进行比较:
@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
Note note = getItem(position);
holder.title.setText(note.getTitle());
holder.text.setText(note.getText());
String s = DateUtils.getRelativeTimeSpanString(note.getTime(),
System.currentTimeMillis(), 0L, DateUtils.FORMAT_ABBREV_ALL).toString();
holder.timestamp.setText(s);
}
Run Code Online (Sandbox Code Playgroud)
如果我刚刚添加了该项目,它会显示“0 秒前”。我如何随着时间的推移更新它?
在写这个问题时,SO 提出了这个问题:更新 android 中的相对时间,但是我不认为每 X 秒更新一次 RecyclerView 是最好的主意:
holder.timestamp
每 X 秒更新一次)有没有更有效的方法来实现这一目标?
我在尝试获取DateUtils.getRelativeTimeSpanString以返回其在Android中应该遇到的问题时遇到了严重的问题。
我在这里经历过每个类似的问题,而发布的解决方案都没有起作用或没有意义。
这是我的代码:
CharSequence relativeDate =
DateUtils.getRelativeTimeSpanString(System.currentTimeMillis(),
currentNewsItem.getTimeInMilliseconds(),
DateUtils.HOUR_IN_MILLIS, DateUtils.FORMAT_ABBREV_RELATIVE);
dateView.setText(relativeDate);
Run Code Online (Sandbox Code Playgroud)
currentNewsItem.getTimeInMilliseconds方法确实确实返回了很长的时间(例如,刚过去1521759734它看起来正确),并且getRelativeTimeSpanString将正确格式的日期字符串以“ 2018年3月23日”的格式传递给视图,因此它知道正确的日期。但是我需要它通过“ x小时前”或“ x分钟前”,而且我无法终生弄清楚为什么不是这样。
我已经检查了模拟器上的时间,并且问题出现在我的设备上,因此不是时区设置。
文档说,如果时间跨度大于7天,它将恢复为这样的日期字符串,但是我检查了过去的时间,因为旧时间已经过去,转换器网站显示这是4小时前的确切时间。此外,它显示的是正确的日期(今天),而不是小时/分钟前。
我已经尝试过将HOUR_IN_MILLIS,MINUTES_IN_MILLIS和SECONDS_IN_MILLIS常量用于第三个参数,并且尝试删除了最后一个缩写参数,但结果仍然相同。
我尝试了各种公共方法签名来尝试查找可能有效的签名,但它们都具有相同的结果。
有没有人遇到过这种情况,或者有人可以指出我要去哪里错了?非常感谢。
android android-date apache-commons-dateutils android-dateutils java-date