我有一个这样的方法:
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/Process:发送信号。PID: 9921 SIG: 9
发布于 2017-11-10 23:41:35
试着把它当做..
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);发布于 2017-11-10 23:54:01
在这里,您只需要当前时间(以毫秒为单位),它会将其转换为所需格式。
SimpleDateFormat date = new SimpleDateFormat("dd/MM/yyyy", Locale.getDefault());
String currentDate = date.format(System.currentTimeMillis());
Intent myIntent=new Intent(MainActivity.this, ChildActivity.class);
myIntent.putExtra(TIMESTAMP,currentDate);
startActivity(myIntent);https://stackoverflow.com/questions/47226315
复制相似问题