我有时间milliseconds,现在我想与这些分开 .timedatemilliseconds
我怎样才能做到这一点???
我想将日期解析成所需的格式,但我每次都收到一个例外.我知道这很容易实现,但我面临一些问题,不知道究竟在哪里:
Exception: java.text.ParseException: Unparseable date: "2014-06-04" (at offset 5)
Run Code Online (Sandbox Code Playgroud)
以下是我的代码:
private String getconvertdate(String date) {
DateFormat inputFormat = new SimpleDateFormat("yyyy-MMM-dd HH:mm:ss",Locale.ENGLISH);
inputFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
DateFormat outputFormat = new SimpleDateFormat("dd-MMM-yyyy",Locale.ENGLISH);
Date parsed = null;
try {
parsed = inputFormat.parse(date);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
String outputText = outputFormat.format(parsed);
return outputText;
}
Run Code Online (Sandbox Code Playgroud)
方法输入:2014-06-04
预期产出:2014年6月6日
我跟着一些Ref.来自Stackoverflow.com,但他仍然存在问题.请帮忙.
我从两个版本的天气 API 获取日期数据。第一个是这样的字符串:2019-08-07 09:00:00和这样的:1565209665。如何将其更改为仅日期或日期和月份的名称?例如,星期一,八月。
我在几个配置中尝试过类似的东西,但它只能在完整版中使用。如果我发现某些东西,则会引发错误:
var date = list.get(position).dt_txt
val formatter = DateTimeFormatterBuilder()
.appendPattern("yyyy-MM-dd HH:mm:ss").toFormatter()
formatter.parse(date)
Run Code Online (Sandbox Code Playgroud) 我必须创建一个应用程序,我必须在其中设置日期,在特定日期9点钟,我必须发出通知.这样做最简单的方法是什么?即使应用程序被杀,我希望应用程序能够正常运行.AlarmManager是一个解决方案吗?
我使用日期转换器类来转换我的日期对象.但是,我仍然遇到一个错误说.错误:无法弄清楚如何将此字段保存到数据库中.您可以考虑为其添加类型转换器.
我的日期转换器类
public class DateConverter {
@TypeConverter
public static Date toDate(Long dateLong){
return dateLong == null ? null: new Date(dateLong);
}
@TypeConverter
public static long fromDate(Date date){
return date == null ? null :date.getTime();
}
}
Run Code Online (Sandbox Code Playgroud)
我的数据库表用于使用日期对象.
@Entity(tableName = "userFitnessDailyRecords")
@TypeConverters(DateConverter.class)
public class UserFitnessDailyRecords {
@NonNull
@PrimaryKey(autoGenerate = true)
public int id;
public Date forDay;
public Date getForDay() {
return forDay;
}
public void setForDay(Date forDay) {
this.forDay = forDay;
}
}
Run Code Online (Sandbox Code Playgroud)
我按照谷歌代码持久性实验室的例子和普通软件室各自的GitHub示例.我正在使用房间版本1.0.0.
如何获取Android中两个日期之间的所有日期.
例如.我有两个字符串.
String first="2012-07-15";
String second="2012-07-21";
Run Code Online (Sandbox Code Playgroud)
我转换并从这些字符串中获取日期.
DateFormat df1 = new SimpleDateFormat("yyyy-MM-dd");
DateFormat df2 = new SimpleDateFormat("MMM dd");
String mydate = df2.format(df1.parse(first));
Run Code Online (Sandbox Code Playgroud)
这样我从第一个和第二个String获得两个日期.
现在我还显示这两个日期之间的所有日期.
我在我的活动中使用DatePicket,
我想将用户选择的日期限制为今天的日期.
他们不应该选择比今天更大的日期.
谢谢.
通过阅读它们,我了解到 android 类是一个仅为方便起见的实用程序类,并且不提供任何SimpleDateFormat不提供的东西。android类有什么意义吗?使用它是否是更好的做法(如果是,为什么)?我的目标是在 Android 应用程序中显示 Date 对象(显然)。这里是:
private Date date;
public String getDateString(String formatStr){
return new SimpleDateFormat(formatStr).format(date);
}
Run Code Online (Sandbox Code Playgroud)
像这样使用 android.text.format.DateFormat 会更好吗:
public String getDateString(String formatStr){
return android.text.format.DateFormat.format(formatStr, date);
}
Run Code Online (Sandbox Code Playgroud)
或者也许两者都不是,但最好采用不同的方式?提前致谢。
在我的应用程序中有一个时间戳字符串,它是这样创建的:
public String getTimestamp(Date when) {
long now = System.currentTimeMillis();
long past = when.getTime();
return DateUtils.getRelativeTimeSpanString(
past,
now,
DateUtils.SECOND_IN_MILLIS);
}
Run Code Online (Sandbox Code Playgroud)
在巴西的设备中运行此功能时,我收到以下错误:
java.util.UnknownFormatConversionException: Conversion: l
at java.util.Formatter$FormatToken.unknownFormatConversionException(Formatter.java:1430)
at java.util.Formatter$FormatToken.checkFlags(Formatter.java:1367)
at java.util.Formatter.transform(Formatter.java:1473)
at java.util.Formatter.doFormat(Formatter.java:1101)
at java.util.Formatter.format(Formatter.java:1062)
at java.util.Formatter.format(Formatter.java:1031)
at java.lang.String.format(String.java:2177)
at java.lang.String.format(String.java:2151)
at android.text.format.DateUtils.getRelativeTimeSpanString(DateUtils.java:526)
at android.text.format.DateUtils.getRelativeTimeSpanString(DateUtils.java:426)
at com.myapp.Utils.MyAdapter.bindView(MyAdapter.java:98)
at com.myapp.Utils.MyAdapter.getView(MyAdapter.java:60)
at android.widget.AbsListView.obtainView(AbsListView.java:1618)
at android.widget.ListView.makeAndAddView(ListView.java:1772)
at android.widget.ListView.fillDown(ListView.java:695)
at android.widget.ListView.fillFromTop(ListView.java:752)
at android.widget.ListView.layoutChildren(ListView.java:1609)
at android.widget.AbsListView.onLayout(AbsListView.java:1448)
at android.view.View.layout(View.java:7375)
at android.widget.RelativeLayout.onLayout(RelativeLayout.java:912)
at android.view.View.layout(View.java:7375)
at android.widget.FrameLayout.onLayout(FrameLayout.java:338)
at android.view.View.layout(View.java:7375)
at android.widget.FrameLayout.onLayout(FrameLayout.java:338)
at android.view.View.layout(View.java:7375)
at android.widget.RelativeLayout.onLayout(RelativeLayout.java:912)
at android.view.View.layout(View.java:7375)
at android.widget.FrameLayout.onLayout(FrameLayout.java:338) …Run Code Online (Sandbox Code Playgroud) 我一直在想如何正确地测试依赖于Android的日期或时间变化的功能.假设我有每个月的第一天需要处理的事件,这个处理的结果取决于当前的日期/时间.我错过了在Android测试框架中伪造某个日期/时间的可能性.我们该如何测试那种东西?
当然,可以通过不是直接从框架查询日期/时间而是从另外的实体查询日期/时间来实时抽象.可以在测试代码中为此实体设置假日期/时间.但是,Android测试框架中是否真的不支持这样的常见需求?
android-date ×10
android ×9
datetime ×2
java ×2
alarmmanager ×1
android-room ×1
api ×1
date ×1
datepicker ×1
java-time ×1
kotlin ×1