android - SimpleDateFormat 返回字符串?

标签 android

我有这样的方法:

public void onClick(View v) {
  SimpleDateFormat date = new SimpleDateFormat("dd/mm/yyyy");
  Calendar cal=Calendar.getInstance();
  String currentDate =date.format(cal);
  Intent myIntent=new Intent(MainActivity.this, ChildActivity.class);
  myIntent.putExtra(TIMESTAMP,currentDate);
  startActivity(myIntent);
}

我的应用程序崩溃了:

String currentDate =date.format(cal);

我想获取当前日期作为变量的timestamp。我想知道从那行代码返回的值是否不是字符串?对不起,我的愚蠢问题,但我只是一个新手。谢谢 编辑:添加 logcat 以获取更多详细信息

11-10 22:42:00.495 9921-9921/com.example.admin.overtimenote E/AndroidRuntime: FATAL EXCEPTION: main
                                                                          Process: com.example.admin.overtimenote, PID: 9921
                                                                          java.lang.IllegalArgumentException: Bad class: class java.util.GregorianCalendar
                                                                              at java.text.DateFormat.format(DateFormat.java:303)
                                                                              at java.text.Format.format(Format.java:93)
                                                                              at com.example.admin.overtimenote.MainActivity$2.onClick(MainActivity.java:37)
                                                                              at android.view.View.performClick(View.java:5198)
                                                                              at android.view.View$PerformClick.run(View.java:21147)
                                                                              at android.os.Handler.handleCallback(Handler.java:739)
                                                                              at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                              at android.os.Looper.loop(Looper.java:148)
                                                                              at android.app.ActivityThread.main(ActivityThread.java:5417)
                                                                              at java.lang.reflect.Method.invoke(Native Method)
                                                                              at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
                                                                              at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

11-10 22:42:02.461 9921-9921/com.example.admin.overtimenote I/进程:发送信号。 PID:9921 SIG:9

最佳答案

尝试将其用作..

SimpleDateFormat date = new SimpleDateFormat("dd/mm/yyyy");
            Calendar cal=Calendar.getInstance();
            String currentDate =date.format(cal.getTime()); // here
            Intent myIntent=new Intent(MainActivity.this, ChildActivity.class);
            myIntent.putExtra(TIMESTAMP,currentDate);
            startActivity(myIntent);

关于android - SimpleDateFormat 返回字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47226315/

相关文章:

android - 改变方向时不要改变背景

javascript - 在带有 Angular 的 NativeScript 中,如何获取接口(interface)元素的 id?

android - 尝试在Android Studio中创建新项目时发生Gradle错误

android - 如何使用 parse.com android 从不同的表中获取 ObjectId

android - 在 Eclipse 上调试时无法使用 F5 步入功能

android - 检索项目 : No resource found that matches the given name 'android:Widget.Material.Button.Colored' 的父项时出错

java - Android 简单相机 Activity

java - 如果用户不知道以前的密码,是否可以通过 Firebase 恢复密码?

android - RecyclerView 重新附加所有 View ,ViewHolder 中的 PublisherAdView 闪烁(重新加载)。这是正常的吗?我怎样才能避免这种广告闪烁?

android - 为什么 android canvas 中的绘制操作使用 float 而不是 int 来表示 (x,y)?